Skip to content

Instantly share code, notes, and snippets.

@dgg
Created March 23, 2017 07:49
Show Gist options
  • Save dgg/0c65baf4abcb455dc68d6328eaf3e169 to your computer and use it in GitHub Desktop.
Save dgg/0c65baf4abcb455dc68d6328eaf3e169 to your computer and use it in GitHub Desktop.
not-last-console-CLAP
*NotLastConsole_CLAP*>doer -h
something: Do something.
/a /awesome : include if the something was awesome
/l /location : place where the something was done (String) (Required)
/t /times : number of times something was done (Int32) (Default = 1)
something-else|somethingelse: Do something else.
/a /awesome : include if the something else was awesome
/l /locations : places where the something else was done (String[]) (Required)
Global Parameters:
/?|h|help : Help
public class Doer
{
private readonly TextWriter _dependencyForAVerb;
public Doer(TextWriter dependencyForAVerb)
{
_dependencyForAVerb = dependencyForAVerb;
}
[Verb(Description = "Do something.")]
public void Something(
[Required, Description("place where the something was done")]
string location,
[Description("number of times something was done"), DefaultValue(1)]
int times,
[Description("include if the something was awesome")]
bool awesome)
{
var cmd = new ADoerOfSomething(_dependencyForAVerb);
var options = new OptionsForSomething
{
Location = location,
Times = times,
Awesome = awesome
};
cmd.Do(options);
}
[Verb(Aliases = "something-else", Description = "Do something else.")]
public void SomethingElse(
[Required, Description("places where the something else was done")]
string[] locations,
[Description("include if the something else was awesome")]
bool awesome)
{
var cmd = new ADoerOfSomethingElse(_dependencyForAVerb);
var options = new OptionsForSomethingElse
{
Locations = locations,
NotSoAwesome = !awesome
};
cmd.Do(options);
}
}
static void Main(string[] args)
{
var parser = new Parser<Doer>();
parser.Register.EmptyHelpHandler(Console.WriteLine);
parser.Register.HelpHandler("?,h,help", Console.WriteLine);
parser.Register.ErrorHandler(ex =>
{
ex.ReThrow = false;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Exception.Message);
Console.ResetColor();
});
parser.Run(args, new Doer(Console.Out));
}
*NotLastConsole_CLAP*>doer something
Missing argument for required parameter 'location'
*NotLastConsole_CLAP*>doer something-else -l:here,there
I did something else not so awesome in here, there
*NotLastConsole_CLAP*>doer something -l:here -t:asd
'asd' cannot be converted to System.Int32
*NotLastConsole_CLAP*>doer something -l:here -t=3 -a
I did something awesome 3 times in here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment