Repository navigation
Help_Text
CommandLine provides automatic help text generation feature. If application is called with /?, -?, /h, -h, /help or --help option, then the help text will be displayed.
The following application will be used for demonstration:
[Description("This test application. Type /? for help.")]
public class Program
{
private static int Main(string[] arguments)
{
/* ... */
}
[Description("Says hello to specified 'name'.")]
public static int Hello(
[Description("Name to greet.")]
string name
)
{
/* ... */
}
}Requesting help without specifying a verb displays a list of available verbs.
>myapp /?
This test application. Type /? for help.
Verbs:
HELLO Says hello to specified 'name'.
If a verb is specified before of the request for help option, then detailed help for that verb will be displayed:
>myapp HELLO /?
Usage:
HELLO --name <TEXT>
Says hello to specified 'name'.
Options:
--name Name to greet.Help is displayed for nested verbs as well. Put the help option after the verb chain:
>myfinance Account /? # list of 'Account' sub-verbs
>myfinance Account ShowBalance /? # help for one sub-verbIn the sub-verb list, the description of the sub-verb class is not shown by default. Set CommandLineConfiguration.OutputSubVerbHelpTitle to true to show it. Sub-verbs inherit this setting, so it could be set once on the root builder or in appsettings.json, or on a sub-verb builder for that sub-verb set only (before version 2.2.1 only the latter worked):
public static int Account(ICommandLineBuilder subVerbBuilder) =>
subVerbBuilder
.Configure(config => config.OutputSubVerbHelpTitle = true)
.Use<AccountApi>() // [Description] of AccountApi is shown in 'myfinance Account /?'
.Run();Requesting help returns CommandLineConfiguration.HelpExitCode (2 by default), not 0. See Exit codes.
You can customize the help text by specifying DescriptionAttribute or HelpTextAttribute attributes on the class, method or parameter.
In addition to this, you can hide a verb or an option from the reference using HiddenAttribute attribute. This will not prevent its use. If you want to mark a method as "not a verb" then use NonVerbAttribute attribute.
If you want to change the text used when displaying help, then you need to provide your own IHelpTextProvider service via your own IServiceProvider. It provides:
- fixed text fragments such as
VerbUsageText("Usage: "),VerbOptionsText, error messages ("Verb '{0}' is not found.") andHelpHeaderText/HelpFooterTextprinted around every help message; -
TrySubstituteHelpTextForto translate texts from[Description]/[HelpText]attributes; -
TryGetParameterTypeFriendlyNameto change type names like<TEXT>.
class RussianHelpTextProvider : IHelpTextProvider
{
public string VerbUsageText => "Использование: ";
public string VerbOptionsText => "Параметры: ";
// ... other texts
public bool TrySubstituteHelpTextFor(string helpText, out string? newHelpText) =>
translations.TryGetValue(helpText, out newHelpText);
public bool TryGetParameterTypeFriendlyName(VerbParameter parameter, out string? friendlyName)
{
friendlyName = null;
return false; // keep default names
}
}services.AddService(typeof(IHelpTextProvider), new RussianHelpTextProvider());