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

Passing description to the "command()" method causes error being produced in runtime #976

Closed
slavafomin opened this issue Jun 16, 2019 · 8 comments

Comments

@slavafomin
Copy link

Hello!

Thank you for this great library!

However, when command description is passed as a second argument to the Command.command() method it causes the following error in runtime error: foo-bar(1) does not exist, try --help.

This doesn't work:

  program
    .command('build', 'Builds the package')
    .action(buildCommand)
  ;

But this works:

  program
    .command('build')
    .description('Builds the package')
    .action(buildCommand)
  ;

This is extremely counter-intuitive, especially when you explore the API using IDE autocompletion. When I see the description argument of the method, my first thought it to pass it to it.

Having two methods of passing the description with one of them triggering some different behavior looks pretty insane from the API standpoint.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jun 16, 2019

It is a common cause of confusion. Added this issue to #938.

@andares
Copy link

andares commented Jun 18, 2019

I have same problem, when I try to attach a desc text to command, It's not work.

alias method also can not work.

program
  |> command("cc <dir>"/*, "Compile pattern to docker compose config"*/)
  /* |> description("Compile pattern to docker compose config") */
  /* |> alias("cc") */
  |> option("-o, --output", "Output dir")
  |> action(Cmd_Cc.main);

coding with reasonml now 😹

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jun 18, 2019

FYI @andares (and I think @slavafomin worked this out after getting caught out by the current behaviour).

For action based command handling you pass the description separately from the command:

program
    .command('build')
    .description('Builds the package')
    .action(buildCommand);

For git-style executables you pass the description along with the command:

program
    .command('build', 'Builds the package');
    // Commander looks for separate executable file

@andares
Copy link

andares commented Jun 21, 2019

if set description with command() function:

var Process = require("process");
var Commander = require("commander");

var program = Commander.version("0.1.0");

program.allowUnknownOption(false);

program.command("cc <dir>", "Compile pattern to docker compose config").option("-o, --output", "Output dir").action((function (param) {
        console.log("test");
        return /* () */0;
      }));

program.parse(Process.argv);

there has problem when run:

图片

help [cmd] is not work too.

@shadowspawn
Copy link
Collaborator

@andares
Do not put the description in the call to .command, just pass one parameter which is the command name.

I fixed my comment with examples where I had a mistake, hopefully clearer now! (Sorry.)
#976 (comment)

@andares
Copy link

andares commented Jun 22, 2019

I got it, thanks 😄

@shadowspawn
Copy link
Collaborator

This issue will be resolved when v3.0.0 is released. Available now as a prerelease. See #1001

Reworked the README and JSDoc and TypeScript to hopefully make the two modes of operation clearer. Have not changed the runtime, but will close this issue and see how the changes help.

@shadowspawn
Copy link
Collaborator

Reworked the README and JSDoc and TypeScript to hopefully make the two modes of operation clearer. Have not changed the runtime, but will close this issue and see how the changes help.

Shipped in v3: https://github.com/tj/commander.js/releases/tag/v3.0.0

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

No branches or pull requests

3 participants