Skip to content

Commit

Permalink
feat: showHelp(level, opts)
Browse files Browse the repository at this point in the history
Adds an `opts` agument to `showHelp`

Currently, to enable showing hidden options:

```js
showHelp('log', {hidden: [true|false]})
```
  • Loading branch information
laggingreflex committed Oct 13, 2017
1 parent 118a91b commit d3b053e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
7 changes: 4 additions & 3 deletions lib/usage.js
Expand Up @@ -138,7 +138,7 @@ module.exports = function usage (yargs, y18n) {
self.deferY18nLookup = str => deferY18nLookupPrefix + str

const defaultGroup = 'Options:'
self.help = function help () {
self.help = function help (opts) {
normalizeAliases()

// handle old demanded API
Expand All @@ -151,6 +151,7 @@ module.exports = function usage (yargs, y18n) {
.concat(Object.keys(demandedOptions))
.concat(Object.keys(demandedCommands))
.concat(Object.keys(options.default))
.filter(key => options.hiddenOptions.indexOf(key) < 0 || (opts && opts.hidden))
.reduce((acc, key) => {
if (key !== '_') acc[key] = true
return acc
Expand Down Expand Up @@ -375,11 +376,11 @@ module.exports = function usage (yargs, y18n) {
return groupedKeys
}

self.showHelp = (level) => {
self.showHelp = (level, opts) => {
const logger = yargs._getLoggerInstance()
if (!level) level = 'error'
const emit = typeof level === 'function' ? level : logger[level]
emit(self.help())
emit(self.help(opts))
}

self.functionDescription = (fn) => {
Expand Down
40 changes: 40 additions & 0 deletions test/usage.js
Expand Up @@ -2726,5 +2726,45 @@ describe('usage tests', () => {
''
])
})
it('--help should display all options (including hidden ones) with showHelp(log, {hidden: true})', () => {
const r = checkUsage(() => {
const argv = yargs('--help --show-hidden')
.help(false)
.options({
foo: {
describe: 'FOO'
},
bar: {},
baz: {
describe: 'BAZ',
hidden: true
},
'help': {
describe: 'Show help',
type: 'boolean'
},
'show-hidden': {
describe: 'Show hidden options',
type: 'boolean'
}
})
.argv

if (yargs.help) {
yargs.showHelp('log', {hidden: argv.showHidden})
}
})

r.logs[0].split('\n').should.deep.equal([
'Options:',
' --version Show version number [boolean]',
' --foo FOO',
' --bar',
' --baz BAZ',
' --help Show help [boolean]',
' --show-hidden Show hidden options [boolean]',
''
])
})
})
})
1 change: 1 addition & 0 deletions test/yargs.js
Expand Up @@ -246,6 +246,7 @@ describe('yargs dsl tests', () => {
config: {},
configObjects: [],
envPrefix: 'YARGS', // preserved as global
hiddenOptions: [],
demandedCommands: {},
demandedOptions: {},
local: [
Expand Down
14 changes: 8 additions & 6 deletions yargs.js
Expand Up @@ -93,7 +93,8 @@ function Yargs (processArgs, cwd, parentRequire) {

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

const objectOptions = [
Expand Down Expand Up @@ -647,8 +648,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) {
options.hiddenOptions.push(key)
}

if (opt.requiresArg) {
Expand Down Expand Up @@ -739,10 +741,10 @@ function Yargs (processArgs, cwd, parentRequire) {
}
self.getStrict = () => strict

self.showHelp = function (level) {
argsert('[string|function]', [level], arguments.length)
self.showHelp = function (level, opts) {
argsert('[string|function] [object]', [level, opts], arguments.length)
if (!self.parsed) self._parseArgs(processArgs) // run parser, if it has not already been executed.
usage.showHelp(level)
usage.showHelp(level, opts)
return self
}

Expand Down

0 comments on commit d3b053e

Please sign in to comment.