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 option to pass yargs parserConfiguration #334

Merged
merged 2 commits into from
Oct 25, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .gitignore
100755 → 100644
Empty file.
Empty file modified LICENSE
100755 → 100644
Empty file.
11 changes: 7 additions & 4 deletions README.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,14 @@ Creates a new program. Options (object, optional) can contain these keys:
displays program usage information.
- `version` (boolean, default: true) adds `version` and `--version` to the
program which displays program version from package.json.
- `historyFile` (string | null, defaults: {homedir}/.bandersnatch_history) is a
- `historyFile` (string | null, default: {homedir}/.bandersnatch_history) is a
path to the app history file. Set to NULL to disable.
- `exit` (boolean | () => void, default: () => process.exit()) Specifies whether to add a default behaviour for an `exit`
command. `false` disables the default implementation, a custom function will be installed
as the actual handler.
- `exit` (boolean | () => void, default: () => process.exit()) Specifies whether
to add a default behaviour for an `exit` command. `false` disables the default
implementation, a custom function will be installed as the actual handler.
- `parserConfiguration` (object, optional) can be used to modify the parser
configuration. For available options, see
- https://github.com/yargs/yargs/blob/main/docs/api.md#parserConfiguration.

#### `program.description(description)`

Expand Down
4 changes: 3 additions & 1 deletion src/autocompleter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export class Autocompleter {

private yargsCompletions(argv: string[]) {
return new Promise<string[]>((resolve, reject) => {
const yargs = this.program.createYargsInstance()
// We need to override 'strip-dashed' to make sure yargs can find the
// '--get-yargs-completions' option.
const yargs = this.program.createYargsInstance({ 'strip-dashed': false })

// yargs.getCompletion() doesn't work for our use case.
yargs.parse(
Expand Down
24 changes: 21 additions & 3 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventEmitter } from 'events'
import os from 'os'
import path from 'path'
import TypedEventEmitter from 'typed-emitter'
import { Argv } from 'yargs'
import { Argv, ParserConfigurationOptions } from 'yargs'
import createYargs from 'yargs/yargs'
import { Arguments, Command, command } from './command'
import { history, History } from './history'
Expand Down Expand Up @@ -67,6 +67,14 @@ type ProgramOptions = {
* Defaults to `() => process.exit()`.
*/
exit?: boolean | (() => void)

/**
* Pass Yargs parser configuration, for available options, see
* https://github.com/yargs/yargs/blob/main/docs/advanced.md#customizing-yargs-parser.
*
* Defaults to `undefined`.
*/
parserConfiguration?: Partial<ParserConfigurationOptions>
}

/**
Expand Down Expand Up @@ -133,8 +141,10 @@ export class Program extends (EventEmitter as new () => TypedEventEmitter<Events
*
* @private
*/
public createYargsInstance() {
const yargs = createYargs()
public createYargsInstance(
overrideParserConfiguration?: Partial<ParserConfigurationOptions>
) {
let yargs = createYargs()

this.options.description && yargs.usage(this.options.description)

Expand All @@ -144,6 +154,14 @@ export class Program extends (EventEmitter as new () => TypedEventEmitter<Events
// Version must be false or undefined
this.options.version !== false ? yargs.version() : yargs.version(false)

// Pass yargs parser options if defined
if (typeof this.options.parserConfiguration !== 'undefined') {
yargs = yargs.parserConfiguration({
...this.options.parserConfiguration,
...overrideParserConfiguration,
})
}

// Non-configurable options
yargs.recommendCommands()
yargs.strict()
Expand Down
13 changes: 13 additions & 0 deletions tests/program.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ test('program executes argv', async () => {
})
})

test('program passes parserConfiguration', async () => {
await mockArgv(['test', '--test-field', '1'], async () => {
const app = program({ parserConfiguration: { 'strip-dashed': true } }).add(
command('test')
.option('test-field')
.action((args) => {
return JSON.stringify(args)
})
)
await expect(app.run()).resolves.toBe('{"testField":1}')
})
})

test('program starts repl', async () => {
const app = program()
expect(app.repl()).toBeInstanceOf(MockedRepl)
Expand Down