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