yargs says nested arguments provided via JSON are "missing"

Using yargs 16.2.0, I’ve defined some nested options and provided them via a config file, but yargs still claims that they are missing:

yargs

yargs
  .command('do-stuff', 'Does some neat stuff.', o => o
    .option('test.something', {
      type: 'string',
      demandOption: true,
      requiresArg: true,
    })
  )
  .strict()
  .demandCommand()
  .config('env')
  .help()
  .argv;

config.json

{
  "test": {
    "something": "blah"
  }
}

Executed via …

> node index.js -- do-stuff --env config.json

index.js do-stu

Does some neat stuff.

Options:
  --version         Show version number                                [boolean]
  --env             Path to the environment config JSON from which to load
                    arguments.
  --help            Show help                                          [boolean]
  --test.something                                           [string] [required]

Missing required argument: test.something

I then edited node_modules/yargs-parser/build/index.cjs (original here) and added a line to log out argv after parsing the config JSON:

setConfigObject(config);            // line 630, existing
console.log(JSON.stringify(argv));  // added for debugging

The resulting output:

{
    "_": ["do-stuff"],
    "env": "config.json",
    "test":
    {
        "something": "blah"
    }
}

I made the same change in node_modules/yargs/build/index.cjs, after line 1472 (just before the "Missing required arguments" message is displayed, and the JSON output still includes the test.something argument.


So, why is yargs claiming that my argument(s) were not supplied, when even argv shows that they were?

160 thoughts on “yargs says nested arguments provided via JSON are "missing"”

  1. 561309 662932Yours is actually a prime example of informative writing. I think my students could learn a good deal from your writing style and your content material. I may share this article with them. 26047

    Reply

Leave a Comment