diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/LICENSE b/LICENSE old mode 100755 new mode 100644 diff --git a/README.md b/README.md old mode 100755 new mode 100644 index 8afdef75..253c470a --- a/README.md +++ b/README.md @@ -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)` diff --git a/src/autocompleter.ts b/src/autocompleter.ts index d2d5f741..e4e218bc 100644 --- a/src/autocompleter.ts +++ b/src/autocompleter.ts @@ -14,7 +14,9 @@ export class Autocompleter { private yargsCompletions(argv: string[]) { return new Promise((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( diff --git a/src/program.ts b/src/program.ts index 16a71625..0b3c55d9 100644 --- a/src/program.ts +++ b/src/program.ts @@ -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' @@ -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 } /** @@ -133,8 +141,10 @@ export class Program extends (EventEmitter as new () => TypedEventEmitter + ) { + let yargs = createYargs() this.options.description && yargs.usage(this.options.description) @@ -144,6 +154,14 @@ export class Program extends (EventEmitter as new () => TypedEventEmitter { }) }) +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)