Skip to content
Taylor Everding edited this page Feb 1, 2017 · 3 revisions

Welcome to the yargs wiki (and cheat-sheet)!

for var y = require("yargs"):

y.argv

  • named parameters: app.js -x 1 -> y.argv = {x:1}

y.argv._

  • generic/unnamed parameters as an array

y.alias("b", "boolean")

  • defines a bigger-name parameter: app.js --boolean -b -> .argv = {b:2}

y.array("x")

  • declare a variable as an array: app.js -x one two -> .argv = {x: ["one", "two"]}

y.boolean("b").boolean("c"),
y.boolean["b","c"])

  • explicitly declare booleans: app.js -b -c -> .argv = {b:1,c:1}

y.check((argv, opts) => {...})

  • programmatically verify input

y.choices("x", ["one", "two"]),
y.choices({x: ["one", "two"], y:[1,2]})

  • specify an option set for a parameter or parameters

y.coerce("x", arg => x.toLowerCase()),
y.coerce({x: x=>x.toLowerCase(), y: Date.parse}),
y.coerce(["x","z"], v => v.toLowerCase())

  • manipulate the value of a parameter or parameters: app.js -x THREE -> .argv = {x: "three"}

y.command(cmd, desc, [builder],[handler])
y.command(cmd, desc, [module])
y.command(module)

  • document the commands exposed by the application (allows otherwise anonymous params to be instructional)

y.commandDir(directory,[opts])

  • specify a directory filled with modules that map to commands

y.completion([cmd],[description],[fn])

  • enable bash-type completion shortcuts

y.config([key],[description],[parseFn])
y.config(object)

  • specify configuration by an object or a file

y.conflicts(x, y)

  • defines that x and y are mutually exclusive

y.count("b")

  • implicitly declare booleans: app.js -b -b -> .argv = {b:2}

y.default("b", 3),
y.default({b:3})

  • define default parameters: app.js -b -> .argv = {b:4} for both of these

y.demand(['x','b'])

  • requires specific named parameters: app.js -x and app.js -b are invalid, where app.js -x -b is allowed

y.demand(3)

  • demands a minimum number of unnamed variables

y.describe(key,desc)

  • provide parameter descriptions for help requests

y.detectLocale([boolean])

  • specify is yargs should attempt to detect OS locale (defaults to true)

y.env([prefix])

  • add environment variables to the command line

.epilog(str)
.epilogue(str)

  • Final instruction text

.example(cmd, desc)

  • provide command examples

.fail(err => console.error(msg, err))

  • define a method to call instead of printing an error message

.getCompletion(args,done)

  • programmatically get completion choices for one line

.globals(globals)

  • declare options that don't get reset between command executions

.group(key[s], groupName)

  • organize help content

.help()
.help([option|boolean])
.help([option, [description | boolean]])
.help([option, [description, [boolean]]])

  • support --help

.implies(x,y)

  • allow the presence of one parameter to require another parameter (if x, y is required)

.locale("de"|"en"|"es"|"fr"|"id"|"it"|"ja"|"ko"|"nb"|"pirate"|"pl"|"pt"|"pt_BR"|"tk"|"zh")

  • override the detected OS locale

.nargs(key, count)

  • specify how many tokens to consume after key

.normalize(key)

  • marks key as a path to be normalized

.number(key|keys)

  • marks key(s) to be parsed as numbers

option(key, opt)
options(key, opt)

  • shortcut to specify many commands at once

.parse(args)

  • process args instead of process.argv

.pkgConf(key, [cwd])

  • read the config from package.json.key

.recommendCommands()

  • show suggestions if no command found

.require(key, [msg|boolean])
.required(key, [msg|boolean])

  • see .demand()

.requiresArg(key)

  • specify params that require values

.reset()

  • reset the argument object

.showCompletionScript()

  • generate a bash completion script

.showHelp(consoleLevel='error')

  • show the help text, optionally specify console level

.showHelpOnFail(enable[,message])

  • customize the show help on error behavior

.skipValidation(key[s])

  • specify keys that don't get validated

.strict()

  • don't allow unrecognized arguments

.string(key[s])

  • force key(s) to not be evaluated as booleans or numbers

.updateLocal(obj)
.updateStrings(obj)

  • override yargs built-in strings

.usage(message[, opts])

  • set the help example string

.version([option],[description],[version])

  • allow for showing application version

.wrap(column)

  • wrap lines at column - use .wrap(null) to turn off wrapping
Clone this wiki locally