Handles parsing of command line arguments.
- Switch argument: The presence of the switch acts as a boolean value.
- Example:
-foo
- Example:
- Value argument: Simply a value on the command line.
- Example:
"./etc/foo.config"
or1
- Example:
- Compound argument: Consists of a switch followed by a value, separated by a space.
- Example:
-path "./etc/foo.config"
or-iterations 1
- Example:
- Case sensitivity
- Aliases
- Ordering
- Mutual exclusivity
- Optional/mandatory
- Acceptable values
The library revolves around the concept of a class containing decorated properties. The raw command line arguments are parsed and their values are put into the decorated properties. See the example code below.
There are many attributes that can be used based on the expected type of an argument. The following are all the attributes that can be used to decorate properties so that they will be treated as arguments.
- Compound argument
- Property type: System.Byte
- Converts an argument to a System.Byte value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.Byte
- Converts an argument to a System.Byte value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: System.DateTime
- Converts an argument to a System.DateTime value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.DateTime
- Converts an argument to a System.DateTime value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: System.Decimal
- Converts an argument to a System.Decimal value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.Decimal
- Converts an argument to a System.Decimal value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.String
- Converts an argument to a System.String value.
- Non-optional: Indicator
- Optional: Aliases, CaseSensitive, Description, Groups, Must Exist, Optional, Ordinal, ShortName
- Value argument
- Property type: System.String
- Converts an argument to a System.String value.
- Non-optional: Ordinal
- Optional: Description, Groups, MustExist, Optional, Ordinal, ShortName
- Compound argument
- Property type: System.Double
- Converts an argument to a System.Double value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.Double
- Converts an argument to a System.Double value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: Custom enumeration
- Converts an argument to a custom enumeration value.
- Non-optional: Indicator
- Optional: Aliases, CaseSensitive, Description, Groups, Optional, Ordinal, ShortName, ValueCaseSensitive
- Value argument
- Property type: Custom enumeration
- Converts an argument to a custom enumeration value.
- Non-optional: Ordinal
- Optional: Description, Groups, Optional, Ordinal, ShortName, ValueCaseSensitive
- Compound argument
- Property type: System.String
- Converts an argument to a System.String value.
- Non-optional: Indicator
- Optional: Aliases, CaseSensitive, Description, Groups, Must Exist, Optional, Ordinal, ShortName
- Value argument
- Property type: System.String
- Converts an argument to a System.String value.
- Non-optional: Ordinal
- Optional: Description, Groups, MustExist, Optional, Ordinal, ShortName
- Compound argument
- Property type: System.Int16
- Converts an argument to a System.Int16 value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.Int16
- Converts an argument to a System.Int16 value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: System.Int32
- Converts an argument to a System.Int32 value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.Int32
- Converts an argument to a System.Int32 value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: System.Int64
- Converts an argument to a System.Int64 value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.Int64
- Converts an argument to a System.Int64 value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: System.SByte
- Converts an argument to a System.SByte value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.SByte
- Converts an argument to a System.SByte value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: System.Single
- Converts an argument to a System.Single value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.Single
- Converts an argument to a System.Single value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: System.String
- Converts an argument to a System.String value.
- Non-optional: Indicator
- Optional: Aliases, CaseSensitive, Description, Groups, Optional, Ordinal, ShortName
- Value argument
- Property type: System.String
- Converts an argument to a System.String value.
- Non-optional: Ordinal
- Optional: Description, Groups, Optional, Ordinal, ShortName
- Switch argument
- Property type: System.Boolean
- If present, the property is set to true (or false if Reverse is set to true).
- Non-optional: Indicator
- Optional: Aliases, CaseSensitive, Description, Groups, Optional, Ordinal, ShortName, Reverse
- Compound argument
- Property type: System.UInt16
- Converts an argument to a System.UInt16 value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.UInt16
- Converts an argument to a System.UInt16 value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: System.UInt32
- Converts an argument to a System.UInt32 value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.UInt32
- Converts an argument to a System.UInt32 value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Compound argument
- Property type: System.UInt64
- Converts an argument to a System.UInt64 value.
- Non-optional: Indicator
- Optional: Aliases, AcceptableValues, CaseSensitive, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
- Value argument
- Property type: System.UInt64
- Converts an argument to a System.UInt64 value.
- Non-optional: Ordinal
- Optional: AcceptableValues, Description, Groups, Optional, Ordinal, RangeMax, RangeMin, ShortName
Named parameters are what are passed into the constructor of the attribute. The following are the supported named parameters. Note that not all named parameters are supported by all attributes.
An array of the only values that the argument can be. By default all values are acceptable.
An array of identifiers that can refer to a switch or compound argument in addition to it its formal identifier. By default there are no aliases.
Description of the argument. Used in generating help. Empty string by default.
Setting to True will make the Indicators on switch and compound arguments case sensitive. If False they will not be case sensitive. Defaults to False.
Used to make arguments mutually exclusive. Array of integers that represent groups that the argument can belong to. A group can be designated by any integer. If zero is specified, the argument belongs to all groups. If no group is specified the argument is assigned zero for a group and belongs to all groups. Arguments can belong to multiple groups. Arguments in the same group are allowed to be specified together on the command line. Arguments NOT in the same group are not allowed to be specified together on the command line.
Non-optional arguments are mandatory within their assigned group. So, if an argument is mandatory and in group 1, but arguments for group 2 were passed on the command line, no exceptions would be thrown.
Defaults to zero.
Example:
- -a (group 1)
- -b (group 2)
- -c (group 1)
- -d (no group specified)
Allowed: app.exe -a -c -d
Not allowed (because -a and -b belong to different groups): app.exe -a -b -d
Used to identify a switch or compound argument. Must be unique.
The file or directory must exist on the file system. Defaults to False.
Marks an argument as optional. Switch arguments can be optional without restrictions. An optional value argument cannot be followed by any required parameters unless they are separated by a switch argument. In this case the switch argument must have an ordinal.
Non-optional arguments are mandatory within their assigned group. So, if an argument is mandatory and in group 1, but arguments for group 2 were passed on the command line, no exceptions would be thrown.
Defaults to False.
Legal Example:
- value1 (Ordinal = 1, Optional = false)
- value2 (Ordinal = 2, Optional = true)
- value3 (Ordinal = 3, Optional = true)
- -a (Ordinal = 4, Optional = false)
- value4 (Ordinal = 5, Optional = false)
Illegal Example:
- value1 (Ordinal = 1, Optional = false)
- value2 (Ordinal = 2, Optional = true)
- value3 (Ordinal = 3, Optional = false)
Specified the order that the argument should appear on the command line. Zero or less is any order. Greater than zero means the argument should appear in that order (ascending). If no ordinal is specified, the default is zero. Multiple switch or compound arguments can have the same ordinal. Those with the same ordinal can be in any order within that ordinal number. Only one argument (switch or value) can have a specific ordinal if that ordinal is assigned to a value argument. Defaults to zero.
The maximum (inclusive) allowed value of a value argument. Defaults to system minimum value for that type.
The minimum (inclusive) allowed value of a value argument. Defaults to system maximum value for that type.
Reverses the final value of a switch. Normally, if a switch is found on the command line, it's corresponding property is set to true. If Reverse is set to true, when the switch is found on the command line, the corresponding property is set to false. For best results, be sure to set the property to true in the containing class's constructor.
A short descriptor for the argument. Used in generating help. Defaults to the Indicator for switches and compounds. For values it defaults to the name of the property.
Setting to True will make the values on EnumValue and EnumCompound arguments case sensitive. If False they will not be case sensitive. Defaults to False.
CommandLineLib can automatically generate command line usage help text. Make sure that the arguments have at least the Description named parameter set. For best results, set the ShortName named parameter for value arguments, as well (switch and compound arguments default to using their Identifier).
The help text is generated by calling the instance method CommandLine.Help(). The generated text can then be written to the Console or elsewhere.
There are three overrides. All automatically retrieved values are done so by using Assembly.GetEntryAssembly().
string Help()
The assembly file name and version are automatically retrieved.string Help( string assemblyFileName )
This takes the assembly file name, while the assembly version is automatically retrieved.string Help( string assemblyFileName, Version assemblyVersion )
The third takes the assembly file name and the assembly version.
See the example code below.
Class that must be given the class that contains properties decorated with argument attributes as a type parameter in its constructor. If construction is complete, then call Parse() and pass in the raw command line arguments that were passed into main().
Thrown when a required value argument is directly followed by an optional value argument.
Thrown when a value cannot be formatted to its specified type.
Thrown when a mandatory argument was not detected.
Thrown when arguments are passed out of order as specified by their Ordinals.
Thrown when the type expected by an argument attribute does not match the type specified for its property.
Thrown when an identifier or alias is not correctly formatted or contains invalid characters.
Thrown when the value part of a compound argument is missing.
Thrown when the same argument is present more than once in the command line arguments.
Thrown when the identifier or aliases of an argument are not unique.
Thrown when multiple arguments are declared with the same ordinal.
Thrown when a group not that is not allowed due to previous group(s) being present is in the command line arguments.
Thrown when a member that is not a property is decorated with a command line argument attribute.
Thrown when no command line attributes are found on the class passed as the type parameter to the CommandLine constructor.
Thrown when multiple arguments have the same identifier or alias.
Thrown when an unknown command line attribute type is encountered.
Thrown when a value is not found in the acceptable values for an argument.
Thrown when a value is found to be out of the specified range for an argument.
using CommandLineLib;
public class Arguments
{
[Int32Value( 1, RangeMin = 0, RangeMax = 100 )]
public Int32 Value1
{
get;
private set;
}
[Switch( "-foo" )]
public bool Foo
{
get;
private set;
}
[StringCompound( "-bar" )]
public string Compound1
{
get;
private set;
}
}
public class Program
{
static void main( string[] args )
{
CommandLine commandLine = null;
try
{
commandLine = new CommandLine<Arguments>();
var arguments = commandLine.Parse( args );
}
catch ( CommandLineException ex )
{
System.Diagnostics.Trace.WriteLine( ex );
if ( commandLine != null )
{
Console.WriteLine( commandLine.Help() );
}
}
}
}