In the second part of the SmartCon series, I want to talk a little bit about command line arguments help.
As a guy raised in the 80s, I am used to the command line. Most PCs (if you could afford one) weren’t even able to show a graphical user interface. So, all I had was this ugly
c:\> prompt an a DOS box or the
bash$ prompt on a unix/linux box. The only command I realy knew of, which worked out of the box was
turbo, which started the Turbo-Pascal IDE on the DOS-box at school.
When making my first steps on the command line, I realized, a lot of commands offered tons of information via the
--help command line argument. And on most commands, there are also tons of command line switches you can use.
When writing my first console apps, I repeatedly wrote code handling a
-h command line switch, which always produced a long string, stored in the binary and passes the string to the console output. After the first run realizing all the typos I had in the description, correcting the typos, recompile, re-run, then looking at ugly line-breaks, correcting the line-breaks, re-compile, re-run etc. So, how can can we do this smarter?
SmartCon offers two built in mechanisms for easily providing command line help via the
ArgumentHelpProvider class. The easiest way to enrich you console app with a nice help message is via a XML file containing the desired content to be shown.
So, first step is to create an
App.config file, if you don’t already have it. Then you need to register a new config section for your command line documentation:
This enriches the
App.config file with a new section named
Help, where you can define your command line switches and the corresponding text to be shown when the user asks for the command line help.
In this example, we are defining two entries in the help section, one entry for the command line switch
-f filename and one for the command line switch
The second thing you need to do to provide the user with your newly created help information, is to set up a command line handler for the
-h command line switch, which outputs the help using the previously mentioned
There is also a second way of defining the command line help inline in your code. You can decorate every class with the
Documentation attribute, specifying the
Description properties containing the desired information.
Please note that the
ArgumentHelpProvider is initialized with the type of the class, which was decorated with the
Documentation attribute (here:
Finally, calling the program with the
-h command line switch, will produce the following output:
Source is available via github, package available via nuget.org: