CommandLineUtils icon indicating copy to clipboard operation
CommandLineUtils copied to clipboard

Parse Sub-commands with their parameters

Open rychlym opened this issue 8 years ago • 7 comments

Hello, Is it correct to have command line e.g. as follows?

app.exe nainCommand mainCmdArg0 subCommand subCmdArg0 ..

if so, then the showHelp for botch command, sub-command seems to work (with one note below) however:

  1. the subCmdArg0 is not recognized during processing - CommandLineProcessor.ProcessCommandOrArgument() leads to HandleUnexpectedArg("command or argument") the fix of it actually might be easy - to remove block if (_currentCOmmandArguments == null) ...and add _currentCommandArguments = new CommandArgumentEnumerator(_currentCommand.Arguments.GetEnumerator()); right after the _currentCommand is set (2 places in the code).

  2. after applying above fix, then the ArgumentAttributeConvention somehow does not work properly -
    It ends up with a TargetException "Object does match target", inside ReflectionHelper.GetPpropertySetter because it tries to set the mainCmdArg0 to the correct property (property name is determined correctly property in the main command), but the target object is the (selected) subCommand. I believe that ArgumentAttributeConvention should provide populating all the underlying models's attributed arguments , but it is looking it does not consider the hirerarchy of sub-commands and theirs own arguments. (I would like to finally access in my code OnExecute to the all possible parent command's arguments/optiosn - not only to the selected sub-command argument/options - also a kind of would like to write parent command arguments, options for showing help)

here the fix seems to me not be so trivial as would be for the 1) ... Is it possible to somehow fix that all?

rychlym avatar Jun 06 '18 22:06 rychlym

This is a guess, but I believe you can't achieve what you want because the argument and subcommand blocks are both positional parameters. If this were allowed the syntax would be ambiguous.

The code for this is here: https://github.com/natemcmaster/CommandLineUtils/blob/c3370cf84cf4240213d9916d024206a4b7c4b76b/src/CommandLineUtils/Internal/CommandLineProcessor.cs#L111-L139

The command/subcommand features approximate what git does... I can't think of an example where git uses the command-argument-subcommand-argument pattern. I'd suggest restructuring your app to:

app.exe mainCommand subCommand mainCmdArg0 subCmdArg0 ...

where only the subcommand accepts the arguments (I kept the naming of mainCmdArg0 but its really a subcommand arg)

or:

app.exe mainCommand -mainCmdOption0 some_value subCommand subCmdArg0 ...

where the mainCommand accepts an option rather than an argument.

atruskie avatar Jun 06 '18 22:06 atruskie

@atruskie is right. This is not currently supported.

I am hesitant to add this. The only example of a well-known tool that supports something like this is the dotnet-sln command.

Usage: dotnet sln [options] <SLN_FILE> [command]

Arguments:
  <SLN_FILE>   Solution file to operate on. If not specified, the command will search the current directory for one.

Commands:
  add, list, remove

The only reason this works is that acceptable values for <SLN_FILE> and [command] are disjoint sets (because <SLN_FILE> always ends in ".sln"). If the sets of valid values intersect, the syntax becomes ambiguous.

I'll leave this open in the backlog. I don't think there is much demand for this pattern, but I'd like to see if there are others who want this.

natemcmaster avatar Jun 07 '18 03:06 natemcmaster

thanks for the suggestion: :) I tend to create a command hierarchy and have all in correct in places (like above fits better to the sln command (comparing to add, list ,remove sub-commands)).

I didnt' realized abiguity before in my case also not obvious, so thanks for clarifying it - now I have undestood, that ambiguity is that afer one command may follow either argument or a following subcommand name.

I understood implementing that could be a slipery slope , just thinking about it was implemeted: So first rule which could work is, that if the follower (arg.Raw in the code) is same as sub-commad, then it is decided as sub-command; otherwise as an argument (the wirst part of the rule is already in the code) It appeared to me an idea, if the follower is quoted, the above rule is overriden that folower is interpreted as an argument (supposing quoted (sub-)commads with spaces are not allowed). (This allows to interpret the special need of argument same as sub-command Do you still see in these two rules any unsolved ambiguity/trap,?

for the curiosity: the sln-file might not always end with *.sln, the "ambiguity" seems to be solved with error message:

dotnet sln list list

ends with error: 'Unrecognized command or argument 'list''

luckily the list is the file name so alteratively:

dotnet sln .\list list

works corretly.

dotnet sln list

interestingly did same work as dotnet sln .\list list (so it is looking, the list sub-command is the default and it somehow supposes?? the list is a valid file and proceed the sub command, but since that I can't follow the reason it cannot proceed with the special case dotnet sln list list)

BTW: Do you know by chance what about following options pattern? command-optionCommand1OptionCommand2-subcommand-optionSubCommand1 ... The pattern seems to be supported, doesn't it? What if options are CommandOptionType.SingleOrNoValue - I see here also an similar ambiguity, that at certain position could be either option value or the following sub-command...

rychlym avatar Jun 07 '18 19:06 rychlym

This is the reason that using CommandOptionType.SingleOrNoValue requires users specify values as --option:value or --option=value. As you correctly pointed out, --option value would be ambiguous in some cases.

natemcmaster avatar Jun 08 '18 04:06 natemcmaster

ok, meanwhile I tried to implement those ideas on the current souce code andhave just published in my repository as cmd-arg-cmd-arg pattern

there are actually following rules top-down priority :

  1. if the argument is mandatory (required), the argument is expected
  2. if the argument is quoted, the argument is expected (and unqote the argument)
  3. sub-command is checked to match and if not presented, the non-required argument is expected.

(in order to do the 1) rule, I had to to expose HastIsRequired on the IValidator, AttributeValidator, and I found also suitable to have IsRequired parameter in the ArgumentAttribute)

Would you be interested to look to the pull request from that?

rychlym avatar Jun 08 '18 06:06 rychlym

Thanks for the offer @rychlym, but I think I'd like to wait to see first if there is more interest in this. It's potentially a breaking change in behavior, and since I don't have a good sense yet for whether this is niche or a widespread need.

natemcmaster avatar Jun 12 '18 04:06 natemcmaster

Understood, no problem.

rychlym avatar Jun 12 '18 05:06 rychlym