Skip to content

Instantly share code, notes, and snippets.

@dgg
Created March 22, 2017 09:12
Show Gist options
  • Save dgg/d47be564e2ffecd93e57491dfd86e5b1 to your computer and use it in GitHub Desktop.
Save dgg/d47be564e2ffecd93e57491dfd86e5b1 to your computer and use it in GitHub Desktop.
not-last-console-CommandLineUtils
*NotLastConsole_CommandLineUtils*>doer something --help
Usage: something [options]
Options:
-l | --location place where the something was done
-t | --times number of times something was done
-a | --awesome include if the something was awesome
-h | -? | --help Show help information
*NotLastConsole_CommandLineUtils*>doer -?
Usage: [options] [command]
Options:
-h | -? | --help Show help information
Commands:
something Do something
something-else Do something else
Use " [command] --help" for more information about a command.
public class Something : CommandLineApplication
{
public Something(string optionHelpTemplate)
{
Name = "something";
Description = "Do something";
_location = Option("-l | --location", "place where the something was done", CommandOptionType.SingleValue);
_times = Option("-t | --times", "number of times something was done", CommandOptionType.SingleValue);
_awesome = Option("-a | --awesome", "include if the something was awesome", CommandOptionType.NoValue);
HelpOption(optionHelpTemplate);
OnExecute((Func<int>) run);
}
private int run()
{
if (!_location.HasValue())
{
Error.WriteLine($"The argument '{_location.LongName}' is required.");
ShowHelp(Name);
return 1;
}
var cmd = new ADoerOfSomething(Out);
int times;
if (!int.TryParse(_times.Value(), NumberStyles.Integer, CultureInfo.InvariantCulture, out times))
{
times = 1;
}
OptionsForSomething options = new OptionsForSomething
{
Location = _location.Value(),
Awesome = _awesome.HasValue(),
Times = times
};
cmd.Do(options);
return 0;
}
private readonly CommandOption _location, _awesome, _times;
}
static void Main(string[] args)
{
var app = new CommandLineApplication();
app.HelpOption("-h | -? | --help");
app.Commands.Add(new Something(app.OptionHelp.Template));
CommandArgument locations = null;
CommandOption awesome = null;
app.Command("something-else", somethingElse =>
{
somethingElse.Description = "Do something else";
somethingElse.HelpOption(app.OptionHelp.Template);
locations = somethingElse.Argument(
"-l | --locations",
"places where the something else was done",
multipleValues: true);
awesome = somethingElse.Option("-a | --awesome", "include if the something else was awesome",
CommandOptionType.NoValue);
}).OnExecute(() =>
{
var cmd = new ADoerOfSomethingElse(app.Out);
cmd.Do(new OptionsForSomethingElse
{
Locations = locations.Values.ToArray(),
NotSoAwesome = !awesome.HasValue()
});
return 0;
});
app.Execute(args);
}
}
*NotLastConsole_CommandLineUtils*>doer something-else here there
I did something else not so awesome in here, there
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment