Tyrrrz/CliFx

Allow for minimum number of items in array-backed parameters

Merlin04 opened this issue · 5 comments

When I have a parameter like this:

[CommandParameter(0)]
public FileInfo File { get; set; }

And I don't pass in a file, the program display the help message. However, if I make that a list:

[CommandParameter(0)]
public IReadOnlyList<FileInfo> Files { get; set; }

And don't pass in any files, the command runs. It would be really helpful to be able to specify that the parameter needs to have at least one file.

I'm interested in taking this on as I'd like to start contributing if that's alright.

Sure thing. If you want, you can outline how you're planning to approach this and we can discuss it. Or you can just make a PR and we'll take it from there.

Give me a chance to take a gander through the source and I'll post here my idea and see if that'll work and then I'll try to hack it out.

@Tyrrrz So for the implementation it would require adding optional parameter of MinimumSize or something of the sort with a default of 0. If this is specified and greater than 0 then it would validate this in ValidateParameters. You could also add to the analyzer that if the value for minimum is larger than 1 then the expected size for the parameter is a collection.

Let me know your thoughts and if I'm way off base.

I think you can just rely on IsRequired parameter. If it's set, having 0 items is invalid.