question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use .demandOption in a Command Module

See original GitHub issue

When using the Command Module pattern, is there a way to use the .demandOptions() method for each command, instead of in the invocation chain?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sdcaulleycommented, Apr 3, 2022

@jly36963 Thank you for the example. That helps a lot.

0reactions
jly36963commented, Apr 21, 2022

I don’t know if you can use positional and option in the command module object. I also don’t know if you can call demandOption at the command level, but you can on a per-option basis. In general, my opinion is that builder as function is more flexible and better documented than builder as object.

let input
input = 'arithmetic 3 5 --operation=add'
input = 'arithmetic 3 5'

yargs(input)
  .command({
    command: 'arithmetic <a> <b>',
    desc: 'arithmetic desc',
    builder: {
      a: { type: 'number', required: true },
      b: { type: 'number', required: true },
      operation: { 
        type: 'string', 
        choices: ['add', 'subtract', 'multiply', 'divide'],
        demandOption: 'Please provide an operation'
      }
    },
    // builder: yargs => yargs
    //   .positional('a', { type: 'number' })
    //   .positional('b', { type: 'number' })
    //   .option('operation', { 
    //     type: 'string', 
    //     choices: ['add', 'subtract', 'multiply', 'divide'] 
    //   })
    //   .demandOption('operation', 'Please provide an operation'),
    handler: argv => {
      const { a, b, operation } = argv
      let result, operator
      switch (operation) {
        case 'add':
          operator = '+'
          result = a + b
          break
        case 'subtract':
          operator = '-'
          result = a - b
          break
        default:
          console.log(`${operation} is not supported yet`)
          return
      }
      console.log(`${a} ${operator} ${b} = ${result}`)
    }
  })
  .strict()
  .parse()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Additional documentation - yargs - JS.ORG
.command(module). Define the commands exposed by your application. cmd should be a string representing the command or an array of strings ...
Read more >
yargs.Argv.demandOption JavaScript and Node.js ... - Tabnine
Argv.command. Define the commands exposed by your application. · Argv.help. Configure an (e.g. `--help`) and implicit command that displays the usage string and ......
Read more >
How to use the yargs.alias function in yargs - Snyk
demandCommand (1) yargs.help('h') yargs.alias('h', 'help') yargs.alias('v', ... demandOption(['files', 'output-dir']) .usage('Usage: $0 -f <files> -o ...
Read more >
Node.js Yargs Module - GeeksforGeeks
Yargs module is used for creating your own command-line commands in node.js and helps in generating an ... demandOption: true , // Required....
Read more >
types/yargs/index.d.ts - UNPKG
On the other hand, in the declaration of 'usage', a `command: string` parameter ... description: string, module: CommandModule<T, U>): Argv<U>;.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found