Command line arguments

Brand new class Energy.Base.Command.Arguments appeared here to ease implement command line options for everybody.

This class is build with Named Parameter Idiom design pattern.

Argument parsing mechanism was inspired by excelent npm package yargs for JavaScript.

var argv = new Energy.Base.Command.Arguments(args)
    .Switch("help")
    .Switch("quiet")
    .Parameter("input")
    .Parameter("output")
    .Alias("?", "help")
    .Alias("q", "quiet")
    .Alias("i", "input")
    .Alias("o", "input")
    .Help("input", "Input file")
    .Help("output", "Output file")
    .Parse();

if (!argv["help"].Empty)
{
    Console.WriteLine("Help");
}

Constructor

Energy.Base.Command.Arguments(string[] args)
Energy.Base.Command.Arguments(string line)

You can still add argument line later.

Energy.Base.Command.Arguments()

Parameter

Add command line parameter that will consume one or more trailing arguments and provide them as values.

If you set count to 0, it will become switch (flag).

Arguments Parameter(string name, int count)

Add command line parameter that will consume one next trailing argument and provide it as value.

Arguments Parameter(string name)

Special one.

Treat all unknown options as parametered.

Arguments Parameter()

Switch

Add single command line switch option, known also as flag. Like “-v”, or “–version”.

Arguments Switch(string name)

Special one.

Treat all unknown options as simple switches.

Arguments Switch()

Line

Add arguments from text line.

Arguments are divided by any whitespace character.

Arguments may use double quote (”) character to include whitespace, and multiple quoting is allowed within one argument.

For example: C:”Documents and settings””Program Files”will be considered as one argument.

Arguments Line(string line)

Strict

Set strict mode.

When strict mode is set, exception will be thrown on unrecognized option name.

Arguments Strict(bool strict)

Slash

Allow usage of slash options (starting with “/”).

Allows to use DOS style options like “/?”.

It’s RSX-11 (and other similar DEC systems), through CP/M to MS-DOS legacy.

Should be avoided probably.

Arguments Slash(bool slash)

Short

Allow usage of short options (starting with “-”).

Turned on as default.

Arguments Short(bool enable)

Long

Allow usage of long options (starting with “–”).

Turned on as default.

Arguments Long(bool enable)

Alias

Add alias to parameter key.

Arguments Alias(string alias, string name)

Help

Add help description for parameter key.

Arguments Help(string name, string description)

Parse

Probably most important function here.

Parse arguments and set values for options.

Arguments Parse()

Parameterless method will parse arguments set by constructor or modified later.

When calling with parameters, arguments to be parsed will only be taken from invoker.

Arguments Parse(string line)
Arguments Parse(string[] args)