Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Sep 18 11:14
    aidanjryan commented #32
  • Sep 18 02:05
    aidanjryan commented #32
  • Sep 18 02:05
    aidanjryan commented #32
  • Sep 18 02:04
    aidanjryan commented #32
  • Sep 18 02:03
    aidanjryan commented #32
  • Sep 18 01:53
    aidanjryan opened #32
  • Sep 18 01:10
    aidanjryan commented #31
  • Sep 18 00:59
    aidanjryan commented #31
  • Sep 17 16:40
    kae commented #31
  • Sep 11 15:41
    kae commented #31
  • Sep 11 13:23
    aidanjryan commented #31
  • Sep 11 04:45
    kae opened #31
  • Sep 02 21:20

    jeremydmiller on gh-pages

    Documentation Update for 2.0.0 (compare)

  • Aug 29 20:33

    jeremydmiller on master

    bumping to Oakton 2.0 (compare)

  • Aug 29 20:27

    jeremydmiller on master

    fixing broken test (compare)

  • Aug 29 20:21

    jeremydmiller on master

    Docs for the assembly scanning/… more docs on environment checks (compare)

  • Aug 28 20:39
    jeremydmiller closed #30
  • Aug 28 20:39

    jeremydmiller on master

    tests on LambdaCheck, renamed n… Fleshing out the environment ch… Tests for EnvironmentCheckResul… and 5 more (compare)

  • Aug 28 20:39
    jeremydmiller edited #30
  • Aug 28 20:39
    jeremydmiller edited #30
Jeremy D. Miller
@jeremydmiller
Hey, just getting this started for anyone who may have questions about Oakton
Luke Foust
@lfoust_twitter
Just trying out the library. Works as expected ;) Does it have any support for using dependency injection? I would like my commands to be instantiated using DI
Vadym Kurachevskyi
@hmvs

Hey. Thanks for the library. It's really great. Wanted to ask is there any way how I can mark Flags as required?
For example I have a tool which migrate database.
it should have 3 required parameters:

string connectionString;
IEnumerable<string> paths;
IEnumerable<string> alwaysRun;

I would like to make this all parameters as flags. But can't find a way how I mark it as required. In case of making them all as arguments I don't get how I would pass them all.

Andy Dote
@Pondidum
@lfoust_twitter yes, it does.
static int Main(string[] args)
{
    var executor = CommandExecutor.For(_ =>
    {
        _.RegisterCommand<ScanCommand>();
        _.DefaultCommand = typeof(ScanCommand);
    }, new StructureMapCreator());

    return executor.Execute(args);
}

public class StructureMapCreator : ICommandCreator
{
    public IOaktonCommand Create(Type commandType)
    {
          return _container.GetInstance(commandType) as IOaktonCommand;
    }
}
Andy Dote
@Pondidum

@hmvs You can do this, but as far as I know, you need to use Arguments for the required parameters, and flags for options.

public class MigrateCommand : OaktonCommand<MigrateInput>
{
    public MigrateCommand()
    {
        // our first usage is just ConnectionString, and optionally the AlwaysRun flag
        Usage("Name is required, Option is optional")
            .Arguments(x => x.ConnectionString)    //required
            .ValidFlags(x => x.AlwaysRunFlag); //a flag

        // our second usage is ConnectionString and multiple paths, and optionally the AlwaysRun flag
        Usage("Name is required, Option is optional")
            .Arguments(x => x.ConnectionString, x => x.Paths)    //required
            .ValidFlags(x => x.AlwaysRunFlag); //a flag
    }

    public override bool Execute(MigrateInput input)
    {
        Console.WriteLine($"ConnectionString: {input.ConnectionString}");
        Console.WriteLine($"AlwaysRun: {input.AlwaysRunFlag}");

        Console.WriteLine("Paths:");
        foreach (var path in (input.Paths ?? Enumerable.Empty<string>()))
        {
            Console.WriteLine($"  - {path}");
        }

        return true;
    }
}

public class MigrateInput
{
    public string ConnectionString { get; set; }
    public IEnumerable<string> Paths { get; set; }
    public bool AlwaysRunFlag { get; set; }
}

There are docs on arguments and flags here:
https://jasperfx.github.io/oakton/documentation/arguments/
https://jasperfx.github.io/oakton/documentation/flags/

Vadym Kurachevskyi
@hmvs
@Pondidum thanks. But alwaysrun it's also requred array of paths. So I have one required string and two required arrays.
Andy Dote
@Pondidum

@hmvs the closest I can get to what you are after is this:

public class MigrateCommand : OaktonCommand<MigrateInput>
{
    public MigrateCommand()
    {
        Usage("Migrate things")
            .Arguments(x => x.ConnectionString)
            .ValidFlags(x => x.PathsFlag, x => x.AlwaysRunFlag);
    }

    public override bool Execute(MigrateInput input)
    {
        if (input.PathsFlag == null || input.PathsFlag.Any() == false)
        {
            ConsoleWriter.Write(ConsoleColor.Red, "You must specify the Paths flag");
            return false;
        }

        if (input.AlwaysRunFlag == null || input.AlwaysRunFlag.Any() == false)
        {
            ConsoleWriter.Write(ConsoleColor.Red, "You must specify the AlwaysRun flag");
            return false;
        }

        Console.WriteLine($"ConnectionString: {input.ConnectionString}");

        Console.WriteLine("Paths:");
        foreach (var path in (input.PathsFlag ?? Enumerable.Empty<string>()))
            Console.WriteLine($"  - {path}");

        Console.WriteLine("Always:");
        foreach (var path in (input.AlwaysRunFlag ?? Enumerable.Empty<string>()))
            Console.WriteLine($"  - {path}");

        return true;
    }
}

public class MigrateInput
{
    public string ConnectionString { get; set; }
    public IEnumerable<string> PathsFlag { get; set; }
    public IEnumerable<string> AlwaysRunFlag { get; set; }
}

usage:

dotnet run -- localhost --paths /d/dev/tmp/1.sql /d/dev/tmp/2.sql --always-run /d/dev/tmp/3.sql
Vadym Kurachevskyi
@hmvs
Thanks. @Pondidum. Though I think we need to make mandatory flags
Andy Dote
@Pondidum
I'd me more inclined for named arguments, I feel that flags are by nature optional, although I haven't run into needing multiple enumerable arguments so it's not been a problem for me
Jeremy D. Miller
@jeremydmiller
@hmvs If it’s mandatory, just use an argument. There’s nothing stopping you from validating the command line input in the commands for that matter and throwing an explanatory exception if flag values are missing
Mo Zaatar ﮼محمدزعتر﮼
@MZaatar_twitter
Hello, anyone has any idea how could I pass a default flag (without adding -- in front of it). Example:
My current command is myExe --option1 "option1" --option2 "option2"
What I want to achieve is be able to run something like
myExe "option1" --option2 "option2"
myExe --option1 "option1" --option2 "option2"
Andy Dote
@Pondidum

You can achive something like what you are after...but it's not entirely pleasant. It involves using an argument and a flag, and specifying usages so that you either can use "argument + flag" or "flag + flag":

    public class TestInput
    {
        public string One { get; set; }
        public string OneFlag { get; set; }
        public string TwoFlag { get; set; }
    }

    public class TestCommand : OaktonCommand<TestInput>
    {
        public TestCommand()
        {
            Usage("default flag").Arguments(x => x.One).ValidFlags(x => x.TwoFlag);
            Usage("do it yourself flag").ValidFlags(x => x.OneFlag, x => x.TwoFlag);
        }

        public override bool Execute(TestInput input)
        {
            Console.WriteLine($"1: {input.One ?? input.OneFlag}");
            Console.WriteLine($"2: {input.TwoFlag}");

            return true;
        }
    }

Example usage:

# working usages:
dotnet OaktonTesting.dll test --one wat --two is
dotnet OaktonTesting.dll test wat --two is

# throws an error, as this is not a valid usage:
dotnet OaktonTesting.dll test omg --one wat --two is
Mo Zaatar ﮼محمدزعتر﮼
@MZaatar_twitter
Hey @Pondidum thanks for your reply. That was my guess but I thought that there is an easier one
Will implement it anyway as I want it :)
Mohamed Bareche
@neoexa
This message was deleted

Hello i have an opts file where i defined some flags, when running my command the program appends the flags this way
mytool -u MyUserName -p ~~HeMan2345 somecommand
instead of

mytool somecommand -u MyUserName -p ~~HeMan2345
which gives me an error : -u is not a command

Any tips ?! Thanks

Jeremy D. Miller
@jeremydmiller
It’s a bug if anything
Mohamed Bareche
@neoexa
@jeremydmiller Ok, i'll look more into it (i'll keep you updated)
Jeremy D. Miller
@jeremydmiller
image.png