diff --git a/README.md b/README.md index 7df675d5c..9407ba5ee 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,22 @@ Yargs helps you build interactive command line tools, by parsing arguments and g It gives you: * commands and (grouped) options (`my-program.js serve --port=5000`). -* a dynamically generated help menu based on your arguments. +* a dynamically generated help menu based on your arguments: -> +``` +mocha [spec..] + +Run tests with Mocha + +Commands + mocha inspect [spec..] Run tests with Mocha [default] + mocha init create a client-side Mocha setup at + +Rules & Behavior + --allow-uncaught Allow uncaught errors to propagate [boolean] + --async-only, -A Require all tests to use a callback (async) or + return a Promise [boolean] +``` * bash-completion shortcuts for commands and options. * and [tons more](/docs/api.md). @@ -46,7 +59,9 @@ npm i yargs@next ```javascript #!/usr/bin/env node -const {argv} = require('yargs') +const yargs = require('yargs/yargs') +const { hideBin } = require('yargs/helpers') +const argv = yargs(hideBin(process.argv)).argv if (argv.ships > 3 && argv.distance < 53.5) { console.log('Plunder more riffiwobbles!') @@ -67,7 +82,10 @@ Retreat from the xupptumblers! ```javascript #!/usr/bin/env node -require('yargs') // eslint-disable-line +const yargs = require('yargs/yargs') +const { hideBin } = require('yargs/helpers') + +yargs(hideBin(process.argv)) .command('serve [port]', 'start the server', (yargs) => { yargs .positional('port', { @@ -108,7 +126,7 @@ As of `v16`, `yargs` supports [Deno](https://github.com/denoland/deno): import yargs from 'https://deno.land/x/yargs/deno.ts' import { Arguments, YargsType } from 'https://deno.land/x/yargs/types.ts' -yargs() +yargs(Deno.args) .command('download ', 'download a list of files', (yargs: YargsType) => { return yargs.positional('files', { describe: 'a list of files to do something with' @@ -118,7 +136,7 @@ yargs() }) .strictCommands() .demandCommand(1) - .parse(Deno.args) + .argv ``` ### ESM diff --git a/docs/advanced.md b/docs/advanced.md index e07b6b4c9..84d3e3e9b 100644 --- a/docs/advanced.md +++ b/docs/advanced.md @@ -14,7 +14,7 @@ commands. tldr; default commands allow you to define the entry point to your application using a similar API to subcommands. ```js -const argv = require('yargs') +const argv = require('yargs/yargs')(process.argv.slice(2)) .command('$0', 'the default command', () => {}, (argv) => { console.log('this command will be run by default') }) @@ -27,7 +27,7 @@ is run with `./my-cli.js --x=22`. Default commands can also be used as a command alias, like so: ```js -const argv = require('yargs') +const argv = require('yargs/yargs')(process.argv.slice(2)) .command(['serve', '$0'], 'the serve command', () => {}, (argv) => { console.log('this command will be run by default') }) @@ -124,7 +124,7 @@ line, the command will be executed. ```js #!/usr/bin/env node -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .command(['start [app]', 'run', 'up'], 'Start up an app', {}, (argv) => { console.log('starting up the', argv.app || 'default', 'app') }) @@ -305,7 +305,7 @@ cli.js: ```js #!/usr/bin/env node -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .commandDir('cmds') .demandCommand() .help() @@ -383,7 +383,7 @@ const findUp = require('find-up') const fs = require('fs') const configPath = findUp.sync(['.myapprc', '.myapprc.json']) const config = configPath ? JSON.parse(fs.readFileSync(configPath)) : {} -const argv = require('yargs') +const argv = require('yargs/yargs')(process.argv.slice(2)) .config(config) .argv ``` @@ -411,7 +411,7 @@ Yargs gives you this functionality using the [`pkgConf()`](/docs/api.md#config) method: ```js -const argv = require('yargs') +const argv = require('yargs/yargs')(process.argv.slice(2)) .pkgConf('nyc') .argv ``` @@ -521,7 +521,7 @@ yargs.middleware(normalizeCredentials) #### yargs parsing configuration ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Usage: $0 [options]') .command('login', 'Authenticate user', (yargs) =>{ return yargs.option('username') @@ -533,12 +533,3 @@ var argv = require('yargs') ) .argv; ``` - -### Using the non-singleton interface - -To use yargs without running as a singleton, do: -```js -const argv = require('yargs/yargs')(process.argv.slice(2)) -``` - -This is especially useful when using yargs in a library, as library authors should not pollute the global state. diff --git a/docs/api.md b/docs/api.md index 734155c21..7cb20eda6 100644 --- a/docs/api.md +++ b/docs/api.md @@ -14,29 +14,29 @@ This document is the Yargs API reference. There are more documentation files in API reference === -You can run Yargs without any configuration, and it will do its -best to parse `process.argv`: +You can pass Yargs the `process.argv` without any additional configuration +and it will do its best to parse it into an object: ```javascript -require('yargs').argv +require('yargs/yargs')(process.argv.slice(2)).argv ``` -You can also pass in the arguments yourself: +You can also pass in an arbitrary array of arguments: ```javascript -require('yargs')([ '-x', '1', '-y', '2' ]).argv +require('yargs/yargs')([ '-x', '1', '-y', '2' ]).argv ``` or use `.parse()` to do the same thing: ```javascript -require('yargs').parse([ '-x', '1', '-y', '2' ]) +require('yargs/yargs')().parse([ '-x', '1', '-y', '2' ]) ``` -Calling `.parse()` with no arguments is equivalent to calling `yargs.argv`: +Calling `.parse()` with no arguments is equivalent to calling `.argv`: ```javascript -require('yargs').parse() +require('yargs/yargs')(process.argv.slice(2)).parse() ``` When passing in the arguments yourself, note that Yargs expects the passed array @@ -47,6 +47,14 @@ starts with two extra elements:`process.execPath` and the path to the JavaScript file being executed. So if you’re getting your arguments from `process.argv` in Node, pass `process.argv.slice(2)` to Yargs. +***Note:*** Yargs exposes the helper `hideBin`, which handles the +`process.argv.slice` logic for you. + +```javascript +const { hideBin } = require('yargs/helpers') +const argv = yargs(hideBin(process.argv)).argv +``` + The rest of these methods below come in just before the terminating `.argv` or terminating `.parse()`. @@ -118,7 +126,7 @@ failed check. at the top-level and for each sub-command. ```js -const argv = require('yargs') +const argv = require('yargs/yargs')(process.argv.slice(2)) .check((argv, options) => { const filePaths = argv._ if (filePaths.length > 1) { @@ -137,7 +145,7 @@ Limit valid values for `key` to a predefined set of `choices`, given as an array or as an individual value. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .alias('i', 'ingredient') .describe('i', 'choose your sandwich ingredients') .choices('i', ['peanut-butter', 'jelly', 'banana', 'pickles']) @@ -155,7 +163,7 @@ choices. Choices can also be specified as `choices` in the object given to `option()`. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .option('size', { alias: 's', describe: 'choose a size', @@ -185,7 +193,7 @@ all other modifications, such as [`.normalize()`](#normalize). _Examples:_ ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .coerce('file', function (arg) { return require('fs').readFileSync(arg, 'utf8') }) @@ -196,7 +204,7 @@ Optionally `.coerce()` can take an object that maps several keys to their respective coercion function. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .coerce({ date: Date.parse, json: JSON.parse @@ -209,7 +217,7 @@ array of keys as the first argument to `.coerce()`: ```js var path = require('path') -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .coerce(['src', 'dest'], path.resolve) .argv ``` @@ -220,7 +228,7 @@ coercion will be applied to the final object that has been parsed: ```js // --user.name Batman --user.password 123 // gives us: {name: 'batman', password: '[SECRET]'} -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .option('user') .coerce('user', opt => { opt.name = opt.name.toLowerCase() @@ -347,7 +355,7 @@ If invoked without parameters, `.completion()` will make `completion` the comman the completion script. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .completion('completion', function(current, argv) { // 'current' is the current command being completed. // 'argv' is the parsed arguments so far. @@ -363,7 +371,7 @@ var argv = require('yargs') You can also provide asynchronous completions. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .completion('completion', function(current, argv, done) { setTimeout(function() { done([ @@ -378,7 +386,7 @@ var argv = require('yargs') But wait, there's more! You can return an asynchronous promise. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .completion('completion', function(current, argv) { return new Promise(function (resolve, reject) { setTimeout(function () { @@ -410,7 +418,7 @@ function must be synchronous, and should return an object containing key value pairs or an error. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .config('settings', function (configPath) { return JSON.parse(fs.readFileSync(configPath, 'utf-8')) }) @@ -421,7 +429,7 @@ You can also pass an explicit configuration `object`, it will be parsed and its properties will be set as arguments. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .config({foo: 1, bar: 2}) .argv console.log(argv) @@ -501,7 +509,7 @@ 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: ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .default('random', function randomValue() { return Math.random() * 256; }).argv; @@ -534,7 +542,7 @@ If a `msg` string is given, it will be printed when the argument is missing, ins ```javascript // demand an array of keys to be provided -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .option('run', { alias: 'r', describe: 'run your program' @@ -568,7 +576,7 @@ this is useful when using `.options()` to specify command line parameters. ```javascript // demand individual options within the option constructor -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .options({ 'run': { alias: 'r', @@ -606,7 +614,7 @@ Missing required arguments: run, path Demand in context of commands. You can demand a minimum and a maximum number a user can have within your program, as well as provide corresponding error messages if either of the demands is not met. ```javascript -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .command({ command: 'configure [value]', aliases: ['config', 'cfg'], @@ -644,7 +652,7 @@ expected value._ Shows a `[deprecated]` notice in front of the option. ```javascript -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .option('old') .deprecateOption('old') .option('new') @@ -658,7 +666,7 @@ Options: You can also specify a message ```javascript -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .option('old') .deprecateOption('old', 'use --new') .option('new') @@ -672,7 +680,7 @@ Options: You can also use it within the option constructor ```javascript -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .option('old', { deprecated: true }) ``` @@ -713,7 +721,7 @@ Program arguments are defined in this order of precedence: 4. Configured defaults ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .env('MY_PROGRAM') .option('f', { alias: 'fruit-thing', @@ -761,7 +769,7 @@ by calling `.env(false)`, e.g. if you need to undo previous configuration. A message to print at the end of the usage instructions, e.g. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .epilogue('for more information, find our manual at http://example.com'); ``` @@ -777,7 +785,7 @@ Examples will be printed out as part of the help message. If you want to add multiple examples at once, just pass an array of examples, e.g ```js -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .example([ ['$0 --config "~/config.json"', 'Use custom config'], ['$0 --safe', 'Start in safe mode'] @@ -811,7 +819,7 @@ Method to execute when a failure occurs, rather than printing the failure messag occured. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .fail(function (msg, err, yargs) { if (err) throw err // preserve stack console.error('You broke it!') @@ -834,7 +842,7 @@ Allows to programmatically get completion choices for any line. For example: ```js -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .option('foobar') .option('foobaz') .completion() @@ -852,7 +860,7 @@ Indicate that an option (or group of options) should not be reset when a command is executed, as an example: ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .option('a', { alias: 'all', default: true, @@ -885,7 +893,7 @@ Given a key, or an array of keys, places options under an alternative heading when displaying usage instructions, e.g., ```js -var yargs = require('yargs')(['--help']) +require('yargs/yargs')(['--help']) .help() .group('batman', 'Heroes:') .describe('batman', "world's greatest detective") @@ -958,7 +966,7 @@ locale. Note that the OS locale can be modified by setting/exporting the `LC_ALL environment variable. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('./$0 - follow ye instructions true') .option('option', { alias: 'o', @@ -1061,7 +1069,7 @@ handler function. ```js // populating home directory from an environment variable. -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .middleware(function (argv) { if (process.env.HOME) argv.home = process.env.HOME }, true) @@ -1086,7 +1094,7 @@ The number of arguments that should be consumed after a key. This can be a useful hint to prevent parsing ambiguity. For example: ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .nargs('token', 1) .parse(['--token', '-my-token']); ``` @@ -1118,7 +1126,7 @@ be populated with `NaN`. Note that decimals, hexadecimals, and scientific notation are all accepted. ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .number('n') .number(['width', 'height']) .argv @@ -1153,7 +1161,7 @@ customization, like `.alias()`, `.demandOption()` etc. for that option. For example: ```javascript -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .option('f', { alias: 'file', demandOption: true, @@ -1168,7 +1176,7 @@ var argv = require('yargs') is the same as ```javascript -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .alias('f', 'file') .demandOption('f') .default('f', '/etc/passwd') @@ -1181,7 +1189,7 @@ var argv = require('yargs') Optionally `.options()` can take an object that maps keys to `opt` parameters. ```javascript -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .options({ 'f': { alias: 'file', @@ -1324,7 +1332,7 @@ available on the top-level yargs instance. [default commands](/docs/advanced.md#default-commands)._ ```js -const argv = require('yargs')('run --help') +const argv = require('yargs/yargs')('run --help') .command('run ', 'run the server', (yargs) => { yargs.positional('guid', { describe: 'a unique identifier for the server', @@ -1381,7 +1389,7 @@ creating nested command line interfaces. Use [global](#global) to specify keys that should not be reset. ```js -var yargs = require('yargs') +var yargs = require('yargs/yargs')(process.argv.slice(2)) .usage('$0 command') .command('hello', 'hello command') .command('world', 'world command') @@ -1471,7 +1479,7 @@ line_count.js: ```javascript #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Count the lines in a file.\nUsage: $0 -f ') .demandOption('f') .alias('f', 'file') @@ -1564,7 +1572,7 @@ Override the default strings used by yargs with the key/value pairs provided in `obj`: ```js -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .command('run', 'the run command') .help('help') .updateStrings({ @@ -1601,7 +1609,7 @@ acts an an alias for [`.command()`](#command). This allows you to use to provide configuration for the positional arguments accepted by your program: ```js -const argv = require('yargs') +const argv = require('yargs/yargs')(process.argv.slice(2)) .usage('$0 ', 'start the application server', (yargs) => { yargs.positional('port', { describe: 'the port that your application should bind to', diff --git a/docs/bundling.md b/docs/bundling.md index 63f40c362..9a035d1ec 100644 --- a/docs/bundling.md +++ b/docs/bundling.md @@ -16,9 +16,9 @@ If you are targetting Node.js with your bundle, we recommend using Given a CommonJS file, **index.js**: ```js -const yargs = require('yargs') +const yargs = require('yargs/yargs') const chalk = require('chalk') -yargs +require('yargs/yargs')(process.argv.slice(2)) .option('awesome-opt', { describe: `my awesome ${chalk.green('option')}` }) @@ -32,9 +32,9 @@ You can simply run: `ncc build index.js`. Given a CommonJS file, **index.js**: ```js -const yargs = require('yargs') +const yargs = require('yargs/yargs') const chalk = require('chalk') -yargs +require('yargs/yargs')(process.argv.slice(2)) .option('awesome-opt', { describe: `my awesome ${chalk.green('option')}` }) diff --git a/docs/examples.md b/docs/examples.md index 9770acb3a..72f3f207f 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -11,7 +11,7 @@ plunder.js: ```javascript #!/usr/bin/env node -var argv = require('yargs').argv; +var argv = require('yargs/yargs')(process.argv.slice(2)).argv; if (argv.ships > 3 && argv.distance < 53.5) { console.log('Plunder more riffiwobbles!'); @@ -35,7 +35,7 @@ short.js: ```javascript #!/usr/bin/env node -var argv = require('yargs').argv; +var argv = require('yargs/yargs')(process.argv.slice(2)).argv; console.log('(%d,%d)', argv.x, argv.y); ``` @@ -51,7 +51,7 @@ bool.js: ```javascript #!/usr/bin/env node -var argv = require('yargs').argv; +var argv = require('yargs/yargs')(process.argv.slice(2)).argv; if (argv.s) { process.stdout.write(argv.fr ? 'Le perroquet dit: ' : 'The parrot says: '); @@ -79,7 +79,7 @@ nonopt.js: ```javascript #!/usr/bin/env node -var argv = require('yargs').argv; +var argv = require('yargs/yargs')(process.argv.slice(2)).argv; console.log('(%d,%d)', argv.x, argv.y); console.log(argv._); ``` @@ -101,7 +101,7 @@ count.js: ```javascript #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .count('verbose') .alias('v', 'verbose') .argv; @@ -142,7 +142,7 @@ area.js: ```javascript #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Usage: $0 -w [num] -h [num]') .demandOption(['w','h']) .argv; @@ -171,7 +171,7 @@ demand_count.js: ```javascript #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .demandCommand(2) .argv; console.dir(argv); @@ -196,7 +196,7 @@ default_singles.js: ```javascript #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .default('x', 10) .default('y', 10) .argv @@ -213,7 +213,7 @@ default_hash.js: ```javascript #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .default({ x : 10, y : 10 }) .argv ; @@ -232,7 +232,7 @@ boolean_single.js: ```javascript #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .boolean(['r','v']) .argv ; @@ -251,7 +251,7 @@ boolean_double.js: ```javascript #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .boolean(['x','y','z']) .argv ; @@ -275,7 +275,7 @@ line_count.js: ```javascript #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Usage: $0 [options]') .command('count', 'Count the lines in a file') .example('$0 count -f foo.js', 'count the lines in the given file') diff --git a/docs/typescript.md b/docs/typescript.md index 5baf9b8f1..6fee6047f 100644 --- a/docs/typescript.md +++ b/docs/typescript.md @@ -7,9 +7,9 @@ The following `.options()` definition: ```typescript #!/usr/bin/env node -import yargs = require('yargs'); +import yargs = require('yargs/yargs'); -const argv = yargs.options({ +const argv = yargs(process.argv.slice(2)).options({ a: { type: 'boolean', default: false }, b: { type: 'string', demandOption: true }, c: { type: 'number', alias: 'chill' }, diff --git a/example/bool.js b/example/bool.js index 3ef4b3412..94b75f849 100644 --- a/example/bool.js +++ b/example/bool.js @@ -1,6 +1,6 @@ #!/usr/bin/env node var util = require('util'); -var argv = require('yargs').argv; +var argv = require('yargs/yargs')(process.argv.slice(2)).argv; if (argv.s) { console.log(argv.fr ? 'Le chat dit: ' : 'The cat says: '); diff --git a/example/boolean_double.js b/example/boolean_double.js index f948c85f5..6d7e98179 100644 --- a/example/boolean_double.js +++ b/example/boolean_double.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .boolean(['x','y','z']) .argv ; diff --git a/example/boolean_single.js b/example/boolean_single.js index 51cf3cbec..a3c17921a 100644 --- a/example/boolean_single.js +++ b/example/boolean_single.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .boolean(['r','v']) .argv ; diff --git a/example/command_hierarchy.js b/example/command_hierarchy.js index 850d4bad7..e096af6e2 100644 --- a/example/command_hierarchy.js +++ b/example/command_hierarchy.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -require('yargs') +require('yargs/yargs')(process.argv.slice(2)) .commandDir('cmds') .demandCommand() .help() diff --git a/example/complex.js b/example/complex.js index 7ce0ac77b..6c2db13de 100644 --- a/example/complex.js +++ b/example/complex.js @@ -1,5 +1,5 @@ // a fairly complex CLI defined using the yargs 3.0 API: -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Usage: $0 [options]') // usage string of application. .command('install', 'install a package (name@version)') // describe commands available. .command('publish', 'publish the package inside the current working directory') diff --git a/example/count.js b/example/count.js index 8bd494770..21dcc6831 100644 --- a/example/count.js +++ b/example/count.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .count('verbose') .alias('v', 'verbose') .argv; diff --git a/example/default_hash.js b/example/default_hash.js index de9f82079..aa72e4daf 100644 --- a/example/default_hash.js +++ b/example/default_hash.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .default({ x : 10, y : 10 }) .argv ; diff --git a/example/default_singles.js b/example/default_singles.js index 5df95f921..bf5f5f735 100644 --- a/example/default_singles.js +++ b/example/default_singles.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .default('x', 10) .default('y', 10) .argv diff --git a/example/demand_count.js b/example/demand_count.js index 1b3d19ba7..886d4f5bd 100644 --- a/example/demand_count.js +++ b/example/demand_count.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .demand(2) .argv; console.dir(argv) diff --git a/example/divide.js b/example/divide.js index f50fb2617..c3d569013 100644 --- a/example/divide.js +++ b/example/divide.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Usage: $0 -x [num] -y [num]') .demand(['x','y']) .argv; diff --git a/example/help.js b/example/help.js index a290809ba..eb241fdfb 100644 --- a/example/help.js +++ b/example/help.js @@ -1,6 +1,6 @@ var yargs = require('../index'); -var argv = yargs +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('This is my awesome program\n\nUsage: $0 [options]') .help('help').alias('help', 'h') .version('version', '1.0.1').alias('version', 'V') diff --git a/example/implies.js b/example/implies.js index 80425e4a6..32a567b4c 100644 --- a/example/implies.js +++ b/example/implies.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Usage: $0 -x [num] -y [num]') .implies('x', 'y') .argv; diff --git a/example/implies_hash.js b/example/implies_hash.js index 039f20d3d..ba80a2632 100644 --- a/example/implies_hash.js +++ b/example/implies_hash.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Usage: $0 -x [num] -y [num] -w [msg] -h [msg]') .implies({ x: 'y', diff --git a/example/line_count.js b/example/line_count.js index 7b0d0d8b5..a2ba64585 100644 --- a/example/line_count.js +++ b/example/line_count.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Count the lines in a file.\nUsage: $0') .demand('f') .alias('f', 'file') diff --git a/example/line_count_options.js b/example/line_count_options.js index 53a345aaf..c753a8763 100644 --- a/example/line_count_options.js +++ b/example/line_count_options.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Count the lines in a file.\nUsage: $0') .options({ file : { diff --git a/example/line_count_wrap.js b/example/line_count_wrap.js index 412d4481b..76989c81b 100644 --- a/example/line_count_wrap.js +++ b/example/line_count_wrap.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .usage('Count the lines in a file.\nUsage: $0') .wrap(80) .demand('f') diff --git a/example/nonopt.js b/example/nonopt.js index 36ae47032..1a30927b2 100644 --- a/example/nonopt.js +++ b/example/nonopt.js @@ -1,4 +1,4 @@ #!/usr/bin/env node -var argv = require('yargs').argv; +var argv = require('yargs/yargs')(process.argv.slice(2)).argv; console.log('(%d,%d)', argv.x, argv.y); console.log(argv._); diff --git a/example/requires_arg.js b/example/requires_arg.js index 02500e45b..06cc0ae38 100644 --- a/example/requires_arg.js +++ b/example/requires_arg.js @@ -1,4 +1,4 @@ -var yargs = require('yargs'); +var yargs = require('yargs/yargs')(process.argv.slice(2)); var argv = yargs.usage('This is my awesome program', { 'input': { diff --git a/example/short.js b/example/short.js index e40a47791..acd1388e4 100644 --- a/example/short.js +++ b/example/short.js @@ -1,3 +1,3 @@ #!/usr/bin/env node -var argv = require('yargs').argv; +var argv = require('yargs/yargs')(process.argv.slice(2)).argv; console.log('(%d,%d)', argv.x, argv.y); diff --git a/example/strict.js b/example/strict.js index 637e1fda9..f1ec29cee 100644 --- a/example/strict.js +++ b/example/strict.js @@ -1,4 +1,4 @@ -var yargs = require('yargs'); +var yargs = require('yargs/yargs')(process.argv.slice(2)); var argv = yargs.usage('This is my awesome program', { 'about': { diff --git a/example/string.js b/example/string.js index baf78e594..2753dc87d 100644 --- a/example/string.js +++ b/example/string.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs') +var argv = require('yargs/yargs')(process.argv.slice(2)) .string('x', 'y') .argv ; diff --git a/example/usage-options.js b/example/usage-options.js index 5bf961689..0ccc8ee0d 100644 --- a/example/usage-options.js +++ b/example/usage-options.js @@ -1,4 +1,4 @@ -var yargs = require('yargs'); +var yargs = require('yargs/yargs')(process.argv.slice(2)); var argv = yargs.usage('This is my awesome program') .options({ diff --git a/example/xup.js b/example/xup.js index 34bad8dde..c2abaf51b 100644 --- a/example/xup.js +++ b/example/xup.js @@ -1,5 +1,5 @@ #!/usr/bin/env node -var argv = require('yargs').argv; +var argv = require('yargs/yargs')(process.argv.slice(2)).argv; if (argv.rif - 5 * argv.xup > 7.138) { console.log('Buy more riffiwobbles'); diff --git a/package.json b/package.json index 43be777ce..3e2b83601 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "./index.cjs" ], "./helpers": { - "import": "./helpers.mjs" + "import": "./helpers.mjs", + "require": "./yargs" }, "./yargs": [ { diff --git a/screen.png b/screen.png deleted file mode 100644 index 557b70b85..000000000 Binary files a/screen.png and /dev/null differ diff --git a/test/helpers.cjs b/test/helpers.cjs index cd6e03c51..eb6383ff1 100644 --- a/test/helpers.cjs +++ b/test/helpers.cjs @@ -5,7 +5,7 @@ const assert = require('assert'); const yargs = require('../yargs'); const {applyExtends} = require('../yargs'); -const HELPER_COUNT = 2; +const HELPER_COUNT = 3; describe('helpers', () => { it('does not expose additional helpers beyond blessed list', () => { @@ -28,4 +28,10 @@ describe('helpers', () => { assert.strictEqual(argv.bar, 99); }); }); + describe('hideBin', () => { + it('exposes helper for hiding node bin', () => { + const argv = yargs.hideBin(['node', 'foo.js', '--hello']); + assert.deepStrictEqual(argv, ['--hello']); + }); + }); }); diff --git a/yargs b/yargs index 81116cd61..4281fdd54 100644 --- a/yargs +++ b/yargs @@ -1,8 +1,9 @@ // TODO: consolidate on using a helpers file at some point in the future, which // is the approach currently used to export Parser and applyExtends for ESM: -const {applyExtends, cjsPlatformShim, Parser, Yargs} = require('./build/index.cjs') +const {applyExtends, cjsPlatformShim, Parser, Yargs, processArgv} = require('./build/index.cjs') Yargs.applyExtends = (config, cwd, mergeExtends) => { return applyExtends(config, cwd, mergeExtends, cjsPlatformShim) } +Yargs.hideBin = processArgv.hideBin Yargs.Parser = Parser module.exports = Yargs