Skip to content

Could two or more options be marked as 'at least one is required'? #546

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

Closed
hadzhiyski opened this issue Dec 1, 2019 · 7 comments
Closed
Labels

Comments

@hadzhiyski
Copy link
Contributor

hadzhiyski commented Dec 1, 2019

Hello,

This is not particularly an issue. I was wondering could I make two or more options to act like at least one of them is required, but if none is provided I will get a required error?

[Verb("rename")]
public sealed class RenameOptions
{
    [Option("prefix", HelpText = "Prefix to append to file name", SetName = "rename", Required = true)]
    public string Prefix { get; set; }

    [Option("suffix", HelpText = "Suffix to append to file name", SetName = "rename", Required = true)]
    public string Suffix { get; set; }
}

This works if both options are included. Could I make them to work separately or together, but always one should be provided?

@moh-hassan
Copy link
Collaborator

You can use Mutually Exclusive Options.
Set every option in a separate SetName and only one option at a time is allowed.

[Verb("rename")]
public sealed class RenameOptions
{
    [Option("prefix", HelpText = "Prefix to append to file name", SetName = "prefix", Required = true)]
    public string Prefix { get; set; }

    [Option("suffix", HelpText = "Suffix to append to file name", SetName = "suffix", Required = true)]
    public string Suffix { get; set; }
}

This is working:

myapp  rename   --prefix aaa   //Ok, --suffix not allowed here
myapp  rename   --suffix bbb   //Ok, --prefix not allowed here

This is not working

myapp  rename    //Error:  MissingRequiredOptionError , at least one prefix/or suffix is needed
myapp  rename   --suffix bbb  --prefix aaa  //both options are not allowed, they are mutually Exclusive

For more details about Mutually Exclusive Options see wiki

@hadzhiyski
Copy link
Contributor Author

Hello,

I actually am trying to do the following

myapp rename --prefix aaa // OK
myapp rename --suffix bbb // OK
myapp rename --prefix aaa --suffix bbb // OK
my app rename // Required error - Either prefix or suffix must be available

I hope this makes sense.

@moh-hassan
Copy link
Collaborator

myapp rename --prefix aaa --suffix bbb // OK

it should not work.
Do you renamed the SetName as I showed above

@hadzhiyski
Copy link
Contributor Author

I removed the SetName completely. I am trying to make it work like the example from my previous comment

@hadzhiyski
Copy link
Contributor Author

I created a PR #552 which solves this issue. It is still "work in progress" so I am open to any suggestions about it.

@hadzhiyski
Copy link
Contributor Author

@moh-hassan please close this issue

@moh-hassan
Copy link
Collaborator

Thanks, closed

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

No branches or pull requests

2 participants