Built-in help
ITGlobal.CLI command line parser contains a built-in support for auto-generated help.
Using an auto-generated help
Parser supports two ways to display auto-generated help:
-
Using a
-hor--helpswitchAppend a
-hor a--helpto a command line to display help an exit. If a command is specified - a help on this command will be printed. Otherwise a generic help will be displayed.So, you should type
my-app --helpto get help on root command and global options.
Typing
my-app my command name --helpwill give you a help on specific command.
-
Using an implicit help
If you’re using a
CliParser.NewTreeParser()and you run your application without any command line arguments, an implicit help on root commands will be displayed
Programmatic usage access
Parser provides a programmatic access to auto-generated help:
var parser = CliParser.NewTreeParser();
// TODO Configure commands and options...
var command = parser.Command("command");
// Get a root usage from parser
var rootUsage = parser.GetUsage();
// Get a command-specific usage from a command
var commandUsage = command.GetUsage();
You may use this data for various purposes, e.g. auto generating MAN pages or text manuals.
Disabling built-in help
In some cases you might need to opt-out built-in help feature.
-
To disable
-hand--helpswitches:Set
CliParser.NewTreeParser()/CliParser.NewSimpleParser()’sdisableHelpSwitchmethod parameter totrue:var parser = CliParser.NewTreeParser(disableHelpSwitch: true); // or var parser = CliParser.NewSimpleParser(disableHelpSwitch: true);This will prevent parser from adding
-h/--helpswitches and their hooks. -
To disable implicit help:
Set
CliParser.NewTreeParser()’sdisableImplicitHelpmethod parameter totrue:var parser = CliParser.NewTreeParser(disableImplicitHelp: true);This will prevent parser from adding default
OnExecutecallback, allowing you to set your own one.Note that implicit help is avaiable for
CliParser.NewTreeParser()only. -
To disable
helpcommand:Set
CliParser.NewTreeParser()’sdisableHelpCommandmethod parameter totrue:var parser = CliParser.NewTreeParser(disableHelpCommand: true);This will prevent parser from adding
helpcommand.Note that help command is avaiable for
CliParser.NewTreeParser()only.
Customizing help output
You may override the way help output is rendered to console. For example, you might need to localize it or to change its visual style.
In order to archieve that, you will need to:
-
Implement a
IHelpPrinterinterface in a custom class (try usingHelpPrinterBaseto simplify your code):public class MyHelpPrinter : HelpPrinterBase { // TODO implement required methods } -
Pass an instance of
IHelpPrintertoICliParser.UseHelpPrinter()method:var parser = CliParser.NewTreeParser(); parser.UseHelpPrinter(new MyHelpPrinter());