-
Notifications
You must be signed in to change notification settings - Fork 293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support For Dynamic Options #96
Comments
No, I don't believe it's possible with this library. How "dynamic" are you talking? Do you accept a completely different "set" of parameters based on a config value? If so, maybe you could create one Options class per config value and conditionally load it. Or are there "flags" in the config that enable or disable a specific parameter? If so, it may be better to create one "master" Options class and just ignore any extraneous parameters. |
The application will be used as a framework. So there could be hundreds to thousands of configurations. I could not realistically create an Options class per config. A configuration file will allow a user to add/remove a set of values that get loaded up at run-time with values for "key" and "description". The values in the configuration file need to be able to be "overridable" by passing in values through the command line arguments. In order to validate the arguments passed in, I was thinking of using this library. But in order to do so, I need a valid Options class. That is why it needs to be dynamic. Any possible workarounds or a possible enhancement for the future? |
If the keys are dynamic, would you be trying to parse the values as I'm not sure if you could reuse the parser to do what you want without a
|
They are essentially a collection of strings. Much like a dictionary. I wanted to use the parser to validate that all arguments were passed through since I do not want to have to worry about the logic for parsing it myself. |
Nope, I don't think that's possible here.
|
Perhaps at a simpler level, would it perhaps be feasible to make a method that parses the arguments into a dictionary of elements? That would provide the value of the library without any strongly typed requirements. |
Well. Just passing by. You can actually do this without requiring features from the library.
|
That compiling at run-time example seemed strange. You have to basically generate a text file, then programmatically invoke the compiler at run-time, and finally Assembly.Load on the new assembly. This would have to be done on each run. It seems like it isn't an elegant solution. I previously tried using an However, I still believe that the extra functionality I defined up above could have some use cases. I'm thinking the method call could be something like
That allows the library to do the parsing and the user to do whatever they deem appropriate with the return value. |
Glad to help. And FYI, you don't need to compile each time. It gives you an option to generate a .dll file on disk. |
You can use Roslyn too if you want to avoid creating and compiling new files. So basically what you're looking for is taking an argument string like "--name team --profession oranges --tagline go team" and turn it into three key-value pairs? I would think the effort needed to rework commandline to do that isn't worth the cost of just writing a parser. Something like this would work for -- arguments:
|
At a very basic level, yes I agree. I thought some of the value from this library was handling all the logic for parsing from a command line. Maybe I am confused, but I thought a user didn't have to use dashes, or they could use dashes, or they could do something like -keyValue with no space in between. All of the logic for the actual parsing I did not want to recreate as much time has been spent on it already for this library. But if you recommend just doing a simple split and looping through the values, then perhaps that is a route to take. Roslyn seems interesting as well. I will try to see if I can dynamically create a Options class using Roslyn. In totality, there are workarounds to get to the end goal. Perhaps I was just looking for some flexibility. This isn't a high priority, but something to consider. |
By "not using dashes" you're referring to positional arguments, right? How would those get parsed into a dictionary since they have no key? You're right about the others, those would have to be coded too. The biggest issue here is that all of this parsing is predicated upon having a spec of what is/is not a valid parameter name. You may be able to hook into the Tokenizer and TokenPartitioner to get the behavior you want (although you will probably need to join the Name and Value tokens together yourself). |
I just think there are many possible variations, like so: Some of these examples could be invalid input, but that just goes to show that the parsing logic should not be something the consumer (developer) should be trying to re-create. If the spec is required for the parser, then I do believe there should be a way to define a spec at run-time. That has really been the scope of the conversation. I fully understand that you need a spec in order for the parser to behave and operate correctly, but requiring a class at design time seems like a limitation in my eyes. |
Oh! I think in all of this discussion about Dictionaries and "dynamic" I forgot that you have a spec, it's just that the spec isn't known until runtime. We've had a discussion before about adding a fluent builder to the library that would do exactly that. We had some ideas, but I don't think anything was ever implemented. |
Yes I think we are on the same page now. There is a spec, but because it is configurable, the spec is not known until run-time. A Fluent Builder sounds interesting. I always thought of fluent libraries as extra. I believe out of the box, the minimal functionality would be to just allow the option. Then fluent would be the next step. So perhaps an overload for the
There is already an What do you think? |
Yeah, you're right. You'd probably want to extract most of the stuff in OptionAttribute into an interface that both OptionAttribute and Option will implement so that they'd be interchangeable when it comes to parsing. |
@issafram Would you be willing to design a "superclass" that contains all possible arguments for any config value? I created my own command line parsing library a while ago in the style of Python's
If you try to use |
@nemec the problem with that is you are assuming that I actually know the combination options at design time. We need to make it a little bit more flexible (my opinion). I'm thinking you could perhaps do something similar to an |
So you could do that to configure the parser, but how will you specify that the parsed output should be a Also, my plan doesn't assume you know the combination at design time, it requires that you know the union of all possible configurations. For example, if config setting A adds a If this still doesn't fix your issue, maybe it would be helpful if you gave a very specific scenario that you want covered. |
Let us pretend that there is a file called input.txt
Notice how as the developer I have no clue what is in the file input.txt This is what I had in my mind. Keep in mind that this is just a high level prototype and the constructors/classes/design will need to be thought out more. What do you think? |
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. |
So, uh, it's possible but very brittle. If you stick to To use it:
This is what you were looking for, right? |
That branch does not exist. Also, how is CliParser related to the CommandLine library? Are they different code bases? |
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. |
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. |
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.
The text was updated successfully, but these errors were encountered: