Skip to content

Strict mode doesn't know about context variables [7.x] #791

@scriptdaemon

Description

@scriptdaemon

Discussion moved from #776.

When running code like so against the 7.x branch:

'use strict'

// Node packaged modules
const yargs = require('yargs')
const parser = yargs()
parser.command({
  command: '!ch', // config.get('cmd'),
  description: 'test desc', // config.get('description'),
  builder: yargs => {
    yargs.command('dice', '', () => {}, () => {
      console.log('dice')
    })
    yargs.command('uptime', '', () => {}, () => {
      console.log('uptime')
    })
    yargs.help()
    yargs.strict()
    yargs.version()
  }
})

parser.parse('!ch uptime', { 'test': 'test' }, (err, argv, output) => {
  if (err && !output) throw err
  if (output) {
    console.log(output)
  }
})

parser.parse('!ch dice', { 'test': 'test' }, (err, argv, output) => {
  if (err && !output) throw err
  if (output) {
    console.log(output)
  }
})

I get an error declaring "test" is an unknown argument:

kwilliams@KENLAPTOP:/mnt/c/Users/kwilliams/Documents/Projects/cheevobot$ node .
 !ch uptime

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

Unknown argument: test
 !ch dice

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

Unknown argument: test

In response to @bcoe:

It seems like to simplify this use-case though, we should add all keys provided in context to the list of known arguments?

That sounds like it should do the trick. In my use-case, the context variables are always something I know ahead of time (at least for now) and can thus account for by specifying option() for each of them directly, but in some cases the context might not be known ahead of time.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions