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

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

Closed
sn0wcat opened this issue Jun 18, 2020 · 4 comments
Closed

Comments

@sn0wcat
Copy link

sn0wcat commented Jun 18, 2020

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
Copy link
Collaborator

See #1226 for explanation and work-around.

@shadowspawn
Copy link
Collaborator

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
Copy link

js2me commented Aug 15, 2022

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
Copy link

js2me commented Aug 15, 2022

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

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