tj / commander.js

node.js command-line interfaces made easy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

intermingled commands & arguments not working as imagined

iiian opened this issue · comments

I am trying to build a CLI that wraps some of my apis. As such, I want to let my users specify commands like "resource <resource_id> subresource <subresource_id> action". For example:

$ my-cli workflow <testwfid> thing <testthingid> status

which might drive an api call that looks like

GET /api/v1/workflow/testwfid/thing/testthingid/status

To try implementing this with commander, I've done:

const cmd = new Command('workflow');
cmd.argument('<wfid>');
const subcmd = new Command('thing');
subcmd.argument('<thingid>');
subcmd.command('status').action(() => { /* ... */ });
cmd.addCommand(subcmd); // thus, workflow <wfid> thing <thingid> status should be possible
program.addCommand(cmd);

However:

$ tsc # to create my build/src/main.js
$ npm link # to put it in place
$ my-cli -h
Usage: main [options] [command]

Options:
  -h, --help       display help for command

Commands:
  workflow <wfid>
  help [command]   display help for command

# ok, looks good so far... guess I'll try:
$ my-cli workflow x thing j status
error: unknown command 'workflow'
# oh, that was weird. what if I try:
$ my-cli workflow x
error: unknown command 'workflow'
# sad, I feel sad
$ my-cli workflow
error: unknown command 'workflow'
# aw man, I did something wrong

Am I doing something wrong or have I excited the confines of defined behavior for commander?

Thanks again for this wonderful library.

Seems like we'd just need to update _parseCommand to check if this command segment has argument(s)/option(s).

Although since we're already at the threshold of v12.0, I can't shake the sneaking suspicion that this has already been discussed/rejected before in some form.

Let me know.

Commander does not support a subcommand coming after a command-argument. You could perhaps supply testwfid in your original calling example as an option instead like:

$ my-cli workflow --wid=<testwfid> thing <testthingid> status

(I am a bit confused about the behaviour you were seeing. It does not match what I expected. I'll try reproducing later. I enjoyed the commentary with the command attempts. 😆 )

Ahh. Well, in any case I just bit the bullet and rolled my own small version. If this is a use case you guys decide to fold in, let me know I will tell all of my friends!

Checking the failure mode, I tried your sample program and got a different error to what you saw. But the high level story is that what you were attempting is not supported! So feel free to ignore this info.

% node index.mjs -h
Usage: index [options] [command]

Options:
  -h, --help       display help for command

Commands:
  workflow <wfid>
  help [command]   display help for command
# this matches report
% node index.mjs workflow x thing j status
error: unknown command 'x'
# workflow does not have an action handler, so Commander is expecting one of the subcommands
# to come next in the arguments
% node index.mjs workflow 
Usage: index workflow [options] [command] <wfid>

Options:
  -h, --help       display help for command

Commands:
  thing <thingid>
  help [command]   display help for command