commandline
commandline copied to clipboard
Add short application description under name on all help screens
Currently if I type application name help screen does not contain any short description of application. If I use help on windows dir command:
`C:\Users\Example>dir /?
Displays a list of files and subdirectories in a directory.
...help text continues
I think that it's nice way of communicating for user, what this tool does (in few words).
Current output:
>MyApp.exe
MyApp 1.0.0
Copyright (C) 2018 MyApp
verb1 Help text for verb 1
verb2 Help text for verb 2
help Display more information on a specific command.
version Display version information.
Would be nice output:
>MyApp.exe
MyApp 1.0.0
Utility for mass renaming files
Copyright (C) 2018 MyApp
verb1 Help text for verb 1
verb2 Help text for verb 2
help Display more information on a specific command.
version Display version information.
I think System.Reflection.AssemblyDescriptionAttribute would be good source of this description.
Currently, as a workaround, you can use Copyright
project property like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Copyright>First line. Copyright info if it is needed.
Description of your application. You can use 
 to introduce a line break.
Another line of the description.
You can also use some project properties like $(Version)</Copyright>
</PropertyGroup>
Then you will get the description printed in the beginning.
> YourProgram.exe --help
YourProgram 0.0-dev
First line. Copyright info if it is needed.
Description of your application. You can use
to introduce a line break.
Another line of the description.
You can also use some project properties like 0.0-dev
Better yet, use the help customisation features.
It's a little more involved, especially if you're using the async
methods, but fairly straightforward, and allows you to use the Description field (which is now multi-line, at least in .net core apps).
Update your main method:
static async Task Main(string[] args)
{
var parser = new Parser(with => with.HelpWriter = null);
var parserResult = parser.ParseArguments<Options>(args);
if (parserResult.Tag == ParserResultType.Parsed)
{
await parserResult.WithParsedAsync(YourMethod);
}
else
{
await parserResult.WithNotParsedAsync(errs => DisplayHelp(parserResult, errs));
}
}
Then create the following DisplayHelp
method:
static Task DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errs)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string description = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>()?.Description;
var helpText = HelpText.AutoBuild(result, h =>
{
if (!string.IsNullOrEmpty(description))
{
// If assembly description isn't null, add it as a line above the options, below the copyright
h.AddPreOptionsLine(description);
}
return HelpText.DefaultParsingErrorsHandler(result, h);
}, e => e);
Console.WriteLine(helpText);
return Task.CompletedTask;
}
If you're not using the async
methods you can just chain it all together:
static void Main(string[] args)
{
var parser = new Parser(with => with.HelpWriter = null);
var parserResult = parser.ParseArguments<Options>(args);
parserResult.WithParsed(options => YourMethod(options)).WithNotParsed(errs => DisplayHelp(parserResult, errs));
}
This would generate:
> YourProgram.exe --help
YourProgram 0.0.1
Copyright © 2022 Company Name
Description text appears here.
Line breaks are supported as expected
--help Display this help screen.
--version Display version information.