0

I downloaded this package https://github.com/commandlineparser/commandline and I wanted to perform parsing for strings like

string str = "file:xxxx\\xxxx\\xxxxx.sh val:-a nsdd m";

so

file = xxxx\\xxxx\\xxxxx.sh
val = -a nsdd m

I wanted to know if anyone had a library in mind or has used the specified library to obtain the parameters specified in the string. I am having a hard time understanding the example on how to parse that string and obtain the file parameter and val parameter. I know i could do string manipulation but I rather use an existing tested durable solution for this.

1
  • Why does val have all those things following it rather than splitting it on space and having nsdd and m as their own properties? Commented Oct 12, 2019 at 0:12

1 Answer 1

3

I've used this library and it's a solid choice.

Here's a very basic sample using some of what you posted, see code comments for clarification.

class Program
{
    static void Main(string[] args)
    {
        // args a space separated array so you should use an array for your test
        // args are identified with the `-` so you should set args like `-f somefilenamehere`
        // args specified are -f and -v
        string[] arguments = new[] {"-f file:xxxx\\xxxx\\xxxxx.sh", "-v nsdd" };
        string file = string.Empty;
        string value = string.Empty;

        // you would pull your args off the options, if they are successfully parsed
        // and map them to your applications properties/settings
        Parser.Default.ParseArguments<Options>(arguments)
            .WithParsed<Options>(o =>
            {
                file = o.InputFile; // map InputFile arg to file property
                value = o.Value; // map Value arg to value property
            });


        Console.WriteLine($"file = {file}");
        Console.WriteLine($"value = {value}");
        Console.ReadLine();

        // output:
        // file =  file:xxxx\xxxx\xxxxx.sh
        // value =  nsdd

    }        
}

// the options class is used to define your arg tokens and map them to the Options property
class Options
{
    [Option('f', "file", Required = true, HelpText = "Input files to be processed.")]
    public string InputFile { get; set; }

    [Option('v', "value", Required = true, HelpText = "Value to be used")]
    public string Value { get; set; }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.