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

Automatic binding of consecutive parameters to collection #368

Closed
Starkie opened this issue Jan 12, 2019 · 5 comments
Closed

Automatic binding of consecutive parameters to collection #368

Starkie opened this issue Jan 12, 2019 · 5 comments

Comments

@Starkie
Copy link

Starkie commented Jan 12, 2019

Some CLI programs infer that a list of consecutive parameters belong to the same option. For example, the program cp in unix has the following profile, were one or more source files can be specified to be copied to directory:

cp [OPTION]... SOURCE... DIRECTORY

The usage would be as following:

cp file1 file2 directory

I tried to implement a similar behaviour in a command line program, that receives a collection of paths:

static void Main(string[] paths)
{
  // Do something
}

I expected to invoke the program like this:
dotnet run -- --paths file1 file2 file3

But instead, the argument name had to be specified each time:
dotnet run -- --paths file1 --paths file2 --paths file3

Does a way to implement options with a similar behaviour exist?

Might be related to #310.

@jonsequitur
Copy link
Contributor

Yes, I think #310 is related, although we haven't discussed enabling this for options, just for commands. #310 will allow command arguments to differ by type, name, and arity, so in your first example, the cp command would accept an Argument<FileInfo[]> and a second Argument<DirectoryInfo>.

The behavior you're describing is currently available in a limited way for command arguments if the Command.Argument.Arity is greater than 1 (e.g. Argument<string[]>), but without #310, your program will have to do its own interpretation of the arguments, and help isn't yet able to differentiate them. But at the moment cp could look like this:

var cp = new Command("cp")
{
    Argument = new Argument<string[]>()
};

Do you have any examples where you would expect the option arguments to similarly be able to differ by type?

Also related: #37

@Starkie
Copy link
Author

Starkie commented Jan 14, 2019

#310 will allow command arguments to differ by type, name, and arity, so in your first example, the cp command would accept an Argument<FileInfo[]> and a second Argument<DirectoryInfo>.

I'm not sure I understand correctly this part. Does this mean that both types could match to the same --paths argument in the same command invocation? Or would that be the same command with a different "profile"?

I'll check the workaround you provided when I get the time. Is there something similar for the DragonFruit model? It's the one we are currently using.

Do you have any examples where you would expect the option arguments to similarly be able to differ by type?

In my use case, no. The inputs of the argument are all of the same type.

Here's the link of the PR where I'm using the library, in case it is of any use. https://github.com/Starkie/DuplicateExtensionFinder/pull/1

@jonsequitur
Copy link
Contributor

I'm not sure I understand correctly this part. Does this mean that both types could match to the same --paths argument in the same command invocation? Or would that be the same command with a different "profile"?

It would be the same command, so for example given a tool like this, where source_file and target_dir are arguments:

usage: cp source_file target_dir

it might bind to a command handler like this:

void copy( FileInfo source_file, DirectoryInfo target_dir ) { }

@Starkie
Copy link
Author

Starkie commented Jan 15, 2019

Oh, sorry for the confussion: In my C# example I didn't specify the target_dir that I mentioned in cp. 😅
But yes, I think that would be a valid model to represent this command.

@jonsequitur
Copy link
Contributor

I'm closing this as I believe it's a duplicate of #310. Please reopen if you think I might be overlooking some detail.

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

No branches or pull requests

2 participants