Skip to content

Commit

Permalink
feat: enable .help() and .version() by default (#912)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: version() and help() are now enabled by default, and show up in help output; the implicit help command can no longer be enabled/disabled independently from the help command itself (which can now be disabled).
  • Loading branch information
bcoe committed Jul 11, 2017
1 parent eda6251 commit 1ef44e0
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 270 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -67,10 +67,11 @@ require('yargs') // eslint-disable-line
alias: 'v',
default: false
})
.help()
.argv
```

Run the example above with `--help` to see the help for the application.

## Table of Contents

* [Yargs' API](/docs/api.md)
Expand Down
40 changes: 15 additions & 25 deletions docs/api.md
Expand Up @@ -762,20 +762,16 @@ var yargs = require('yargs')(['--help'])
-----------------------------------------
.help([option | boolean])
-----------------------------------------
.help([option, [description | boolean]])
-----------------------------------------
.help([option, [description, [boolean]]])
.help([option, [description]])
-----------------------------------------

Add an option (e.g. `--help`) and implicit command that displays the usage
string and exits the process.
Configure an (e.g. `--help`) and implicit command that displays the usage
string and exits the process. By default yargs enables help on the `--help` option.

If present, the `description` parameter customizes the description of
the help option in the usage string.

If a boolean argument is provided, it will enable or disable the use of an
implicit command. The implicit command is enabled by default, but it can be
disabled by passing `false`.
If the boolean argument `false` is provided, it will disable `--help`.

Note that any multi-char aliases (e.g. `help`) used for the help option will
also be used for the implicit command. If there are no multi-char aliases (e.g.
Expand All @@ -787,14 +783,12 @@ If invoked without parameters, `.help()` will use `--help` as the option and
Example:

```js
var yargs = require("yargs")(['--help'])
var yargs = require("yargs")(['--info'])
.usage("$0 -operand1 number -operand2 number -operation [add|subtract]")
.help()
.help('info')
.argv
```

Later on, `argv` can be retrieved with `yargs.argv`.

<a name="implies"></a>.implies(x, y)
--------------

Expand Down Expand Up @@ -1254,25 +1248,21 @@ present script similar to how `$0` works in bash or perl.

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

<a name="version"></a>.version([option], [description], [version])
<a name="version"></a>
.version()
----------------------------------------
.version([version|boolean])
----------------------------------------
.version([option], [description], [version])
----------------------------------------

Add an option (e.g. `--version`) that displays the version number (given by the
`version` parameter) and exits the process.
`version` parameter) and exits the process. By default yargs enables version for the `--version` option.

If no arguments are passed to `version` (`.version()`), yargs will parse the `package.json`
of your module and use its `version` value. The default value of `option` is `--version`.
of your module and use its `version` value.

You can provide a `function` for version, rather than a string.
This is useful if you want to use a version stored in a location other than package.json:

```js
var argv = require('yargs')
.version(function() {
return require('../lib/version').version;
})
.argv;
```
If the boolean argument `false` is provided, it will disable `--version`.

<a name="wrap"></a>.wrap(columns)
--------------
Expand Down
3 changes: 1 addition & 2 deletions lib/usage.js
Expand Up @@ -444,8 +444,7 @@ module.exports = function (yargs, y18n) {

self.showVersion = function () {
const logger = yargs._getLoggerInstance()
if (typeof version === 'function') logger.log(version())
else logger.log(version)
logger.log(version)
}

self.reset = function (localLookup) {
Expand Down
47 changes: 26 additions & 21 deletions test/command.js
Expand Up @@ -501,7 +501,7 @@ describe('Command', function () {
describe('commandDir', function () {
it('supports relative dirs', function () {
var r = checkOutput(function () {
return yargs('--help').help().wrap(null)
return yargs('--help').wrap(null)
.commandDir('fixtures/cmddir')
.argv
})
Expand All @@ -512,14 +512,15 @@ describe('Command', function () {
'Commands:',
' dream [command] [opts] Go to sleep and dream',
'Options:',
' --help Show help [boolean]',
' --help Show help [boolean]',
' --version Show version number [boolean]',
''
])
})

it('supports nested subcommands', function () {
var r = checkOutput(function () {
return yargs('dream --help').help().wrap(null)
return yargs('dream --help').wrap(null)
.commandDir('fixtures/cmddir')
.argv
}, [ './command' ])
Expand All @@ -533,6 +534,7 @@ describe('Command', function () {
' within-a-dream [command] [opts] Dream within a dream',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]',
' --shared Is the dream shared with others? [boolean]',
' --extract Attempt extraction? [boolean]',
''
Expand All @@ -541,7 +543,7 @@ describe('Command', function () {

it('supports a "recurse" boolean option', function () {
var r = checkOutput(function () {
return yargs('--help').help().wrap(null)
return yargs('--help').wrap(null)
.commandDir('fixtures/cmddir', { recurse: true })
.argv
})
Expand All @@ -555,7 +557,8 @@ describe('Command', function () {
' within-a-dream [command] [opts] Dream within a dream',
' dream [command] [opts] Go to sleep and dream',
'Options:',
' --help Show help [boolean]',
' --help Show help [boolean]',
' --version Show version number [boolean]',
''
])
})
Expand All @@ -565,7 +568,7 @@ describe('Command', function () {
var pathToFile
var filename
var r = checkOutput(function () {
return yargs('--help').help().wrap(null)
return yargs('--help').wrap(null)
.commandDir('fixtures/cmddir', {
visit: function (_commandObject, _pathToFile, _filename) {
commandObject = _commandObject
Expand All @@ -587,14 +590,15 @@ describe('Command', function () {
r.should.have.property('logs')
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --help Show help [boolean]',
' --help Show help [boolean]',
' --version Show version number [boolean]',
''
])
})

it('detects and ignores cyclic dir references', function () {
var r = checkOutput(function () {
return yargs('cyclic --help').help().wrap(null)
return yargs('cyclic --help').wrap(null)
.commandDir('fixtures/cmddir_cyclic')
.argv
}, [ './command' ])
Expand All @@ -604,14 +608,15 @@ describe('Command', function () {
r.logs.join('\n').split(/\n+/).should.deep.equal([
'./command cyclic',
'Options:',
' --help Show help [boolean]',
' --help Show help [boolean]',
' --version Show version number [boolean]',
''
])
})

it('derives \'command\' string from filename when not exported', function () {
var r = checkOutput(function () {
return yargs('--help').help().wrap(null)
return yargs('--help').wrap(null)
.commandDir('fixtures/cmddir_noname')
.argv
})
Expand All @@ -622,7 +627,8 @@ describe('Command', function () {
'Commands:',
' nameless Command name derived from module filename',
'Options:',
' --help Show help [boolean]',
' --help Show help [boolean]',
' --version Show version number [boolean]',
''
])
})
Expand All @@ -648,35 +654,35 @@ describe('Command', function () {

var helpCmd = checkOutput(function () {
return yargs('help cmd')
.help().wrap(null)
.wrap(null)
.command(cmd)
.argv
}, [ './command' ])

var cmdHelp = checkOutput(function () {
return yargs('cmd help')
.help().wrap(null)
.wrap(null)
.command(cmd)
.argv
}, [ './command' ])

var helpCmdSub = checkOutput(function () {
return yargs('help cmd sub')
.help().wrap(null)
.wrap(null)
.command(cmd)
.argv
}, [ './command' ])

var cmdHelpSub = checkOutput(function () {
return yargs('cmd help sub')
.help().wrap(null)
.wrap(null)
.command(cmd)
.argv
}, [ './command' ])

var cmdSubHelp = checkOutput(function () {
return yargs('cmd sub help')
.help().wrap(null)
.wrap(null)
.command(cmd)
.argv
}, [ './command' ])
Expand All @@ -686,14 +692,16 @@ describe('Command', function () {
'Commands:',
' sub Run the subcommand',
'Options:',
' --help Show help [boolean]',
' --help Show help [boolean]',
' --version Show version number [boolean]',
''
]

var expectedSub = [
'./command cmd sub',
'Options:',
' --help Show help [boolean]',
' --help Show help [boolean]',
' --version Show version number [boolean]',
''
]

Expand Down Expand Up @@ -765,7 +773,6 @@ describe('Command', function () {
.command('foo', 'the foo command', {}, function (argv) {
counter++
})
.help()
y.parse(['foo'], function () {})
y.parse(['foo'], function () {
counter.should.equal(2)
Expand Down Expand Up @@ -1158,7 +1165,6 @@ describe('Command', function () {
yargs()
.command('command [snuh]', 'a command')
.describe('foo', 'an awesome argument')
.help()
.parse('command --help', function (err, argv, output) {
if (err) return done(err)
output.should.not.match(/Commands:/)
Expand Down Expand Up @@ -1204,7 +1210,6 @@ describe('Command', function () {
.group('snuh', 'Bad Variable Names:')
.describe('foo', 'foo option')
.describe('snuh', 'snuh positional')
.help()
.parse('command --help', function (err, argv, output) {
if (err) return done(err)
output.should.match(/Bad Variable Names:\W*--foo/)
Expand Down
7 changes: 6 additions & 1 deletion test/completion.js
Expand Up @@ -77,7 +77,8 @@ describe('Completion', function () {
describe: 'bar option'
}
})
.help('help')
.help(true)
.version(false)
})
.completion()
.argv
Expand All @@ -91,6 +92,8 @@ describe('Completion', function () {
it('completes options for the correct command', function () {
var r = checkUsage(function () {
return yargs(['./completion', '--get-yargs-completions', 'cmd2', '--o'])
.help(false)
.version(false)
.command('cmd1', 'first command', function (subYargs) {
subYargs.options({
opt1: {
Expand Down Expand Up @@ -129,6 +132,8 @@ describe('Completion', function () {
it('works if command has no options', function () {
var r = checkUsage(function () {
return yargs(['./completion', '--get-yargs-completions', 'foo', '--b'])
.help(false)
.version(false)
.command('foo', 'foo command', function (subYargs) {
subYargs.completion().argv
})
Expand Down

0 comments on commit 1ef44e0

Please sign in to comment.