Today I am going to write my last console application. I am done with it. My fingers refuse to type (G-d forbid) any parsing code or import any more command-line parsers and/or frameworks.
No more baby seals slaughtered
I bet that vast majority of computer language tutorials or courses start with some sort of program that handles console input/output.
I have done my fair share of them at university using diverse languages: Pascal, C, Assembler, C++,… even LISP! For each one of those languages I wrote some clumsy, most likely buggy, command-line argument parsing. I was so focused on the problem in hand (and was such a mediocre programmer, now that we come to admit things) that I did not even have a personal utility or learnt to use a library for the purpose.
Hell, even when I decided to become a professional developer I have written some embarrassing parsing code for console programs.
Fortunately enough, as I became lazier and lazier (some people call it experience, some… don’t) I learnt that some people wrote libraries to help do something I was not very good at, and I started using some of them.
What follows is a sketch of my travel through some command line parsing libraries and the one I am ditching them all for.
Let’s Frame It, Shall We?
If I want to exemplify my evolution I have to establish a baseline. A common example that will be implemented using all the libraries, for the sake of comparison.
The example is as simple as examples should be:
- two commands “do something” and “do something else”, represented by the
DoerOfSomething
andDoerOfSomethingElse
classes - each command is dispatched depending on the proper command line argument
- each command takes its slightly different own parameter object:
OptionsForSomething
andOptionsForSomethingElse
- each parameter object is built according to the command line arguments passed
- parameter objects have a couple of simple and very common features: types that differ from
string
and a list argument - each command writes a message to a provided
TextWriter
parameterized by the passed argument
Going Custom
I am too embarrassed to show snapshots of my buggy code (one can always head to Github and make fun of me). Nonetheless I bothered to split the dispatching part (execute one of the two commands) and the options parsing (“awesome” code inside ®).
This awful code shows the effort required to do simple things: show help, validate that we can execute a command, validate the creation of the options,…
Next?
Next time, we will be looking at code that can be really used for professional purposes using Mono.Options.
Last Console Series
- The Beginning (this)
- Mono.Options
- NConsoler
- ManyConsole
- Powershell