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(): add examples #1682

Merged
merged 8 commits into from Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
11 changes: 11 additions & 0 deletions docs/api.md
Expand Up @@ -766,12 +766,23 @@ var argv = require('yargs')

.example(cmd, desc)
-------------------
.example(exampleArray)
rluvaton marked this conversation as resolved.
Show resolved Hide resolved
-------------------

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.

If you want to add multiple examples at once, just pass an array of examples, e.g
```js
require('yargs')
.example([
['$0 --config "~/config.json"', 'Use custom config'],
['$0 --safe', 'Start in safe mode']
]);
```

<a name="exitprocess"></a>.exitProcess(enable)
----------------------------------

Expand Down
14 changes: 10 additions & 4 deletions lib/yargs.ts
Expand Up @@ -451,9 +451,15 @@ export function Yargs (processArgs: string | string[] = [], cwd = process.cwd(),
return self
}

self.example = function (cmd, description) {
argsert('<string> [string]', [cmd, description], arguments.length)
usage.example(cmd, description)
self.example = function (cmd: string | [string, string?][], description?: string) {
argsert('<string|array> [string]', [cmd, description], arguments.length)

if (Array.isArray(cmd)) {
cmd.forEach((exampleParams) => self.example(...exampleParams))
} else {
usage.example(cmd, description)
}

return self
}

Expand Down Expand Up @@ -1490,7 +1496,7 @@ export interface YargsInstance {
env (prefix?: string | false): YargsInstance
epilog: YargsInstance['epilogue']
epilogue (msg: string): YargsInstance
example (cmd: string, description?: string): YargsInstance
example (cmd: string | [string, string?][], description?: string): YargsInstance
exit (code: number, err?: YError | string): void
exitProcess (enabled: boolean): YargsInstance
fail (f: FailureFunction): YargsInstance
Expand Down
27 changes: 27 additions & 0 deletions test/usage.js
Expand Up @@ -1084,6 +1084,33 @@ describe('usage tests', () => {
])
})

it('should display examples on fail when passing multiple examples at once', () => {
const r = checkUsage(() => yargs('')
.example([
['$0 something', 'description'],
['$0 something else', 'other description']
])
.demand(['y'])
.wrap(null)
.parse()
)
r.should.have.property('result')
r.result.should.have.property('_').with.length(0)
r.should.have.property('errors')
r.should.have.property('logs').with.length(0)
r.should.have.property('exit').and.equal(true)
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]',
' -y [required]',
'Examples:',
' usage something description',
' usage something else other description',
'Missing required argument: y'
])
})

describe('demand option with boolean flag', () => {
describe('with demand option', () => {
it('should report missing required arguments', () => {
Expand Down