Commandline: Proposal for handling multiple mutual exclusive sets

Created on 14 Mar 2019  路  3Comments  路  Source: commandlineparser/commandline

As probably already discussed in #79 and #93 I would need to specify multiple mutual exclusive sets.

I would suggest to distinguish between multiple sets at an interface level.

For example I have a command that requires either a workspace name or a path to a workspace. The same command expects a connection name or user-host combination:

    interface IWorkspaceOptions
    {
        [Option(Required = true, SetName = "fromName")]
        string WorkspaceName { get; set; }

        [Option(Required = true, SetName = "Path")]
        string FromPath { get; set; }
    }

    interface IConnectionOptions
    {
        [Option(Required = true, SetName = "fromName")] // not in the same set as IWorkspaceOptions !
        string ConnectionName { get; set; }

        [Option(Required = true, SetName = "Params")]
        string Host { get; set; }

        [Option(SetName = "Params")]
        string User { get; set; }
    }

    class Opts : IWorkspaceOptions, IConnectionOptions
    {
        public string ConnectionName { get; set; }
        public string Host { get; set; }
        public string User { get; set; }
        public string WorkspaceName { get; set; }
        public string FromPath { get; set; }
    }

I would expect the following command lines to be valid:

--WorkspaceName MyWorkspace --ConnectionName MyConnection
--WorkspaceName MyWorkspace --Host 1.2.3.4
--WorkspaceName MyWorkspace --Host 1.2.3.4 --User Foo
--FromPath C:\TMP --ConnectionName MyConnection
--FromPath C:\TMP --Host 1.2.3.4
--FromPath C:\TMP --Host 1.2.3.4 --User Foo

and the following invalid:
--WorkspaceName MyWorkspace

Missing required connection options

--ConnectionName MyConnection

Missing workspace options

--WorkspaceName MyWorkspace --ConnectionName MyConnection --User Foo

Invalid mixture of mutual exclusive connection options

Most helpful comment

+1

All 3 comments

+1

Another approach could be to bundle the options into individual members.

class Opts
{
    [SubOptions] // probably using a marker attribute 
    public IConnectionOptions ConnectionOptions { get; set; }
    [SubOptions]
    public IWorkspaceOptions WorkspaceOptions { get; set; } 
}

Of course also class could be used instead of interfaces in this case.

I'm always supportive of an option that doesn't rely on maintaining linkages via strings. Harder to break and easier to debug.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Silverwing1983 picture Silverwing1983  路  4Comments

timotei picture timotei  路  4Comments

SeanoNET picture SeanoNET  路  4Comments

ericnewton76 picture ericnewton76  路  4Comments

Rohansi picture Rohansi  路  6Comments