3

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 strings.

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.

1 Answer 1

4

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I overlooked this one. After changing / to -- it indeed parses the input correctly, but the problem is that i need to percede arguments with / - this is my specification. IK i could solve this dirty by replacing slashes with double dashes in arguments string, but it just doesn't seem right... Any ideas? The library doesn't seem to allow perceding character configuration. Of course all remaining issues still exist and I would be grateful for help.
The library seems fairly limited - unfortunately it looks like there is no way to do that without manually converting / to -- like you said. I edited my answer with one way that you can do that. Sorry that there doesn't seem to be a better solution.
Yea. That's the only possible solution to / problem I guess. Any idea about nulls to disable short options? Also I guess that for list of maturities to be list of doubles, not strings i need to create another object containing desired list (of doubles) and convert former one to the later? Any easy way to incorporate polymorphism or something like that in here or should i simply create another class repeathing every property except the list which will be created ouf of different types?
Note that Microsoft now has System.CommandLine for .NET 6.0 and above. Find it here: learn.microsoft.com/en-us/dotnet/standard/commandline

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.