Command-line-api: Mutually exclusive options

Created on 21 Jan 2019  路  11Comments  路  Source: dotnet/command-line-api

It would be great to be able to handle mutually exclusive options.

Example

For example, if I had a command that either took an --all option or a --tenant <tenant-id> option, but never both.

public static Command Rebuild(IContainer container)
{
    var rebuild = new Command("rebuild", "Rebuilds a search index for one (--tenant <tenant-id>) or all (--all) tenants.")
    {
        Handler = CommandHandler.Create<bool, Guid>((all, tenant) =>
            container.Resolve<SearchRebuildTask>().ExecuteAsync(all, tenant))
    };

    rebuild.AddOption(new Option(new[] {"--all", "-a"},
        "Indicates if all tenants should be rebuilt.",
        new Argument<bool>(false) {Arity = ArgumentArity.ZeroOrOne}));

    rebuild.AddOption(new Option(new[] {"--tenant", "-t"},
        "Specifies the specific tenant that should be rebuilt.",
        new Argument<Guid> {Arity = ArgumentArity.ZeroOrOne}));

    return rebuild;
}
Area-Parser and Model Binder enhancement help wanted

Most helpful comment

Sorry, I was not clear with my comment. It is regarding tab suggestions.
For example, if I use the arg --label-column-name then since --label-column-index is mutually exclusive, the tab suggestion shouldn't show --label-column-index but the next argument.

I think we're good if that feature related to tab suggestions is moving forward. You are right that this is not a blocking issue but improving the user's experience. 馃憤

All 11 comments

This can currently be done using a custom validator, although it's a bit awkward.

https://github.com/dotnet/command-line-api/blob/master/src/System.CommandLine.Tests/ParsingValidationTests.cs#L104

Suggestions on a nicer API for this feature?

@jonsequitur As per our discussion earlier.
It would also be great to have these changes cascade to dotnet-suggest.
i.e
The auto completion should not suggest the mutually excusive options when one of them is set.

Should we track this in separate issue or should we include it in this issue itself?

I think it's fine to track it here.

cc : @CESARDELATORRE @vinodshanbhag @rjoshi

@jonsequitur
Jon, what's the priority for this feature?
It'd be very good for the ML.NET CLI due to parameters like the following which are exclusive:

--label-column-name
|
--label-column-index

@CESARDELATORRE, this capability is supported right now but the API is awkward. Improving the API will likely be done along with improvements to enable #310, and it's fairly high priority but I don't have a timeline for it. I don't believe this should block you.

@srsaggam, my understanding is that you have the validation working but tab suggestions don't reflect the exclusivity. Is that correct?

@jonsequitur That is correct. My comment is regarding tab suggestions. I think @CESARDELATORRE is referring to the same. We are not blocked by the functionality but we were thinking the intelligent suggestions for this case will be a good feature to demo and will help our users to not make mistakes.

Sorry, I was not clear with my comment. It is regarding tab suggestions.
For example, if I use the arg --label-column-name then since --label-column-index is mutually exclusive, the tab suggestion shouldn't show --label-column-index but the next argument.

I think we're good if that feature related to tab suggestions is moving forward. You are right that this is not a blocking issue but improving the user's experience. 馃憤

Also for this. Lets say we have options like : --abc --abze --dab --dbc

when we type --ab[->TAB] the suggestions should be abc on first tab and abze on the second tab and this should repeat it shouldn't suggest anything else. kind of prefix tree based suggestions then sorting ascending order

but looks like the current dotnet suggest doesn't do this kind of behavior it doesn't seem to respect the prefix that is already typed. it just suggests in the alphabetical order on whole set rather than filtered prefix.

@jonsequitur @CESARDELATORRE

@srsaggam This deserves a separate issue. If you type ab and hit tab do you see the expected behavior?

The link to a custom validator above is no longer valid. Here is a valid one

Was this page helpful?
0 / 5 - 0 ratings

Related issues

erichiller picture erichiller  路  3Comments

SirJosh3917 picture SirJosh3917  路  3Comments

jonsequitur picture jonsequitur  路  6Comments

ibigbug picture ibigbug  路  3Comments

divinebovine picture divinebovine  路  3Comments