Skip to content

Instantly share code, notes, and snippets.

@dgg
Last active February 16, 2017 14:55
Show Gist options
  • Save dgg/2f3fd37480a1cc8917f2ba6bdf6c723d to your computer and use it in GitHub Desktop.
Save dgg/2f3fd37480a1cc8917f2ba6bdf6c723d to your computer and use it in GitHub Desktop.
not-last-console-CommandLineParser
[Verb("something", HelpText = "Do something")]
public class Something
{
[Option('l', "location", HelpText = "place where the something was done", Required = true)]
public string Location { get; set; }
[Option('t', "times", HelpText = "number of times something was done", Default = 1, Required = false)]
public int Times { get; set; }
[Option('a', "awesome", HelpText = "include if the something was awesome", Required = false)]
public bool Awesome { get; set; }
}
[Verb("something-else", HelpText = "Do something else")]
public class SomethingElse
{
[Option('l', "locations", HelpText = "places where the something else was done", Required = true, Separator = ',')]
public IEnumerable<string> Locations { get; set; }
[Option('a', "awesome", HelpText = "include if the something else was awesome", Required = false, Default = true)]
public bool Awesome { get; set; }
}
*NotLastConsole_CommandLineParser*> .\doer.exe help something
NotLastConsole_SystemCommandLine 1.0.0.0
Copyright © 2017
-l, --location Required. place where the something was done
-t, --times (Default: 1) number of times something was done
-a, --awesome include if the something was awesome
--help Display this help screen.
--version Display version information.
*NotLastConsole_CommandLineParser*> .\doer.exe
NotLastConsole_SystemCommandLine 1.0.0.0
Copyright © 2017
ERROR(S):
No verb selected.
something Do something
something-else Do something else
help Display more information on a specific command.
version Display version information.
static void Main(string[] args)
{
Parser.Default.ParseArguments<Something, SomethingElse>(args).MapResult(
(Something something) =>
{
var options = new OptionsForSomething
{
Location = something.Location,
Times = something.Times,
Awesome = something.Awesome
};
var command = new ADoerOfSomething(Console.Out);
command.Do(options);
return 0;
},
(SomethingElse somethingElse) =>
{
var options = new OptionsForSomethingElse
{
Locations = somethingElse.Locations.ToArray(),
NotSoAwesome = !somethingElse.Awesome
};
var command = new ADoerOfSomethingElse(Console.Out);
command.Do(options);
return 0;
},
errors => -1);
}
*NotLastConsole_CommandLineParser*> .\doer.exe something
NotLastConsole_SystemCommandLine 1.0.0.0
Copyright © 2017
ERROR(S):
Required option 'l, location' is missing.
-l, --location Required. place where the something was done
-t, --times (Default: 1) number of times something was done
-a, --awesome include if the something was awesome
--help Display this help screen.
--version Display version information.
*NotLastConsole_CommandLineParser*> .\doer.exe something-else -l here,there
I did something else in here, there
*NotLastConsole_CommandLineParser*> .\doer.exe something -l here -t asd
NotLastConsole_SystemCommandLine 1.0.0.0
Copyright © 2017
ERROR(S):
Option 't, times' is defined with a bad format.
-l, --location Required. place where the something was done
-t, --times (Default: 1) number of times something was done
-a, --awesome include if the something was awesome
--help Display this help screen.
--version Display version information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment