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

Exclusive or relationship #629

Closed
jjzieve opened this issue May 15, 2020 · 11 comments
Closed

Exclusive or relationship #629

jjzieve opened this issue May 15, 2020 · 11 comments

Comments

@jjzieve
Copy link

jjzieve commented May 15, 2020

With the release of 2.8.0. I see that #575
changed it so you can't use Groups and SetNames together. Is there a reason that was removed?

Using both together achieved an XOR relationship that was useful. If say, I want at least one option in a group to be required but you can't use the other options in that group at the same time.

@moh-hassan
Copy link
Collaborator

Can you provide example of use case

@jjzieve
Copy link
Author

jjzieve commented May 19, 2020

The Group example (https://github.com/commandlineparser/commandline/wiki/Option-Groups) could be one. Just imagine a world where I want to validate that the user can only --append a prefix or a suffix but not both.

@jjzieve jjzieve closed this as completed May 19, 2020
@jjzieve jjzieve reopened this May 19, 2020
@moh-hassan
Copy link
Collaborator

I mean case where both group and exclusive or for the same option. Both are not applicable for the same option.

@jjzieve
Copy link
Author

jjzieve commented May 19, 2020

Group achieves the "Required" part whereas I don't see how to use "SetName" (i.e. Mutually Exclusive Options") to say "hey at least one of these sets are required". If there's a way to achieve this XOR relationship without using both Group and SetName together I can do that but I don't know of a way (other than implementing it myself)? I don't fully understand why Group and SetName shouldn't be used together?

@moh-hassan
Copy link
Collaborator

Just show me example to show how both can be applied without contradiction.

@moh-hassan
Copy link
Collaborator

Cc @hadzhiyski
Can you help in this feature?

@moh-hassan
Copy link
Collaborator

moh-hassan commented May 20, 2020

If the options of group are contained in different setname, it will raise error.
If group is contained in the same setname, it has no meaning.

@jjzieve
Copy link
Author

jjzieve commented May 20, 2020

If the options of group are contained in different setname, it will raise error.

It did not raise an error prior to 2.8, that's why I opened this issue.

If group is contained in the same setname, it has no meaning.

It would be different setnames to achieve the XOR functionality.

So for the append example it would look like this:

class AppendToFileNameOptions
{
    //define commands in set(group) named fetch
  [Option(SetName = "prefix", Group = "append", HelpText="Prefix to append to file name")]
  public string Prefix { get; set; }

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

  [Option("source", HelpText="The path of a file to rename")]
  public string FilePath { get; set; }
}

And you could do:
app --prefix foo --source bar
or
app --suffix foo --source bar

But not:
app --source bar
You get this error:

ERROR(S):
  At least one option from group 'append' (prefix, suffix) is required.

or
app --suffix foo --prefix foo --source bar
You get this error:

ERROR(S):
Option: 'prefix' is not compatible with: 'suffix'.
Option: 'suffix' is not compatible with: 'prefix'.

Does that make sense?

@moh-hassan
Copy link
Collaborator

moh-hassan commented May 20, 2020

You can remove group and set the option Required in both sets and apply the same logic.
Let me know if this solution is reasonable.
In wiki page it is stated that both group and exclusive option are not allowed.
The change in 2.8 is taking default value into account for group.
The logic behind this constraint is:
In group, at least one option is required.
In Exclsive sets only one set is allowed.

@jjzieve
Copy link
Author

jjzieve commented May 20, 2020

Oh you're right, it seems setting Required = true without the group works to achieve XOR. I thought it didn't before but must've been my mistake. Thank you!

@jjzieve jjzieve closed this as completed May 20, 2020
@moh-hassan
Copy link
Collaborator

Glad that you resolved your issue. 👍

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

2 participants