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

halt-at-non-option is not working as expected. #2423

Open
sergei-dyshel opened this issue Jul 6, 2024 · 1 comment
Open

halt-at-non-option is not working as expected. #2423

sergei-dyshel opened this issue Jul 6, 2024 · 1 comment
Labels

Comments

@sergei-dyshel
Copy link

sergei-dyshel commented Jul 6, 2024

I'm trying to write a CLI similar to ssh. Here's the minimal example:

import yargs from "yargs";

function main() {
  yargs(process.argv.slice(2))
    .usage(
      "$0 <host> [<cmd>...]",
      "Run command via ssh",
      (yargs) =>
        yargs
          .positional("host", { type: "string", demandOption: true })
          .positional("cmd", { array: true, type: "string" })
          .options({
            verbose: { type: "boolean", alias: "v", count: true },
          }),
      (args) => {
        console.log(args);
      },
    )
    .strict()
    .parserConfiguration({
      "halt-at-non-option": true,
    })
    .parseSync();
}

main()

which just adds one option. When parsing node my-script.js my-host ls, I get this error:

Not enough non-option arguments: got 0, need at least 1

The only way I manage to get this work is with default command syntax:

  const args = yargs(process.argv.slice(2))
    .usage("$0 <host> [<cmd>...]")
    .options({
      verbose: { type: "boolean", alias: "v", count: true },
      zone: { type: "string", alias: "z", describe: "Availability Zone" },
    })
    .strict()
    .parserConfiguration({
      "halt-at-non-option": true,
    })
    .parse();

though in this case positional arguments are not supported so I can't mix <host> with option flags but at least I can extract it as first element of args._

@shadowspawn
Copy link
Member

I reproduced the problem.

Deep in the code... At the point the error is being thrown, the arguments are in arg.-- instead of argv._. I can see code "counting" the positionals with and without taking -- into account. I suspect need the first case to include -- length like in the second case.

this.validation.positionalCount(demanded.length, argv._.length);

argv._.length + (argv['--'] ? argv['--'].length : 0);

(I don't currently understand why halt-at-non-option triggers the failure.)

@shadowspawn shadowspawn added the bug label Jul 7, 2024
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