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

TypeError when running nested subcommand #1274

Closed
maximilianschmitt opened this issue Jun 6, 2020 · 3 comments
Closed

TypeError when running nested subcommand #1274

maximilianschmitt opened this issue Jun 6, 2020 · 3 comments
Assignees
Labels
bug Commander is not working as intended
Milestone

Comments

@maximilianschmitt
Copy link

Repo with minimal example code to reproduce: https://github.com/maximilianschmitt/commander-debug


Hi!

I'm currently developing a CLI where I'm wanting to nest the subcommands.

I have the following files:

cli/
    hello                
    hello-world          # `hello world`
    hello-world-germany  # `hello world germany`

And I'm trying to run them like so:

$ node cli/hello
# Should run cli/hello

$ node cli/hello world
# Should run cli/hello-world

$ node cli/hello world germany 
# Should run cli/hello-world-germany

I've read that when defining nested subcommands, you need to specify the executableFile, so I've done this in cli/hello:

#!/usr/bin/env node
const { program, Command } = require("commander");
const packageJSON = require("../package.json");

program.version(packageJSON.version);

const helloWorld = new Command("world");
helloWorld.command("germany", "Saying hello to Germany", {
  executableFile: "hello-world-germany",
});

program.addCommand(helloWorld);

program.parse(process.argv);

However, when I run node cli/hello world germany, I get a TypeError:

~/Desktop/commander-debug $ node cli/hello world germany
internal/validators.js:120
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received null
    at validateString (internal/validators.js:120:11)
    at Object.extname (path.js:1229:5)
    at Command._executeSubCommand (/Users/max/Desktop/commander-debug/node_modules/commander/index.js:774:46)
    at Command._dispatchSubcommand (/Users/max/Desktop/commander-debug/node_modules/commander/index.js:863:12)
    at Command._parseCommand (/Users/max/Desktop/commander-debug/node_modules/commander/index.js:882:12)
    at Command._dispatchSubcommand (/Users/max/Desktop/commander-debug/node_modules/commander/index.js:865:18)
    at Command._parseCommand (/Users/max/Desktop/commander-debug/node_modules/commander/index.js:882:12)
    at Command.parse (/Users/max/Desktop/commander-debug/node_modules/commander/index.js:717:10)
    at Object.<anonymous> (/Users/max/Desktop/commander-debug/cli/hello:14:9)
    at Module._compile (internal/modules/cjs/loader.js:1138:30) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Is this a bug or am I doing something wrong?

Thanks for your help!

@shadowspawn shadowspawn added the bug Commander is not working as intended label Jun 6, 2020
@shadowspawn
Copy link
Collaborator

You have uncovered a bug, but I think your program isn't working the way you intend either and fixing that will avoid the bug.

The error message is due to a bug when a sub-sub-command implemented as a stand-alone executable is added. The internal _scriptPath property has not been set. (I think this probably got broken when the support was added for specifying where the parse arguments came from.)

For implementing nested subcommands, you can either do it within a single program (from Commander v5), or using stand-alone executables.

Your program is a bit of both. Would you like some help towards doing it within a single program, or as multiple stand-alone subcommands? (Or both approaches because you are exploring!)

The example file for nested subcommands within a single program is: https://github.com/tj/commander.js/blob/master/examples/nestedCommands.js

The example file for stand-alone executable subcommands just has one layer, but the subcommands can add subcommands...: https://github.com/tj/commander.js/blob/master/examples/pm

@maximilianschmitt
Copy link
Author

maximilianschmitt commented Jun 6, 2020

Hey @shadowspawn, thanks for the quick and helpful reply!

You pointed me in the right direction and I've got the following program now working :)

cli/hello

#!/usr/bin/env node
const { program, Command } = require("commander");
const packageJSON = require("../package.json");

program.version(packageJSON.version);
program.command("world", "Saying hello world");

program.action(() => {
  console.log("hello");
});

program.parse(process.argv);

cli/hello-world

#!/usr/bin/env node
const { program, Command } = require("commander");
const packageJSON = require("../package.json");

program.version(packageJSON.version);
program.command("germany", "Saying hello to Germany");

program.action(() => {
  console.log("Hello world");
});

program.parse(process.argv);

cli/hello-world-germany

#!/usr/bin/env node
const { program } = require("commander");
const packageJSON = require("../package.json");

program.version(packageJSON.version);

program.action(() => {
  console.log("Hello world germany");
});

program.parse(process.argv);

...and when I run the programs I get the expected output :)

~/Desktop/commander-debug $ node cli/hello
hello
~/Desktop/commander-debug $ node cli/hello world
Hello world
~/Desktop/commander-debug $ node cli/hello world germany
Hello world germany

Thank you! 🥳

shadowspawn added a commit to shadowspawn/commander.js that referenced this issue Aug 1, 2020
@shadowspawn shadowspawn self-assigned this Aug 1, 2020
@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Aug 1, 2020
@shadowspawn shadowspawn removed their assignment Aug 1, 2020
@shadowspawn shadowspawn added this to the v6.1.0 milestone Aug 5, 2020
@shadowspawn shadowspawn self-assigned this Aug 26, 2020
@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Aug 28, 2020
@shadowspawn
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Commander is not working as intended
Projects
None yet
Development

No branches or pull requests

2 participants