Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: --show-hidden #1061

Merged
merged 1 commit into from Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/api.md
Expand Up @@ -576,6 +576,11 @@ Describe a `key` for the generated usage information.

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

<a name="hide"></a>.hide(key)
--------------------

Hides a `key` from the generated usage information. Unless a `--show-hidden` option is also passed with `--help` (see [`showHidden()`](#showHidden)).

.detectLocale(boolean)
-----------

Expand Down Expand Up @@ -798,6 +803,29 @@ var yargs = require("yargs")(['--info'])
.argv
```

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

Configure the `--show-hidden` option that displays the hidden keys (see [`hide()`](#hide)).

If the first argument is a boolean, it enables/disables this option altogether. I.e. hidden keys will be permanently hidden if first argument is `false`.

If the first argument is a string it changes the key name ("--show-hidden").

Second argument changes the default description ("Show hidden options")

Example:

```js
var yargs = require("yargs")(['--help'])
.showHidden('show-hidden', 'Show hidden options')
.argv
```

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

Expand Down
27 changes: 17 additions & 10 deletions lib/usage.js
Expand Up @@ -152,16 +152,23 @@ module.exports = function usage (yargs, y18n) {
const demandedCommands = yargs.getDemandedCommands()
const groups = yargs.getGroups()
const options = yargs.getOptions()
let keys = Object.keys(
Object.keys(descriptions)
.concat(Object.keys(demandedOptions))
.concat(Object.keys(demandedCommands))
.concat(Object.keys(options.default))
.reduce((acc, key) => {
if (key !== '_') acc[key] = true
return acc
}, {})
)

let keys = []
keys = keys.concat(Object.keys(descriptions))
keys = keys.concat(Object.keys(demandedOptions))
keys = keys.concat(Object.keys(demandedCommands))
keys = keys.concat(Object.keys(options.default))
keys = keys.filter(key => {
if (options.hiddenOptions.indexOf(key) < 0) {
return true
} else if (yargs.parsed.argv[options.showHiddenOpt]) {
return true
}
})
keys = Object.keys(keys.reduce((acc, key) => {
if (key !== '_') acc[key] = true
return acc
}, {}))

const theWrap = getWrap()
const ui = require('cliui')({
Expand Down
78 changes: 78 additions & 0 deletions test/usage.js
Expand Up @@ -2887,5 +2887,83 @@ describe('usage tests', () => {
''
])
})
it('--help should display all options (including hidden ones) with --show-hidden', () => {
const r = checkUsage(() => yargs('--help --show-hidden --mama ama')
.options({
foo: {
describe: 'FOO'
},
bar: {},
baz: {
describe: 'BAZ',
hidden: true
}
})
.argv
)

r.logs[0].split('\n').should.deep.equal([
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]',
' --foo FOO',
' --bar',
' --baz BAZ',
''
])
})
it('--help should display --custom-show-hidden', () => {
const r = checkUsage(() => yargs('--help')
.options({
foo: {
describe: 'FOO'
},
bar: {},
baz: {
describe: 'BAZ',
hidden: true
}
})
.showHidden('custom-show-hidden')
.argv
)

r.logs[0].split('\n').should.deep.equal([
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]',
' --foo FOO',
' --bar',
' --custom-show-hidden Show hidden options [boolean]',
''
])
})
it('--help should display all options with --custom-show-hidden', () => {
const r = checkUsage(() => yargs('--help --custom-show-hidden')
.options({
foo: {
describe: 'FOO'
},
bar: {},
baz: {
describe: 'BAZ',
hidden: true
}
})
.showHidden('custom-show-hidden')
.argv
)

r.logs[0].split('\n').should.deep.equal([
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]',
' --foo FOO',
' --bar',
' --baz BAZ',
' --custom-show-hidden Show hidden options [boolean]',
''
])
})
})
})
37 changes: 37 additions & 0 deletions test/yargs.js
Expand Up @@ -129,6 +129,42 @@ describe('yargs dsl tests', () => {
expect(r.errors).to.deep.equal([])
})

describe('hide', () => {
it('should add the key to hiddenOptions', () => {
const options = yargs('')
.hide('someKey')
.getOptions()
options.should.have.property('hiddenOptions')
options.hiddenOptions.should.include('someKey')
})
})

describe('showHidden', () => {
it('should have a default show-hidden private option pre-configured', () => {
const options = yargs('').getOptions()
options.should.have.property('showHiddenOpt')
options.showHiddenOpt.should.eql('show-hidden')
})
it('should not have show-hidden as an actual option described by default', () => {
const options = yargs('').getOptions()
options.key.should.not.have.property('show-hidden')
})
it('should set show-hidden option', () => {
const options = yargs('')
.showHidden()
.getOptions()
options.key.should.have.property('show-hidden')
})
it('should set custom-show-hidden option', () => {
const options = yargs('')
.showHidden('custom-show-hidden')
.getOptions()
options.key.should.have.property('custom-show-hidden')
options.should.have.property('showHiddenOpt')
options.showHiddenOpt.should.eql('custom-show-hidden')
})
})

describe('showHelpOnFail', () => {
it('should display custom failure message, if string is provided as first argument', () => {
const r = checkOutput(() => yargs([])
Expand Down Expand Up @@ -233,6 +269,7 @@ describe('yargs dsl tests', () => {
config: {},
configObjects: [],
envPrefix: 'YARGS', // preserved as global
hiddenOptions: [],
demandedCommands: {},
demandedOptions: {},
local: [
Expand Down
30 changes: 27 additions & 3 deletions yargs.js
Expand Up @@ -93,7 +93,8 @@ function Yargs (processArgs, cwd, parentRequire) {

const arrayOptions = [
'array', 'boolean', 'string', 'skipValidation',
'count', 'normalize', 'number'
'count', 'normalize', 'number',
'hiddenOptions'
]

const objectOptions = [
Expand Down Expand Up @@ -649,8 +650,9 @@ function Yargs (processArgs, cwd, parentRequire) {
}

const desc = opt.describe || opt.description || opt.desc
if (!opt.hidden) {
self.describe(key, desc)
self.describe(key, desc)
if (opt.hidden) {
self.hide(key)
}

if (opt.requiresArg) {
Expand Down Expand Up @@ -816,6 +818,28 @@ function Yargs (processArgs, cwd, parentRequire) {
return self
}

const defaultShowHiddenOpt = 'show-hidden'
options.showHiddenOpt = defaultShowHiddenOpt
self.addShowHiddenOpt = self.showHidden = function addShowHiddenOpt (opt, msg) {
argsert('[string|boolean] [string]', [opt, msg], arguments.length)

if (arguments.length === 1) {
if (opt === false) return self
}

const showHiddenOpt = typeof opt === 'string' ? opt : defaultShowHiddenOpt
self.boolean(showHiddenOpt)
self.describe(showHiddenOpt, msg || usage.deferY18nLookup('Show hidden options'))
options.showHiddenOpt = showHiddenOpt
return self
}

self.hide = function hide (key) {
argsert('<string|object>', [key], arguments.length)
options.hiddenOptions.push(key)
return self
}

self.showHelpOnFail = function showHelpOnFail (enabled, message) {
argsert('[boolean|string] [string]', [enabled, message], arguments.length)
usage.showHelpOnFail(enabled, message)
Expand Down