commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Support For Dynamic Options

Open issafram opened this issue 13 years ago • 25 comments

I have an application that will have 1 to many different arguments being passed in. The argument names will always be different depending on configuration (dynamic). So I will never know the name of the argument names as the designer of the application.

Does this library support something like that? It is great for parsing but it requires the options parameter to be defined. I have tried using an ExpandoObject but I don't think it works properly because attributes are required on the properties.

issafram avatar Aug 28 '13 14:08 issafram

Ah, so the keys can potentially be any string at all? Makes sense. I'll take a look sometime and see if it's feasible to factor out the PropertyInfo into an interface so that I can build a backend for dict indexers...

Although the library still wants to know what type to convert the values into, so it'a pretty limited in what the output can be (all values will need to be of the same type). Not a problem for you, since you're using strings though.

nemec avatar Sep 02 '13 06:09 nemec

So, uh, it's possible but very brittle. If you stick to Dictionary<string, string> parsing you shouldn't have any problems though. To use it, check out the code on my dictbackend branch.

To use it:

var opt = new Dictionary<string, string>();
var parser = new CliParser<Dictionary<string, string>>(opt);
parser.HasNamedArgument(c => c["name"])
        .WithShortName('n');

parser.Parse("-n frank".Split());

Console.WriteLine("Parsed Keys:");
foreach (var kv in opt)
{
    Console.WriteLine("\t{0}: {1}", kv.Key, kv.Value);
}
// Parsed Keys:
//    name: frank

This is what you were looking for, right?

nemec avatar Sep 02 '13 09:09 nemec

That branch does not exist. Also, how is CliParser related to the CommandLine library? Are they different code bases?

issafram avatar Sep 15 '13 02:09 issafram

Oh yeah I merged it into master a week ago, so I deleted the branch. They're not related, other than the fact that they're both command line parsing libraries. I wanted to experiment with a slightly different configuration syntax than CommandLine, so I wrote my own.

nemec avatar Sep 15 '13 03:09 nemec

Perhaps that is my confusion? I put this issue in for CommandLine. I am glad that you put in an enhancement in for your Clipr codebase though. I am sure that I can check it out via NuGet at some point.

issafram avatar Sep 15 '13 22:09 issafram