arrow-left

All pages
gitbookPowered by GitBook
1 of 2

Loading...

Loading...

README

hashtag
yargs

Yargs be a node.js library fer hearties tryin' ter parse optstrings.

With yargs, ye be havin' a map that leads straight to yer treasure! Treasure of course, being a simple option hash.

arrow-up-right arrow-up-right arrow-up-right arrow-up-right

Yargs is the official successor to optimist. Please feel free to submit issues and pull requests. If you'd like to contribute and don't know where to start, have a look at :)

hashtag
examples

hashtag
With yargs, the options be just a hash!

plunder.js:

hashtag
But don't walk the plank just yet! There be more! You can do short options:

short.js:

hashtag
And booleans, both long, short, and even grouped:

bool.js:

hashtag
And non-hyphenated options too! Just use argv._!

nonopt.js:

hashtag
Yargs even counts your booleans!

count.js

hashtag
Tell users how to use yer options and make demands.

area.js:

hashtag
After yer demands have been met, demand more! Ask for non-hypenated arguments!

demand_count.js:

hashtag
EVEN MORE SHIVER ME TIMBERS!

default_singles.js:

default_hash.js:

hashtag
And if you really want to get all descriptive about it...

boolean_single.js

boolean_double.js

hashtag
Yargs is here to help you...

Ye can describe parameters fer help messages and set aliases. Yargs figures out how ter format a handy help string automatically.

line_count.js

hashtag
methods

By itself,

will use process.argv array to construct the argv object.

You can pass in the process.argv yourself:

or use .parse() to do the same thing:

The rest of these methods below come in just before the terminating .argv.

hashtag
.alias(key, alias)

Set key names as equivalent such that updates to a key will propagate to aliases and vice-versa.

Optionally .alias() can take an object that maps keys to aliases. Each key of this object should be the canonical version of the option, and each value should be a string or an array of strings.

hashtag
.default(key, value, [description])

Set argv[key] to value if no option was specified on process.argv.

Optionally .default() can take an object that maps keys to default values.

But wait, there's more! the default value can be a function which returns a value. The name of the function will be used in the usage string:

Optionally, description can also be provided and will take precedence over displaying the value in the usage instructions:

hashtag
.demand(key, [msg | boolean])

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

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

If key is a string, show the usage information and exit if key wasn't specified in process.argv.

If key is a number, demand at least as many non-option arguments, which show up in argv._.

If key is an Array, demand each element.

If a msg string is given, it will be printed when the argument is missing, instead of the standard error message. This is especially helpful for the non-option arguments in argv._.

If a boolean value is given, it controls whether the option is demanded; this is useful when using .options() to specify command line parameters.

hashtag
.requiresArg(key)

Specifies either a single option key (string), or an array of options that must be followed by option values. If any option value is missing, show the usage information and exit.

The default behaviour is to set the value of any key not followed by an option value to true.

hashtag
.implies(x, y)

Given the key x is set, it is required that the key y is set.

implies can also accept an object specifying multiple implications.

hashtag
.describe(key, desc)

Describe a key for the generated usage information.

Optionally .describe() can take an object that maps keys to descriptions.

hashtag
.option(key, opt)

hashtag
.options(key, opt)

Instead of chaining together .alias().demand().default().describe().string(), you can specify keys in opt for each of the chainable methods.

For example:

is the same as

Optionally .options() can take an object that maps keys to opt parameters.

hashtag
.usage(message, opts)

Set a usage message to show which commands to use. Inside message, the string $0 will get interpolated to the current script name or node command for the present script similar to how $0 works in bash or perl.

opts is optional and acts like calling .options(opts).

hashtag
.command(cmd, desc, [fn])

Document the commands exposed by your application.

use desc to provide a description for each command your application accepts (the values stored in argv._).

Optionally, you can provide a handler fn which will be executed when a given command is provided. The handler will be executed with an instance of yargs, which can be used to compose nested commands.

Here's an example of top-level and nested commands in action:

hashtag
.example(cmd, desc)

Give some example invocations of your program. Inside cmd, the string $0 will get interpolated to the current script name or node command for the present script similar to how $0 works in bash or perl. Examples will be printed out as part of the help message.

hashtag
.epilogue(str)

hashtag
.epilog(str)

A message to print at the end of the usage instructions, e.g.,

hashtag
.check(fn)

Check that certain conditions are met in the provided arguments.

fn is called with two arguments, the parsed argv hash and an array of options and their aliases.

If fn throws or returns a non-truthy value, show the thrown error, usage information, and exit.

hashtag
.fail(fn)

Method to execute when a failure occurs, rather then printing the failure message.

fn is called with the failure message that would have been printed.

hashtag
.boolean(key)

Interpret key as a boolean. If a non-flag option follows key in process.argv, that string won't get set as the value of key.

key will default to false, unless an default(key, undefined) is explicitly set.

If key is an Array, interpret all the elements as booleans.

hashtag
.string(key)

Tell the parser logic not to interpret key as a number or boolean. This can be useful if you need to preserve leading zeros in an input.

If key is an Array, interpret all the elements as strings.

.string('_') will result in non-hyphenated arguments being interpreted as strings, regardless of whether they resemble numbers.

hashtag
.array(key)

Tell the parser to interpret key as an array. If .array('foo') is set, --foo foo bar will be parsed as ['foo', 'bar'] rather than as 'bar'.

hashtag
.nargs(key, count)

The number of arguments that should be consumed after a key. This can be a useful hint to prevent parsing ambiguity:

parses as:

{ _: [], token: '-my-token', '$0': 'node test' }

Optionally .nargs() can take an object of key/narg pairs.

hashtag
.config(key)

Tells the parser that if the option specified by key is passed in, it should be interpreted as a path to a JSON config file. The file is loaded and parsed, and its properties are set as arguments.

hashtag
.wrap(columns)

Format usage output to wrap at columns many columns.

By default wrap will be set to Math.min(80, windowWidth). Use .wrap(null) to specify no column limit.

yargs.wrap(yargs.terminalWidth()) can be used to maximize the width of yargs' usage instructions.

hashtag
.strict()

Any command-line argument given that is not demanded, or does not have a corresponding description, will be reported as an error.

hashtag
.help([option, [description]])

Add an option (e.g., --help) that displays the usage string and exits the process. If present, the description parameter customises the description of the help option in the usage string.

If invoked without parameters, .help returns the generated usage string.

Example:

Later on, argv can be retrived with yargs.argv

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

Add an option (e.g., --version) that displays the version number (given by the version parameter) and exits the process. If present, the description parameter customizes the description of the version option in the usage string.

You can provide a function for version, rather than a string. This is useful if you want to use the version from your package.json:

hashtag
.showHelpOnFail(enable, [message])

By default, yargs outputs a usage string if any error is detected. Use the .showHelpOnFail method to customize this behaviour. if enable is false, the usage string is not output. If the message parameter is present, this message is output after the error message.

line_count.js

hashtag
.showHelp(consoleLevel='error')

Print the usage data using the function consoleLevel for printing.

Example:

Or, to print the usage data to stdout instead, you can specify the use of console.log:

Later on, argv can be retrived with yargs.argv

hashtag
.completion(cmd, [description], [fn]);

Enable bash-completion shortcuts for commands and options.

cmd: when present in argv._, will result in the .bashrc completion script being outputted. To enable bash completions, concat the generated script to your .bashrc, or .bash_profile.

description: provide a description in your usage instructions for the command that generates bash completion scripts.

fn, rather than relying on yargs' default completion functionlity, which shiver me timbers is pretty awesome, you can provide your own completion method.

But wait, there's more! you can provide asynchronous completions.

hashtag
.showCompletionScript()

Generate a bash completion script. Users of your application can install this script in their .bashrc, and yargs will provide completion shortcuts for commands and options.

hashtag
.exitProcess(enable)

By default, yargs exits the process when the user passes a help flag, uses the .version functionality or when validation fails. Calling .exitProcess(false) disables this behavior, enabling further actions after yargs have been validated.

hashtag
.parse(args)

Parse args instead of process.argv. Returns the argv object.

hashtag
.reset()

Reset the argument object built up so far. This is useful for creating nested command line interfaces.

hashtag
.argv

Get the arguments as a plain old object.

Arguments without a corresponding flag show up in the argv._ array.

The script name or node command is available at argv.$0 similarly to how $0 works in bash or perl.

hashtag
parsing tricks

hashtag
stop parsing

Use -- to stop parsing flags and stuff the remainder into argv._.

hashtag
negate fields

If you want to explicity set a field to false instead of just leaving it undefined or to override a default you can do --no-key.

hashtag
numbers

Every argument that looks like a number (!isNaN(Number(arg))) is converted to one. This way you can just net.createConnection(argv.port) and you can add numbers out of argv with + without having that mean concatenation, which is super frustrating.

hashtag
duplicates

If you specify a flag multiple times it will get turned into an array containing all the values in order.

hashtag
dot notation

When you use dots (.s) in argument names, an implicit object path is assumed. This lets you organize arguments into nested objects.

hashtag
short numbers

Short numeric head -n5 style argument work too:

hashtag
installation

With , just do:

or clone this project on github:

To run the tests with npm, just do:

hashtag
inspired by

This module is loosely inspired by Perl's .

Change Log

hashtag
v3.10.0 (2015/05/29 04:25 +00:00)

  • expose yargs.terminalWidth() thanks @ensonic (@bcoe)

#164arrow-up-right better array handling thanks @getify (@bcoe)

hashtag
v3.9.1 (2015/05/20 05:14 +00:00)

  • b6662b6arrow-up-right clarify .config() docs (@linclark)

  • 0291360arrow-up-right fixed tests, switched to nyc for coverage, fixed security issue, added Lin as collaborator (@bcoe)

hashtag
v3.9.0 (2015/05/10 18:32 +00:00)

  • #157arrow-up-right Merge pull request #157 from bcoe/command-yargs. allows handling of command specific arguments. Thanks for the suggestion @ohjames (@bcoe)

  • #158arrow-up-right Merge pull request #158 from kemitchell/spdx-license. Update license format (@kemitchell)

hashtag
v3.8.0 (2015/04/24 23:10 +00:00)

  • #154arrow-up-right showHelp's method signature was misleading fixes #153 (@bcoe)

  • #151arrow-up-right refactor yargs' table layout logic to use new helper library (@bcoe)

  • #150arrow-up-right Fix README example in argument requirements (@annonymouse)

hashtag
v3.7.2 (2015/04/13 11:52 -07:00)

  • 679fbbfarrow-up-right updated yargs to use the standardarrow-up-right style guide (agokjr)

  • [22382ee](https://github.com/bcoe/yargs/commit/22382ee9f5b495bc2586c1758cd1091cec3647f9arrow-up-right various bug fixes for $0 (@nylen)

hashtag
v3.7.1 (2015/04/10 11:06 -07:00)

  • 89e1992arrow-up-right detect iojs bin along with node bin. (@bcoe)

  • 755509earrow-up-right improvements to example documentation in README.md (@rstacruz)

  • 0d2dfc8arrow-up-right showHelp() no longer requires that .argv has been called (@bcoe)

hashtag
v3.7.0 (2015/04/04 02:29 -07:00)

  • 56cbe2darrow-up-right make .requiresArg() work with type hints. (@bcoe).

  • 2f5d562arrow-up-right serialize arrays and objects in usage strings. (@bcoe).

  • 5126304arrow-up-right be more lenient about alias/primary key ordering in chaining API. (@bcoe)

hashtag
v3.6.0 (2015/03/21 01:00 +00:00)

  • 4e24e22arrow-up-right support for .js configuration files. (@pirxpilot)

hashtag
v3.5.4 (2015/03/12 05:56 +00:00)

  • c16cc08arrow-up-right message for non-option arguments is now optional, thanks to (@raine)

hashtag
v3.5.3 (2015/03/09 06:14 +00:00)

  • 870b428arrow-up-right completion script was missing in package.json (@bcoe)

hashtag
v3.5.2 (2015/03/09 06:11 +00:00)

  • 58a4b24arrow-up-right parse was being called multiple times, resulting in strange behavior (@bcoe)

hashtag
v3.5.1 (2015/03/09 04:55 +00:00)

  • 4e588e0arrow-up-right accidentally left testing logic in (@bcoe)

hashtag
v3.5.0 (2015/03/09 04:49 +00:00)

  • 718bacdarrow-up-right added support for bash completions see #4 (@bcoe)

  • a192882arrow-up-right downgrade to mocha 2.1.0 until https://github.com/mochajs/mocha/issues/1585arrow-up-right can be sorted out (@bcoe)

hashtag
v3.4.7 (2015/03/09 04:09 +00:00)

  • 9845e5carrow-up-right the Argv singleton was not being updated when manually parsing arguments, fixes #114 (@bcoe)

hashtag
v3.4.6 (2015/03/09 04:01 +00:00)

  • 45b4c80arrow-up-right set placeholders for all keys fixes #115 (@bcoe)

hashtag
v3.4.5 (2015/03/01 20:31 +00:00)

  • a758e0barrow-up-right fix for count consuming too many arguments (@bcoe)

hashtag
v3.4.4 (2015/02/28 04:52 +00:00)

  • 0476af7arrow-up-right added nargs feature, allowing you to specify the number of arguments after an option (@bcoe)

  • 092477darrow-up-right updated README with full example of v3.0 API (@bcoe)

hashtag
v3.3.3 (2015/02/28 04:23 +00:00)

  • 0c4b769arrow-up-right remove string dependency, which conflicted with other libraries see #106 (@bcoe)

hashtag
v3.3.2 (2015/02/28 04:11 +00:00)

  • 2a98906arrow-up-right add $0 to epilog (@schnittstabil)

hashtag
v3.3.1 (2015/02/24 03:28 +00:00)

  • ad485cearrow-up-right fix for applying defaults to camel-case args (@bcoe)

hashtag
v3.3.0 (2015/02/24 00:49 +00:00)

  • 8bfe36darrow-up-right fix and document restart() command, as a tool for building nested CLIs (@bcoe)

hashtag
v3.2.1 (2015/02/22 05:45 +00:00)

  • 49a6d18arrow-up-right you can now provide a function that generates a default value (@bcoe)

hashtag
v3.2.0 (2015/02/22 05:24 +00:00)

  • 7a55886arrow-up-right improvements to yargs two-column text layout (@bcoe)

  • b6ab513arrow-up-right Tweak NPM version badge (@nylen)

hashtag
v3.1.0 (2015/02/19 19:37 +00:00)

  • 9bd2379arrow-up-right version now accepts a function, making it easy to load version #s from a package.json (@bcoe)

hashtag
v3.0.4 (2015/02/14 01:40 +00:00)

  • 0b7c19barrow-up-right various fixes for dot-notation handling (@bcoe)

hashtag
v3.0.3 (2015/02/14 00:59 +00:00)

  • c3f35e9arrow-up-right make sure dot-notation is applied to aliases (@bcoe)

hashtag
3.0.2 (2015/02/13 16:50 +00:00)

  • 74c8967arrow-up-right document epilog shorthand of epilogue. (@bcoe)

  • 670110farrow-up-right any non-truthy value now causes check to fail see #76 (@bcoe)

  • 0d8f791arrow-up-right finished implementing my wish-list of fetures for yargs 3.0. see #88 (@bcoe)

  • fix coverage. (@bcoe)

  • detect console width and perform word-wrapping. (@bcoe)

  • refactor two-column table layout so that we can use it for examples and usage (@bcoe)

  • major refactor of index.js, in prep for 3.x release. (@bcoe)

hashtag
v2.3.0 (2015/02/08 20:41 +00:00)

  • d824620arrow-up-right allow for undefined boolean defaults (@ashi009)

hashtag
v2.2.0 (2015/02/08 20:07 +00:00)

  • d6edd98arrow-up-right in-prep for further refactoring, and a 3.x release I've shuffled some things around and gotten test-coverage to 100%. (@bcoe)

hashtag
v2.1.2 (2015/02/08 06:05 +00:00)

  • d640745arrow-up-right switch to path.relative (@bcoe)

  • 3bfd41farrow-up-right remove mocha.opts. (@bcoe)

  • 47a2f35arrow-up-right document using .string('_') for string ids. see #56 (@bcoe)

  • Merge pull request #57 from eush77/option-readme (@eush77)

hashtag
v2.1.1 (2015/02/06 08:08 +00:00)

  • 01c6c61arrow-up-right fix for #71, 'newAliases' of undefined (@bcoe)

hashtag
v2.1.0 (2015/02/06 07:59 +00:00)

  • 6a1a3faarrow-up-right try to guess argument types, and apply sensible defaults see #73 (@bcoe)

hashtag
v2.0.1 (2015/02/06 07:54 +00:00)

  • 96a06b2arrow-up-right Fix for strange behavior with --sort option, see #51 (@bcoe)

hashtag
v2.0.0 (2015/02/06 07:45 +00:00)

  • 0250517arrow-up-right - 108fb84arrow-up-right fixed bug with boolean parsing, when bools separated by = see #66 (@bcoe)

  • a465a59arrow-up-right Add files field to the package.json (@shinnn)

  • 31043dearrow-up-right fix for yargs.argv having the same keys added multiple times see #63 (@bcoe)

  • Disable process.exit calls using .exitProcess(false) (@cianclarke)

  • Mention .option in README (@eush77)

hashtag
v1.3.2 (2014/10/06 21:56 +00:00)

  • b8d3472arrow-up-right 1.3.2 (@chevex)

hashtag
list (2014/08/30 18:41 +00:00)

  • fbc777farrow-up-right Now that yargs is the successor to optimist, I'm changing the README language to be more universal. Pirate speak isn't very accessible to non-native speakers. (@chevex)

  • a54d068arrow-up-right version output will not print extra newline (@boneskull)

  • 1cef5d6arrow-up-right Added contributors section to package.json (@chrisn)

  • Added 'require' and 'required' as synonyms for 'demand' (@chrisn)

  • Updating minimist. (@chevex)

  • Fix #31 (bad interaction between camelCase options and strict mode) (@nylen)

  • Added .help() and .version() methods (@chrisn)

  • Added .showHelpOnFail() method (@chrisn)

  • Allow boolean flag with .demand() (@chrisn)

  • Fixes issue #22. Arguments are no longer printed to the console when using .config. (@chevex)

  • Informing users that Yargs is the official optimist successor. (@chevex)

  • Merge pull request #24 from chrisn/strict (@chrisn)

  • Added requiresArg option, for options that require values (@chrisn)

  • Added .strict() method, to report error if unknown arguments are given (@chrisn)

  • Changed optimist to yargs in usage-options.js example (@chrisn)

  • Change optimist to yargs in examples (@chrisn)

  • Fix a couple of bad interactions between aliases and defaults (@nylen)

  • Document second argument of usage(message, opts) (@Gobie)

  • For "--some-option", also set argv.someOption (@nylen)

  • Finished porting unit tests to Mocha. (@chevex)

hashtag
v1.0.15 (2014/02/05 23:18 +00:00)

  • e2b1fc0arrow-up-right 1.0.15 update to badges (@chevex)

hashtag
v1.0.14 (2014/02/05 23:17 +00:00)

  • f33bbb0arrow-up-right Revert "Fixed issue which caused .demand function not to work correctly." (@chevex)

hashtag
v1.0.13 (2014/02/05 22:13 +00:00)

  • 6509e5earrow-up-right Fixed issue which caused .demand function not to work correctly. (@chevex)

hashtag
v1.0.12 (2013/12/13 00:09 +00:00)

  • 05eb267arrow-up-right 1.0.12 (@chevex)

hashtag
v1.0.11 (2013/12/13 00:07 +00:00)

  • c1bde46arrow-up-right 1.0.11 (@chevex)

hashtag
v1.0.10 (2013/12/12 23:57 +00:00)

  • dfebf81arrow-up-right Fixed formatting in README (@chevex)

hashtag
v1.0.9 (2013/12/12 23:47 +00:00)

  • 0b4e34aarrow-up-right Update README.md (@chevex)

hashtag
v1.0.8 (2013/12/06 16:36 +00:00)

  • #1arrow-up-right fix error caused by check() see #1 (@martinheidegger)

hashtag
v1.0.7 (2013/11/24 18:01 +00:00)

  • a247d88arrow-up-right Modified Pirate Joe image. (@chevex)

hashtag
v1.0.6 (2013/11/23 19:21 +00:00)

  • d7f69e1arrow-up-right Updated Pirate Joe image. (@chevex)

hashtag
v1.0.5 (2013/11/23 19:09 +00:00)

  • ece809carrow-up-right Updated readme notice again. (@chevex)

hashtag
v1.0.4 (2013/11/23 19:05 +00:00)

  • 9e81e81arrow-up-right Updated README with a notice about yargs being a fork of optimist and what that implies. (@chevex)

hashtag
v1.0.3 (2013/11/23 17:43 +00:00)

  • 65e7a78arrow-up-right Changed some small wording in README.md. (@chevex)

  • 459e20earrow-up-right Fix a bug in the options function, when string and boolean options weren't applied to aliases. (@shockone)

hashtag
v1.0.2 (2013/11/23 09:46 +00:00)

  • 3d80ebearrow-up-right 1.0.2 (@chevex)

hashtag
v1.0.1 (2013/11/23 09:39 +00:00)

  • f80ff36arrow-up-right Updated image. (@chevex)

hashtag
v1.0.0 (2013/11/23 09:33 +00:00)

  • 54e31d5arrow-up-right Rebranded from optimist to yargs in the spirit of the fork :D (@chevex)

  • 4ebb6c5arrow-up-right Added documentation for demandCount(). (@chevex)

  • 4561ce6arrow-up-right Simplified the error messages returned by .check(). (@chevex)

  • Fixed an issue with demand not accepting a zero value. (@chevex)

  • Add .fail(fn) so death isn't the only option. Should fix issue #39. (@chevex)

  • Added a few missing 'return self' (@chevex)

  • Fix showing help in certain JS environments. (@chevex)

  • Better string representation of default values. (@chevex)

  • Implies: conditional demands (@chevex)

  • Added support for JSON config files. (@chevex)

  • Add .example(cmd, desc) feature. (@chevex)

  • Added 'defaults' as alias to 'default' so as to avoid usage of a reserved keyword. (@chevex)

  • add .normalize(args..) support for normalizing paths (@chevex)

  • Customize error messages with demand(key, msg) (@chevex)

  • Merge branch 'rewrite-duplicate-test' of github.com:isbadawi/node-optimist (@chevex)

  • Pass aliases object to check functions for greater versatility. (@chevex)

  • Added ability to count boolean options and rolled minimist library back into project. (@chevex)

  • Fixed small typo. (@chevex)

  • Removed dependency on wordwrap module. (@chevex)

  • Merge branch 'master' of github.com:chbrown/node-optimist (@chevex)

  • Merge branch 'master' of github.com:seanzhou1023/node-optimist (@chevex)

  • Merge branch 'patch-1' of github.com:thefourtheye/node-optimist (@chevex)

  • Renamed README. (@chevex)

  • Renamed readme and added .gitignore. (@chevex)

  • Included examples for help and showHelp functions and fixed few formatting issues (@thefourtheye)

  • .alias({}) behaves differently based on mapping direction when generating descriptions (@chbrown)

  • Documented function signatures are useful for dynamically typed languages. (@chbrown)

hashtag
0.6.0 (2013/06/25 08:48 +00:00)

  • d37bfe0arrow-up-right all tests passing using minimist (@substack)

  • 76f1352arrow-up-right all parse tests now passing (@substack)

  • a7b6754arrow-up-right using minimist, some tests passing (@substack)

  • Give credit where its due (@DeadAlready)

  • v0.5.3 - Remove wordwrap as dependency (@DeadAlready)

hashtag
0.5.2 (2013/05/31 03:46 +00:00)

  • 4497ca5arrow-up-right fixed the whitespace bug without breaking anything else (@substack)

  • 5a3dd1aarrow-up-right failing test for whitespace arg (@substack)

hashtag
0.5.1 (2013/05/30 07:17 +00:00)

  • a20228farrow-up-right fix parse() to work with functions before it (@substack)

  • b13bd4carrow-up-right failing test for parse() with modifiers (@substack)

hashtag
0.5.0 (2013/05/18 21:59 +00:00)

  • c474a64arrow-up-right fixes for dash (@substack)

hashtag
0.4.0 (2013/04/13 19:03 +00:00)

  • dafe3e1arrow-up-right failing short test (@substack)

hashtag
0.3.7 (2013/04/04 04:07 +00:00)

  • 6c7a0ecarrow-up-right Fix for windows. On windows there is no _ in environment. (@hdf)

hashtag
0.3.6 (2013/04/04 04:04 +00:00)

  • e72346aarrow-up-right Add support for newlines in -a="" arguments (@danielbeardsley)

  • 71e1fb5arrow-up-right drop 0.4, add 0.8 to travis (@substack)

hashtag
0.3.5 (2012/10/10 11:09 +00:00)

  • ee692b3arrow-up-right Fix parsing booleans (@vojtajina)

  • 5045122arrow-up-right set $0 properly in the tests (@substack)

hashtag
0.3.4 (2012/04/30 06:54 +00:00)

  • f28c0e6arrow-up-right bump for string "true" params (@substack)

  • 8f44aebarrow-up-right Fix failing test for aliased booleans. (@coderarity)

  • b9f7b61arrow-up-right Add failing test for short aliased booleans. (@coderarity)

hashtag
0.3.3 (2012/04/30 06:45 +00:00)

  • 541bac8arrow-up-right Fixes #37.

hashtag
0.3.2 (2012/04/12 20:28 +00:00)

  • 3a0f014arrow-up-right travis badge (@substack)

  • 4fb60bfarrow-up-right Fix boolean aliases. (@coderarity)

  • f14dda5arrow-up-right Adjusted package.json to use tap (@jfhbrook)

  • test/usage.js no longer hangs (@jfhbrook)

  • two tests for combined boolean/alias opts parsing (@jfhbrook)

hashtag
0.3.1 (2011/12/31 08:44 +00:00)

  • d09b719arrow-up-right If "default" is set to false it was not passed on, fixed. (@wolframkriesing)

hashtag
0.3.0 (2011/12/09 06:03 +00:00)

  • 6e74aa7arrow-up-right bump and documented dot notation (@substack)

hashtag
0.2.7 (2011/10/20 02:25 +00:00)

  • 94adee2arrow-up-right argv. can be told 'Hey! argv.! Don't be messing with my args.', and it WILL obey (@colinta)

  • c46fdd5arrow-up-right optimistic critter image (@substack)

  • 5c95c73arrow-up-right alias options() to option() (@substack)

  • [fix] Fix for parsing boolean edge case (@indexzero)

  • [fix test] Update to ensure optimist is aware of default booleans. Associated tests included (@indexzero)

  • [dist test] Update devDependencies in package.json. Update test pathing to be more npm and require.paths future-proof (@indexzero)

  • s/sys/util/ (@substack)

  • update usage output (@substack)

  • some sage readme protips about parsing rules (@substack)

  • documented all the methods finally (@substack)

  • fenced syntax highlighting (@substack)

  • right-alignment of wrapped extra params (@substack)

  • now with .wrap() (@substack)

  • don't show 'Options:' when there aren't any (@substack)

  • failing test for multi-aliasing (@substack)

  • "Options:" > "options:" (@substack)

  • [minor] Update formatting for .showHelp() (@indexzero)

  • options works again, too lazy to write a proper test right now (@substack)

  • line_count_options example, which breaks (@substack)

  • line count example (@substack)

  • remove self.argv setting in boolean (@substack)

  • removed camel case for now (@substack)

  • remove dead longest checking code (@substack)

  • .help() too (@substack)

  • rm old help generator (@substack)

  • nub keys (@substack)

  • generate help message based on the previous calls, todo: nub (@substack)

  • stub out new showHelp, better checks (@substack)

  • let .options() take single options too (@substack)

  • .options() is now heaps simpler (@substack)

  • defaults work again, all tests pass (@substack)

  • update test error messages, down to 2 failing tests (@substack)

  • fix for bools doubling up, passes the parse test again, others fail (@substack)

  • refactored for an argv getter, failing several tests (@substack)

  • just rescan for now, alias test passes (@substack)

  • failing alias test (@substack)

  • .alias() (@substack)

  • [api] Remove .describe() in favor of building upon the existing .usage() API (@indexzero)

  • [doc api] Add .describe(), .options(), and .showHelp() methods along with example. (@indexzero)

  • updates for coffee since it now does argv the node way (@substack)

  • more general coffeescript detection (@substack)

  • Don't trigger the CoffeeScript hack when running under node_g. (@papandreou)

  • .string() but failing test (@substack)

  • test hex strings (@substack)

  • more keywords (@substack)

  • Added camelCase function that converts --multi-word-option to camel case (so it becomes argv.multiWordOption). (@papandreou)

  • fixed boolean bug by rescanning (@substack)

  • boolean examples (@substack)

  • boolean() with passing test (@substack)

  • coffee compatibility with node regex for versions too (@substack)

  • argv._ fixed by fixing the coffee detection (@substack)

  • whichNodeArgs test fails too (@substack)

  • replicated mnot's bug in whichNodeEmpty test (@substack)

  • test for ./bin usage (@substack)

  • don't coerce booleans to numbers (@substack)

  • package bump for automatic number conversion (@substack)

  • updated readme and examples with new auto-numberification goodness (@substack)

  • auto number conversion works yay (@substack)

  • failing test for not-implemented auto numification (@substack)

  • odd that eql doesn't check types careflly (@substack)

  • package author + keywords (@substack)

  • updated readme with .default() stuff (@substack)

  • passing tests for new .default() behavior (@substack)

  • new default() thing for setting default key/values (@substack)

  • test for coffee usage (@substack)

  • new --key value style with passing tests. NOTE: changes existing behavior (@substack)

  • package bump for summatix's coffee script fix (@substack)

  • Added support for CoffeeScript (@summatix)

  • test coverage for the falsy check() usage (@substack)

  • check bug fixed plus a handy string (@substack)

  • tests for demandCount, back up to 100% coverage (@substack)

  • call demandCount from demand (@substack)

  • add demandCount to check for the number of arguments in the _ list (@marshall)

  • Rebase checks. That will be its own module eventually. (@substack)

  • a $0 like in perl (@substack)

  • usage test hacking around process and console (@substack)

  • description pun (@substack)

  • mit/x11 license (@substack)

  • bool example is more consistent and also shows off short option grouping (@substack)

  • start of the readme and examples (@substack)

  • more tests for long and short captures (@substack)

  • silly bug in the tests with s/not/no/, all tests pass now (@substack)

  • hack an instance for process.argv onto Argv so the export can be called to create an instance or used for argv, which is the most common case (@substack)

  • divide example (@substack)

  • start of the lib with a package.json (@substack)

#165arrow-up-right
the issue listarrow-up-right
consolearrow-up-right
npmarrow-up-right
Getopt::Casualarrow-up-right
Joe was one optimistic pirate.
#!/usr/bin/env node
var argv = require('yargs').argv;

if (argv.ships > 3 && argv.distance < 53.5) {
    console.log('Plunder more riffiwobbles!');
}
else {
    console.log('Retreat from the xupptumblers!');
}
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!

$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!
#!/usr/bin/env node
var argv = require('yargs').argv;
console.log('(%d,%d)', argv.x, argv.y);
$ ./short.js -x 10 -y 21
(10,21)
#!/usr/bin/env node
var util = require('util');
var argv = require('yargs').argv;

if (argv.s) {
    util.print(argv.fr ? 'Le perroquet dit: ' : 'The parrot says: ');
}
console.log(
    (argv.fr ? 'couac' : 'squawk') + (argv.p ? '!' : '')
);
$ ./bool.js -s
The parrot says: squawk

$ ./bool.js -sp
The parrot says: squawk!

$ ./bool.js -sp --fr
Le perroquet dit: couac!
#!/usr/bin/env node
var argv = require('yargs').argv;
console.log('(%d,%d)', argv.x, argv.y);
console.log(argv._);
$ ./nonopt.js -x 6.82 -y 3.35 rum
(6.82,3.35)
[ 'rum' ]

$ ./nonopt.js "me hearties" -x 0.54 yo -y 1.12 ho
(0.54,1.12)
[ 'me hearties', 'yo', 'ho' ]
#!/usr/bin/env node
var argv = require('yargs')
    .count('verbose')
    .alias('v', 'verbose')
    .argv;

VERBOSE_LEVEL = argv.verbose;

function WARN()  { VERBOSE_LEVEL >= 0 && console.log.apply(console, arguments); }
function INFO()  { VERBOSE_LEVEL >= 1 && console.log.apply(console, arguments); }
function DEBUG() { VERBOSE_LEVEL >= 2 && console.log.apply(console, arguments); }

WARN("Showing only important stuff");
INFO("Showing semi-mportant stuff too");
DEBUG("Extra chatty mode");
$ node count.js
Showing only important stuff

$ node count.js -v
Showing only important stuff
Showing semi-important stuff too

$ node count.js -vv
Showing only important stuff
Showing semi-important stuff too
Extra chatty mode

$ node count.js -v --verbose
Showing only important stuff
Showing semi-important stuff too
Extra chatty mode
#!/usr/bin/env node
var argv = require('yargs')
    .usage('Usage: $0 -w [num] -yh[num]')
    .demand(['w','h'])
    .argv;

console.log("The area is:", argv.w * argv.h);
$ ./area.js -w 55 -h 11
605

$ node ./area.js -w 4.91 -w 2.51
Usage: node ./area.js -w [num] -h [num]

Options:
  -w  [required]
  -h  [required]

Missing required arguments: h
#!/usr/bin/env node
var argv = require('yargs')
    .demand(2)
    .argv;
console.dir(argv)
$ ./demand_count.js a
Not enough arguments, expected 2, but only found 1
$ ./demand_count.js a b
{ _: [ 'a', 'b' ], '$0': 'node ./demand_count.js' }
$ ./demand_count.js a b c
{ _: [ 'a', 'b', 'c' ], '$0': 'node ./demand_count.js' }
#!/usr/bin/env node
var argv = require('yargs')
    .default('x', 10)
    .default('y', 10)
    .argv
;
console.log(argv.x + argv.y);
$ ./default_singles.js -x 5
15
#!/usr/bin/env node
var argv = require('yargs')
    .default({ x : 10, y : 10 })
    .argv
;
console.log(argv.x + argv.y);
$ ./default_hash.js -y 7
17
#!/usr/bin/env node
var argv = require('yargs')
    .boolean('v')
    .argv
;
console.dir(argv.v);
console.dir(argv._);
$ ./boolean_single.js -v "me hearties" yo ho
true
[ 'me hearties', 'yo', 'ho' ]
#!/usr/bin/env node
var argv = require('yargs')
    .boolean(['x','y','z'])
    .argv
;
console.dir([ argv.x, argv.y, argv.z ]);
console.dir(argv._);
$ ./boolean_double.js -x -z one two three
[ true, false, true ]
[ 'one', 'two', 'three' ]
#!/usr/bin/env node
var argv = require('yargs')
    .usage('Usage: $0 <command> [options]')
    .command('count', 'Count the lines in a file')
    .demand(1)
    .example('$0 count -f foo.js', 'count the lines in the given file')
    .demand('f')
    .alias('f', 'file')
    .nargs('f', 1)
    .describe('f', 'Load a file')
    .help('h')
    .alias('h', 'help')
    .epilog('copyright 2015')
    .argv;

var fs = require('fs');
var s = fs.createReadStream(argv.file);

var lines = 0;
s.on('data', function (buf) {
    lines += buf.toString().match(/\n/g).length;
});

s.on('end', function () {
    console.log(lines);
});
$ node line_count.js count
Usage: node test.js <command> [options]

Commands:
  count    Count the lines in a file

Options:
  -f, --file  Load a file        [required]
  -h, --help  Show help

Examples:
  node test.js count -f foo.js    count the lines in the given file

copyright 2015

Missing required arguments: f

$ node line_count.js count --file line_count.js
20

$ node line_count.js count -f line_count.js
20
require('yargs').argv
`
require('yargs')([ '-x', '1', '-y', '2' ]).argv
require('yargs').parse([ '-x', '1', '-y', '2' ])
var argv = require('yargs')
  .default('random', function randomValue() {
    return Math.random() * 256;
  }).argv;
.default('timeout', 60000, '(one-minute)');
var argv = require('yargs')
    .option('f', {
        alias : 'file',
        demand: true,
        default: '/etc/passwd',
        describe: 'x marks the spot',
        type: 'string'
    })
    .argv
;
var argv = require('yargs')
    .alias('f', 'file')
    .default('f', '/etc/passwd')
    .argv
;
var argv = require('yargs')
    .options({
      'f': {
        alias: 'file',
        demand: true,
        default: '/etc/passwd',
        describe: 'x marks the spot',
        type: 'string'
      }
    })
    .argv
;
var argv = require('yargs')
  .usage('npm <command>')
  .command('install', 'tis a mighty fine package to install')
  .command('publish', 'shiver me timbers, should you be sharing all that', function (yargs) {
    argv = yargs.option('f', {
      alias: 'force',
      description: 'yar, it usually be a bad idea'
    })
    .help('help')
    .argv
  })
  .help('help')
  .argv;
var argv = require('yargs')
  .epilogue('for more information, find our manual at http://example.com');
var argv = require('yargs')
  .nargs('token', 1)
  .parse(['--token', '-my-token']);
var yargs = require("yargs")
       .usage("$0 -operand1 number -operand2 number -operation [add|subtract]");
console.log(yargs.help());
var argv = require('yargs')
  .version(function() {
    return require('../package').version;
  })
  .argv;
#!/usr/bin/env node
var argv = require('yargs')
    .usage('Count the lines in a file.\nUsage: $0')
    .demand('f')
    .alias('f', 'file')
    .describe('f', 'Load a file')
    .showHelpOnFail(false, "Specify --help for available options")
    .argv;

// etc.
$ node line_count.js --file
Missing argument value: f

Specify --help for available options
var yargs = require("yargs")
       .usage("$0 -operand1 number -operand2 number -operation [add|subtract]");
yargs.showHelp();
yargs.showHelp("log");
var argv = require('yargs')
  .completion('completion', function(current, argv) {
    // 'current' is the current command being completed.
    // 'argv' is the parsed arguments so far.
    // simply return an array of completions.
    return [
      'foo',
      'bar'
    ];
  })
  .argv;
var argv = require('yargs')
  .completion('completion', function(current, argv, done) {
    setTimeout(function() {
      done([
        'apple',
        'banana'
      ]);
    }, 500);
  })
  .argv;
var yargs = require('./yargs')
  .usage('$0 command')
  .command('hello', 'hello command')
  .command('world', 'world command')
  .demand(1, 'must provide a valid command'),
  argv = yargs.argv,
  command = argv._[0];

if (command === 'hello') {
  yargs.reset()
    .usage('$0 hello')
    .help('h')
    .example('$0 hello', 'print the hello message!')
    .argv

  console.log('hello!');
} else if (command === 'world'){
  yargs.reset()
    .usage('$0 world')
    .help('h')
    .example('$0 world', 'print the world message!')
    .argv

  console.log('world!');
} else {
  yargs.showHelp();
}
$ node examples/reflect.js -a 1 -b 2 -- -c 3 -d 4
{ _: [ '-c', '3', '-d', '4' ],
  '$0': 'node ./examples/reflect.js',
  a: 1,
  b: 2 }
$ node examples/reflect.js -a --no-b
{ _: [],
  '$0': 'node ./examples/reflect.js',
  a: true,
  b: false }
$ node examples/reflect.js -x 5 -x 8 -x 0
{ _: [],
  '$0': 'node ./examples/reflect.js',
    x: [ 5, 8, 0 ] }
 $ node examples/reflect.js --foo.bar.baz=33 --foo.quux=5
 { _: [],
   '$0': 'node ./examples/reflect.js',
     foo: { bar: { baz: 33 }, quux: 5 } }
$ node reflect.js -n123 -m456
{ '3': true,
  '6': true,
  _: [],
  '$0': 'node ./reflect.js',
  n: 123,
  m: 456 }
npm install yargs
git clone http://github.com/bcoe/yargs.git
npm test
5768447arrow-up-right
82e793farrow-up-right
67476b3arrow-up-right
4724cdfarrow-up-right
#57arrow-up-right
2d68c5barrow-up-right
45da9ecarrow-up-right
cc295c0arrow-up-right
d0bf951arrow-up-right
c15f8e7arrow-up-right
d991b9barrow-up-right
e8c8aa4arrow-up-right
e855af4arrow-up-right
14dbec2arrow-up-right
bef74fcarrow-up-right
#24arrow-up-right
889a2b2arrow-up-right
eb16369arrow-up-right
0471c3farrow-up-right
5c88f74arrow-up-right
66f12c8arrow-up-right
8fa1d80arrow-up-right
56e6528arrow-up-right
ed5f6d3arrow-up-right
661c678arrow-up-right
731dd3carrow-up-right
fa15417arrow-up-right
e655e4darrow-up-right
a746a31arrow-up-right
6134619arrow-up-right
046b93barrow-up-right
a677ec0arrow-up-right
1bd4375arrow-up-right
6b753c1arrow-up-right
33d7d59arrow-up-right
647d37farrow-up-right
9059d1aarrow-up-right
623dc26arrow-up-right
49f0dcearrow-up-right
79ec980arrow-up-right
ea14630arrow-up-right
2b75da2arrow-up-right
d9bda11arrow-up-right
d6cc606arrow-up-right
9498d3farrow-up-right
bbd1fe3arrow-up-right
37fea04arrow-up-right
855b20darrow-up-right
6655688arrow-up-right
602a2a9arrow-up-right
88e5d32arrow-up-right
e1e740carrow-up-right
f7692eaarrow-up-right
d1f92d1arrow-up-right
b01bda8arrow-up-right
aa753e7arrow-up-right
7bfce2farrow-up-right
d420a7aarrow-up-right
cf86eedarrow-up-right
5da9f7aarrow-up-right
8ca6879arrow-up-right
b72bacfarrow-up-right
2b980bfarrow-up-right
d614f63arrow-up-right
691eda3arrow-up-right
0826c9farrow-up-right
72f7490arrow-up-right
75aeccearrow-up-right
f742e54arrow-up-right
4ca06b8arrow-up-right
eeb8423arrow-up-right
6903412arrow-up-right
5a0d88barrow-up-right
d782170arrow-up-right
622ec17arrow-up-right
7c8baacarrow-up-right
8197785arrow-up-right
3ffbdc3arrow-up-right
d4e21f5arrow-up-right
3c4cf29arrow-up-right
89f0d04arrow-up-right
dd87333arrow-up-right
53f7bc6arrow-up-right
2213e2darrow-up-right
d1e7379arrow-up-right
b2f8c99arrow-up-right
d0c0174arrow-up-right
d85f431arrow-up-right
edbd527arrow-up-right
be4902farrow-up-right
e24cb23arrow-up-right
78ac753arrow-up-right
bcfe973arrow-up-right
1987acaarrow-up-right
ef36db3arrow-up-right
cc53c56arrow-up-right
60b57daarrow-up-right
dff6d07arrow-up-right
0e380b9arrow-up-right
62644d4arrow-up-right
430fafcarrow-up-right
343b8afarrow-up-right
63df2f3arrow-up-right
35473a4arrow-up-right
13df151arrow-up-right
85f8007arrow-up-right
8f17014arrow-up-right
73dc901arrow-up-right
bcec56barrow-up-right
ebd2844arrow-up-right
fd854b0arrow-up-right
656a1d5arrow-up-right
cd7f8c5arrow-up-right
932725earrow-up-right
4e6c7abarrow-up-right
d54ffccarrow-up-right
ed2a2d5arrow-up-right
75a975earrow-up-right
56b2b1darrow-up-right
a4843a9arrow-up-right
857bd2darrow-up-right
073b776arrow-up-right
4bd4b7aarrow-up-right
b8689acarrow-up-right
e688370arrow-up-right
2e5e196arrow-up-right
fcc3521arrow-up-right
87a1fe2arrow-up-right
8d089d2arrow-up-right
448d747arrow-up-right
da74deaarrow-up-right
ab6387earrow-up-right
102496aarrow-up-right
a01caebarrow-up-right
443da55arrow-up-right
Coverage Status
NPM version
Build Status
Dependency Status