Skip to content
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

Specifying that an Option requires another Option #3

Closed
ericnewton76 opened this issue Nov 4, 2017 · 11 comments
Closed

Specifying that an Option requires another Option #3

ericnewton76 opened this issue Nov 4, 2017 · 11 comments

Comments

@ericnewton76
Copy link
Member

Issue by st1led
Tuesday Apr 09, 2013 at 09:55 GMT
Originally opened as gsscoder/commandline#76


Hi everybody, is there a way in CommandLine (I guess in a way similar to what is done with mutually exclusive properties) to specify that an option requires another option to be set? This is a short example:

        [Option('v', "verbose",
            Required = false,
            DefaultValue = false,
            HelpText = "Prints log messages.")]
        public bool Verbose { get; set; }

        [Option('o', "optional-operation",
            Required = false,
            DefaultValue = false,
            HelpText = "Performs an operation (a.k.a. \"does something\") in the code.")]
        public bool PerformOperation { get; set; }

        [Option('t', "time-limit",
            Required = false,
            DefaultValue = false,
            HelpText = "Requires option -o. Specifies a time limit for the optional operation")]
        public int OperationTimeLimit { get; set; }

In this scenario, one would want to allow command line arguments like -o and -vot but not -vt or -t for instance, since in the latter the option -t is specified without -o. Is this currently possible in CommandLine?

Regards,
Stefano

@ericnewton76
Copy link
Member Author

Comment by gsscoder
Wednesday Apr 10, 2013 at 06:46 GMT


Hi, @st1led,
I'm sorry but I'm not sure to understand what you want to get...

Can you be so kind to reformulate your question telling _what you're not able to get and than explaining what you want to get?

Regards, Giacomo

@ericnewton76
Copy link
Member Author

Comment by st1led
Wednesday Apr 10, 2013 at 08:03 GMT


Thank you for your support, I'll try to reformulate the question in a clearer way. I would like to know if CommandLine offers some API for a behavior like the following one:

        [Option('v', "verbose",
            Required = false,
            DefaultValue = false,
            HelpText = "Prints log messages.")]
        public bool Verbose { get; set; }

        [Option('o', "optional-operation",
            Required = false,
            DefaultValue = false,
            HelpText = "Performs an operation (a.k.a. \"does something\") in the code.")]
        public bool PerformOperation { get; set; }

        [Option('t', "time-limit",
            Required = false,
            RequiresOptions = {"optional-operation"},
            DefaultValue = false,
            HelpText = "Requires option -o. Specifies a time limit for the optional operation")]
        public int OperationTimeLimit { get; set; }

Note that in this example I defined option -t with an hypothetical parameter RequiresOptions, specifying that calling the program with the argument -t without having set the argument -o leads to a parsing error. This is what I meant in the first post as "Specifying that an Option requires another Option".

To achieve this behavior, I have to manually write some code in my program to check if arguments have been correctly set. Since I can't find any way to do it in CommandLine, I'm asking if such feature exists, or is planned for future versions and if not... Would it be a good idea for future CommandLine versions? To me having dependencies between arguments sounds like a pretty common scenario.

Regards,
Stefano

@ericnewton76
Copy link
Member Author

Comment by nemec
Wednesday Apr 10, 2013 at 16:17 GMT


I think that it's a good idea. I'm not sure what kind of syntax you were going for with {"other-stuff"}, but it could be made into an array of strings.

A couple potential issues: What if the option you depend on doesn't have a long option? If an option defines both a short and long option, which do you use?

@ericnewton76
Copy link
Member Author

Comment by st1led
Wednesday Apr 10, 2013 at 16:34 GMT


The syntax was just an attempt to specify an array of identifiers representing the options (just o in the example) that current option (t in the example) depends on.

Since an Option can specify either a long or a short name, there would be the need to add a new parameter (named Identifier), unique for each Option. In this way RequiredOptions would be an array of the same type as Identifier.

I guess Identifier could be a simple integer, or a string, or maybe a more complicated object encapsulating the long and short name of an Option.

Regards,
Stefano

@ericnewton76
Copy link
Member Author

Comment by nemec
Wednesday Apr 10, 2013 at 20:37 GMT


I'm inclined to recommend using the property name itself (e.g. PerformOperation). If you define an option without either a short or long identifier, the parameter name is determined using reflection already.

@ericnewton76
Copy link
Member Author

Comment by st1led
Wednesday Apr 10, 2013 at 21:03 GMT


Using the property name sounds like a brilliant idea to me. This way the RequiresOptions parameter can be defined as an array of strings, each being the property name of the required option. No need to add further parameters, then.

Regards,
Stefano

@ericnewton76
Copy link
Member Author

Comment by nemec
Wednesday Apr 10, 2013 at 21:16 GMT


If only delegates were allowed as arguments to an attribute... you could do so much with strong typing.

[DependsOn<Config>(c => c.PerformOperation)]
public int OperationTimeLimit { get; set; }

@ericnewton76
Copy link
Member Author

Comment by shengqh
Friday Apr 19, 2013 at 14:54 GMT


Is there any news about this feature? I met such problem too.

@ericnewton76
Copy link
Member Author

Comment by Mizipzor
Thursday Jan 28, 2016 at 12:04 GMT


To me it makes more sense allowing a time-limit to be set, even if the optional-operation is not set. The time limit would then just be ignored by the program. And if you want to enforce this dependency it's a trivial check to do after parsing. I don't think the increased parsing complexity is worth it to provide this functionality built in.

Can we find some other program that behaves this way, with argument dependencies?

@IanKemp
Copy link

IanKemp commented Feb 14, 2023

So... how do we do this? I can't see anything in the wiki that indicates that this feature is available.

@rmeshksar
Copy link

Why is this issue closed? Is there anyway to do it now that is not documented?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants