From 5ed7d7bf27850be1c55aeedeeb21495857a26d0e Mon Sep 17 00:00:00 2001 From: Joram van den Boezem Date: Mon, 25 Oct 2021 09:22:52 +0000 Subject: [PATCH 1/2] feat: add option to pass yargs parserConfiguration --- .gitignore | 0 LICENSE | 0 README.md | 0 src/autocompleter.ts | 4 +++- src/program.ts | 24 +++++++++++++++++++++--- tests/program.spec.ts | 13 +++++++++++++ 6 files changed, 37 insertions(+), 4 deletions(-) mode change 100755 => 100644 .gitignore mode change 100755 => 100644 LICENSE mode change 100755 => 100644 README.md 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 diff --git a/src/autocompleter.ts b/src/autocompleter.ts index d2d5f741..357f68e7 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..7bc05910 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) From 8d33904f59ce80c82b798d4e560db0eaaa8394e3 Mon Sep 17 00:00:00 2001 From: Joram van den Boezem Date: Mon, 25 Oct 2021 11:31:28 +0200 Subject: [PATCH 2/2] chore: readme --- README.md | 11 +++++++---- src/autocompleter.ts | 2 +- src/program.ts | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8afdef75..253c470a 100644 --- 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 357f68e7..e4e218bc 100644 --- a/src/autocompleter.ts +++ b/src/autocompleter.ts @@ -14,7 +14,7 @@ export class Autocompleter { private yargsCompletions(argv: string[]) { return new Promise((resolve, reject) => { - // We need to override strip-dashed to make sure yargs can find the + // 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 }) diff --git a/src/program.ts b/src/program.ts index 7bc05910..0b3c55d9 100644 --- a/src/program.ts +++ b/src/program.ts @@ -142,7 +142,7 @@ export class Program extends (EventEmitter as new () => TypedEventEmitter + overrideParserConfiguration?: Partial ) { let yargs = createYargs() @@ -158,7 +158,7 @@ export class Program extends (EventEmitter as new () => TypedEventEmitter