Understand Syntax

There are general rules of parsing regardless of the options that your code will manage. This library subdivides the command line arguments in two main categories, options and values.

  • Options are used to indicate the values. These can be grouped together. They may have a short name, a long name or both.
  • Values are tied with options that support them and/or are those left out of the mapping.

As an example, if your program accepts the following options:

  • an input filename; short name = "i"; long name = "infile"; mandatory
  • report details; short name = "v"; no long name; not mandatory

You can type these combinations at the command prompt:

Console:

yourapp -idata.bin

yourapp --infile=data.bin -v

yourapp -vi data.bin


As you can see you can:

  • specify an option with its short name if preceded by single dash; you can omit the space before the value
-s{SPACE}[VALUE]
-s[VALUE]
-s
  • specify an option with its long name if preceded by two dash; you can use; you can separate the value with a space or the equal sign
--long{SPACE}[VALUE]
--long=[VALUE]
--long
  • specify a group of optios with their short name if preceded by single dash; if the rightmost option accepts a value, you can assign it to the option
-xyzs{SPACE}[VALUE]
-xyzs[VALUE]
-xyz


You can read a more schematic version of these rules here: Input Specification.

Attributes Overview

Attributes (System.Attribute) are the declarative syntax with which you define the parsing rules of your command line options. These types are classes derived from CommandLine.BaseOptionAttribute.

C#:

namespace CommandLine
{
    public abstract class BaseOptionAttribute : System.Attribute
    {
        public string ShortName { get; internal set; }       

        public string LongName { get; internal set; }

        public string HelpText { get; set; }

        public virtual bool Required { get; set; }
    }
}

A public property associated with an attribute receives the value of the command line argument. If not explicitly set, each option is not mandatory, otherwise set Required to true using the attribute's constructor. The HelpText property is used with text formatting types found in CommandLine.Text namespace.

Last edited May 15, 2012 at 9:11 AM by gsscoder, version 13

Comments

Nautious Mar 17, 2015 at 8:56 AM 
@gemasp: Do this:

[Option('s', "sourcedate", Required = false,
HelpText = "Source Data date used to source various inputs into PnL Automation.")]
public string ParserDateString
{
get { return SourceDataDate == null ? null : SourceDataDate.ToString(); }
set
{
DateTime sdd;
if (DateTime.TryParse(value, out sdd))
SourceDataDate = sdd;
}
}
public DateTime? SourceDataDate { get; set; }



Input:
-t "28/01/2015 01:23"
-t "28/01/2015"

works for me.

gemasp Jan 29, 2015 at 8:59 AM 
Does it support passing dates as arguments?

[Option('s', "sourcedate", Required = false,
HelpText = "Source Data date used to source various inputs into PnL Automation.")]
public DateTime? SourceDataDate { get; set; }

tried to pass -s 28/01/2015 and -s "28/01/2015" - doesnt work!