Link Search Menu Expand Document

API

array

  • if .array('foo') is set, –foo foo bar will be parsed as ['foo', 'bar'] rather than as ‘foo’
  • if you use the option multiple times all the values will be flattened in one array

command(cmd, desc, [builder], [handler])

  • args:

    • cmd: str str[], name of cmd
    • desc: str, description
    • builder: obj func, give hint about options accepted
      • type: BuilderCallback

        • obj
        yargs
          .command('get', 'make a get HTTP request', {
            url: {
              alias: 'u',
              default: 'http://yargs.js.org/'
            }
          })
          .help()
          .argv
        
        • func
         yargs
          .command('get', 'make a get HTTP request', function (yargs) {
            return yargs.option('url', {
              alias: 'u',
              default: 'http://yargs.js.org/'
            })
          })
          .help()
          .argv
        
    • handler: func, gets executed with argv
  • related

    • BuilderCallback<T, U>
      type BuilderCallback<T, R> = ((args: Argv<T>) => PromiseLike<Argv<R>>) | ((args: Argv<T>) => Argv<R>) | ((args: Argv<T>) => void);
      
    • Argv<T = {}>

cook

  • demand positional arg
    • note: adding demandOption or marking requried doesn’t do anything
command('doFoo afooarg')
  • optional positiona arg
command('doFoo [afooarg]')

demandOption(key, [msg|bool])

option(name: str, opts: OptObj)

  • https://github.com/yargs/yargs/blob/HEAD/docs/api.md#optionkey-opt

  • alias: string or array of strings, alias(es) for the canonical option key, see alias()

  • array: boolean, interpret option as an array, see array()

  • boolean: boolean, interpret option as a boolean flag, see boolean()
  • choices: value or array of values, limit valid option arguments to a predefined set, see choices()
  • coerce: function, coerce or transform parsed command line values into another value, see coerce()
  • config: boolean, interpret option as a path to a JSON config file, see config()
  • configParser: function, provide a custom config parsing function, see config()
  • conflicts: string or object, require certain keys not to be set, see conflicts()
  • count: boolean, interpret option as a count of boolean flags, see count()
  • default: value, set a default value for the option, see default()
  • defaultDescription: string, use this description for the default value in help content, see default()
  • demandOption: boolean or string, demand the option be given, with optional error message, see demandOption()
  • desc/describe/description: string, the option description for help content, see describe()
  • global: boolean, indicate that this key should not be reset when a command is invoked, see global()
  • group: string, when displaying usage instructions place the option under an alternative group heading, see group()
  • hidden: don’t display option in help output.
  • implies: string or object, require certain keys to be set, see implies()
  • nargs: number, specify how many arguments should be consumed for the option, see nargs()
  • normalize: boolean, apply path.normalize() to the option, see normalize()
  • number: boolean, interpret option as a number, number()
  • requiresArg: boolean, require the option be specified with a value, see requiresArg()
  • skipValidation: boolean, skips validation if the option is present, see skipValidation()
  • string: boolean, interpret option as a string, see string()
  • type: one of the following strings
    • ‘array’: synonymous for array: true, see array()
    • ‘boolean’: synonymous for boolean: true, see boolean()
    • ‘count’: synonymous for count: true, see count()
    • ‘number’: synonymous for number: true, see number()
    • ‘string’: synonymous for string: true, see string()
yargs.option('port', {
  describe: 'port to bind on',
  default: 5000
})

-– example

 .option('run', {
    alias: 'r',
    describe: 'run your program'
  })
  .option('path', {
    alias: 'p',
    describe: 'provide a path to file'
  })
  .option('spec', {
    alias: 's',
    describe: 'program specifications'
  })
  .demandOption(['run', 'path'], 'Please provide both run and path arguments to work with this tool')

config


Copyright © 2020 Thence LLC