Reputation: 31
I am using CommandLine library v 1.9.71.2 from nuget. Unfortunately documentation is not up to date and I don't understand advanced C# language constructions used for this library, so I couldn't come up with working solution just by watching interface available in library.
My options class looks like this:
class ProgramOptions
{
[Option('l', "spotsL", Required = true, HelpText = "Lowest stock price used for calculations.")]
public double lowestSpot { get; set; }
[Option('h', "spotsH", Required = true, HelpText = "Highest stock price used for calculations.")]
public double highestSpot { get; set; }
[Option('n', "spotsN", Required = true, HelpText = "Number of (equally distributed) stock prices [spotsL,spotsH].")]
public int spotsNumber { get; set; }
[OptionList('m', "maturities", ',', Required = true, HelpText = "Comma separated list of options maturities (as a fraction of a year).")]
public IList<string> maturities { get; set; } //we want double here.
[Option('s', "strike", Required = true, HelpText = "Strike price of options.")]
public double strikePrice { get; set; }
[Option('v', "vol", Required = true, HelpText = "Volatility used for calculation.")]
public double volatility { get; set; }
}
I need only long names of the options, but when I put null
's (as documentation suggests) in place of short names I get compiler errors. I'd also prefer to have maturities as a list of doubles (IList<double> maturities
), but IDK how to achieve this - i think with pure CommandLine it works only for list of string
s.
Second part is that I can't parse options from command line args
to the ProgramOptions
object. Was trying something like:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args[1]);
///Parse Command Line
var options = new ProgramOptions();
bool is_succes = Parser.Default.ParseArguments(args, options);
Console.WriteLine("parsed? {0}",is_succes);
Console.WriteLine(options.highestSpot);
Console.WriteLine(options.lowestSpot);
Console.WriteLine(options.maturities.ToString());
Console.WriteLine(options.spotsNumber);
Console.WriteLine(options.strikePrice);
Console.WriteLine(options.volatility);
Console.WriteLine("program working");
}
}
Unfortunately it doesn't work and gives
False
in is_succes
variable. All other variables display 0
.My command line arguments were Any chance to get the library to parse something like:
/spotsL 10 /spotsH 1000 /spotsN 9 /maturities 1,0.5,0.001 /strike 495 /vol 0.1
and 10
is indeed displayed by te first WriteLine
.
Upvotes: 3
Views: 4279
Reputation: 6830
I'm not too familiar with the CommandLine library, but from a quick look at the documentation, it looks like you need to use a single dash (-
) for single-character arguments or a double dash (--
) for multiple-character arguments, i.e.
--spotsL 10 --spotsH 1000 --spotsN 9 --maturities 1,0.5,0.001 --strike 495 --vol 0.1
Edit: if you really need the input arguments to have slashes, you need to convert them to dashes:
public static void FormatArguments(string[] args)
{
for (int i = 0; i < args.Length; i++)
if (args[i].StartsWith("/"))
args[i] = "--" + args[i].Substring(1);
}
Upvotes: 4