tj / commander.js

node.js command-line interfaces made easy
MIT License
26.59k stars 1.69k forks source link

this.name is not a function when a subcommand with option --name is used #1282

Closed sn0wcat closed 4 years ago

sn0wcat commented 4 years ago

Commander in version 5.1.0 seems to have problems with option called name in subcommand

this typescript command snippet works:

program
        .command("create-agent")
        .alias("ca")
        .option("-c, --config <agentconfig>", "config file for agent configuration")
        .option(
            "-r, --cert [privatekey]",
            "required for agents with RSA_3072 profile. create with: openssl genrsa -out private.key 3072"
        )
        .option("-n, --agentname <agentname>", "agent name", `Agent${Date.now()}`)
        .option("-p, --parentid <parentid>", "parent asset id")
        .option("-f, --profile <profile>", "security profile [SHARED_SECRET|RSA_3072]", "SHARED_SECRET")
        .option("-k, --passkey <passkey>", "passkey")
        .option("-y, --retry <number>", "retry attempts before giving up", "3")
        .option("-v, --verbose", "verbose output")
        .description(color("create an agent in the mindsphere *"))
        .action((options) => {
            (async () => {

but replace the --agentname with --name in options

 .option("-n, --name <name>", "agent name", `Agent${Date.now()}`)

and you will get a following exception:

C:\git\github\mindconnect-nodejs\node_modules\commander\index.js:916
        this.emit('command:' + this.name(), operands, unknown);
                                   ^

TypeError: this.name is not a function
    at Command._parseCommand (C:\git\github\mindconnect-nodejs\node_modules\commander\index.js:916:37)
    at Command._dispatchSubcommand (C:\git\github\mindconnect-nodejs\node_modules\commander\index.js:865:18)
    at Command._parseCommand (C:\git\github\mindconnect-nodejs\node_modules\commander\index.js:882:12)
    at Command.parse (C:\git\github\mindconnect-nodejs\node_modules\commander\index.js:717:10)
    at Object.<anonymous> (C:\git\github\mindconnect-nodejs\src\cli\mc.js:84:9)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47

Used node version

$ node --version
v12.16.1
shadowspawn commented 4 years ago

See #1226 for explanation and work-around.

shadowspawn commented 4 years ago

Covered by the linked issue. Added to the Triage item for future reference. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

js2me commented 2 years ago

Hello @shadowspawn I've got the same problem in project https://github.com/acacode/swagger-typescript-api

If you have a free time please check the commander declaration in this file
https://github.com/acacode/swagger-typescript-api/blob/next/index.js

Also if you need steps to reproduce I have spec test for cli commands (npm run test:--cli on next branch)

This bug happened after update commander from ^6.2.1 to ^9.4.0

js2me commented 2 years ago

Sorry, it was my bad, after update was needed to read more documentation about fresh version.

For someone, who dig into this problem too - you should remove .storeOptionsAsProperties(true) and take your program options from program.opts() instead of program.yourOption as it was before