commandlineparser / commandline

The best C# command line parser that brings standardized *nix getopt style, for .NET. Includes F# support
MIT License
4.46k stars 473 forks source link

Parsing key-value pairs #19

Open ericnewton76 opened 6 years ago

ericnewton76 commented 6 years ago

Issue by mr-miles Wednesday Mar 26, 2014 at 18:36 GMT Originally opened as https://github.com/gsscoder/commandline/issues/122


Is it possible to use the parser to populate key-value pairs?

So something like "myapp.exe -v:a=x -v:b=y -v:c=z" would populate a property v which is a Dictionary<string, string> with (a,x), (b,y), (c,z) values?

I want to use the parser to specify replacements to be made on a text file

Great package by the way!

ericnewton76 commented 6 years ago

Comment by alastairtree Sunday May 31, 2015 at 12:43 GMT


I have too been looking for this feature. Anyone find/build anything?

ericnewton76 commented 6 years ago

Comment by gsscoder Saturday Jun 06, 2015 at 17:15 GMT


Happy you like it... I'm recently back to work on the project.

When 2.0 swirch to beta, a lot is planned. I was thinking to something similar too...

Maybe adding a form of post-value processing.

Regards.

Il domenica 31 maggio 2015, alastairtree notifications@github.com ha scritto:

I have too been looking for this feature. Anyone find/build anything?

— Reply to this email directly or view it on GitHub https://github.com/gsscoder/commandline/issues/122#issuecomment-107173091 .

Giacomo Stelluti Scala _Information Technology _Consultant

ericnewton76 commented 6 years ago

Comment by TheFanatr Saturday Oct 21, 2017 at 01:42 GMT


Any update on this?

DominikWiesend commented 5 years ago

I'm really waiting for this.

sybaris commented 5 years ago

Hello

Waiting this feature, I think I will use something like the following code.

It's ok for key1=value1 key2="value2" but not for key3="foo""bar"

Do you have something better to propose ?

Thanks for advance. Sybaris

  ```

[Option("Dico", HelpText = "Test")] public IEnumerable Dico { get; set; }

    public Dictionary<string, string> GetDico()
    {
        const string KEY_VALUE1 = @"(\w+)\=(\w+)";
        const string KEY_VALUE2 = @"(\w+)\=\""([.|\""]+)\""";

        Dictionary<string, string> result = new Dictionary<string, string>();
        foreach (var item in Dico)
        {
            bool matchKeyValue1 = Regex.IsMatch(item,KEY_VALUE1);
            bool matchKeyValue2 = Regex.IsMatch(item, KEY_VALUE2);
            if (!(matchKeyValue1 || matchKeyValue1))
                throw new Exception($"Parameter {item} does not match the requirements.");
            string regexElected = matchKeyValue2 ? KEY_VALUE2 : matchKeyValue1 ? KEY_VALUE1 : "";
            Debug.Assert(!string.IsNullOrEmpty(regexElected));
            string key = Regex.Match(item, regexElected).Groups[1].Value;
            string value = Regex.Match(item, regexElected).Groups[2].Value;
            result.Add(key, value);
        }
        return result;
    }
AceCoderLaura commented 2 years ago

Definitely a feature worth having. I'm currently using environment variables for key-value pairs. It's a bit yuck but it works for my scenario. Would be good to do it properly with this library at some stage.

carlo-setldpay commented 1 month ago

Is there any update on this?