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 #76

Open
st1led opened this issue Apr 9, 2013 · 9 comments
Open

Specifying that an Option requires another Option #76

st1led opened this issue Apr 9, 2013 · 9 comments

Comments

@st1led
Copy link

st1led commented Apr 9, 2013

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

@gsscoder
Copy link
Owner

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

@st1led
Copy link
Author

st1led commented Apr 10, 2013

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

@nemec
Copy link
Collaborator

nemec commented Apr 10, 2013

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?

@st1led
Copy link
Author

st1led commented Apr 10, 2013

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

@nemec
Copy link
Collaborator

nemec commented Apr 10, 2013

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.

@st1led
Copy link
Author

st1led commented Apr 10, 2013

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

@nemec
Copy link
Collaborator

nemec commented Apr 10, 2013

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; }

@shengqh
Copy link

shengqh commented Apr 19, 2013

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

@alexanderfast
Copy link
Collaborator

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?

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

5 participants