Despite the popularity of application servers and single page web apps, there are still times when a simple console application is the easiest - and best - way to solve some kinds of problems.

I’ve published an open-source project to GitHub to make it easier to write these console applications by providing a simple convention based approach for handling the command line parameters. This project is also available through nuget for simple use.

To use the Niche.CommandLineProcessor, write a simple driver class that abides by one of these conventions:

For a simple switch, write a method that takes no parameters and returns no value. Decorate it with [Description] to opt-in for handling as a switch. The name of the method is used to define a pair of flags for the command line.

[Description("Provide help on available parameters.")]
// This method defines -h and --help
public void Help() { ... }

For a parameter that takes a single value, write a method that takes one parameter and returns no value. Again, you ‘ must decorate it with [Description] to opt-in for handling as a parameter, and the name of the method is used to define a pair of flags.

[Description("Specify the output file to write.")]
// This method defiles -of and --output-file
public void OutputFile(string fileName) { ... }

For a parameter that can be specified multiple times, write a method that takes a sequence of values and returns no value. Again, you ‘ must decorate it with [Description] to opt-in for handling as a parameter, and the name of the method is used to define a pair of flags.

[Description("Specify a file to process.")]
// This method defiles -rf and --read-file
public void ReadFile(IEnumerable<string> fileName) { ... }

You can also specify the parameter as IList<string> or List<string>.

For more information, see the project page on GitHub.

Comments

blog comments powered by Disqus
Next Post
Slides for FSIS, The Authorised Biography  18 Oct 2014
Prior Post
Becoming a Better Developer  18 Sep 2014
Related Posts
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
When are you done?  18 Apr 2022
Fixing GitHub Authentication  28 Nov 2021
Archives
September 2014
2014