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

Nested Sub-Commands don't follow through. #1905

Closed
seivan opened this issue Apr 7, 2021 · 3 comments
Closed

Nested Sub-Commands don't follow through. #1905

seivan opened this issue Apr 7, 2021 · 3 comments
Labels

Comments

@seivan
Copy link
Contributor

seivan commented Apr 7, 2021

Running
$ mybin mycommand sub hello
Will output Missing required argument: command

Yargs
  .default(YargsHelper.hideBin(process.argv))
  .scriptName(ProjectPackage.name)
  .command("mycommand <command>", "", (yargs) => {
    return yargs
      .positional("command", {
        choices: ["sub", "some", "choices", "would-love-if-this-was-dynamic"],
      })
      .command("sub <other>", "", (otherYarg) => {
        return otherYarg
          .positional("other", { type: "string" })
        }, (args) => { })

This works if you remove the first positional under mycommand for <command>

I am just not sure how this is supposed to be, am I not allowed to have a positional for a subcommand to describe more or offer choices?

@bcoe
Copy link
Member

bcoe commented Apr 7, 2021

Yargs
  .default(YargsHelper.hideBin(process.argv))
  .scriptName(ProjectPackage.name)
  . strictCommands()
  .command("mycommand", "", (yargs) => {
    return yargs 
      .positional("command", {
        choices: ["sub", "some", "choices", "would-love-if-this-was-dynamic"],
      })
      .command("sub <other>", "", (otherYarg) => {
        return otherYarg
          .positional("other", { type: "string" })
        }, (args) => { })

☝️ if mycommand accepts additional commands, it should not use positionals. Instead you should be able to do something like above.

@seivan
Copy link
Contributor Author

seivan commented Apr 7, 2021

@bcoe Thanks for getting back so quickly.
Yes I did mention that

This works if you remove the first positional under mycommand for

And that's what I've done now, but I am bit confused over the concept of positional.
Shouldn't a subcommand be considered a positional?

Since I'm getting the impression that this isn't a bug, could it be worth documenting?

It's not unreasonable to assume that this would work:

  .command("mycommand <sub>", "", (yargs) => {
    return yargs 
      .positional("sub", {
        type: ["string"],
      })
      .command("oneOftheSubs <other>", "", (otherYarg) => {
        return otherYarg
          .positional("other", { type: "string" })
        }, (args) => { })

Either way I'm closing the ticket since the impression I get, that this isn't a bug.

Thanks for the help!

@seivan seivan closed this as completed Apr 7, 2021
@bcoe
Copy link
Member

bcoe commented Apr 7, 2021

@seivan I think it would be worth documenting, the fact that positionals behave like options, whereas commands are their own beast.

I think the behavior is correct, mainly because <sub> implies there would be a variable called sub with some value you've provided, whereas the behavior is actually that if the single constant value oneOfthesubs is provided, that command should execute.

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

No branches or pull requests

2 participants