From 83e5317dc9c7b303aec852307177f143cd0a2b63 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Sun, 27 Mar 2022 10:53:03 -0600 Subject: [PATCH 1/6] Add method to hide option extras --- lib/usage.ts | 9 ++++++++- lib/yargs-factory.ts | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/usage.ts b/lib/usage.ts index cea2f7c22..cb4a5787e 100644 --- a/lib/usage.ts +++ b/lib/usage.ts @@ -443,7 +443,8 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) { desc ); - if (extra) ui.div({text: extra, padding: [0, 0, 0, 2], align: 'right'}); + if (extra && !shouldHideOptionExtras) + ui.div({text: extra, padding: [0, 0, 0, 2], align: 'right'}); else ui.div(); }); @@ -734,6 +735,11 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) { } }; + let shouldHideOptionExtras = false; + self.hideOptionExtras = function hideOptionExtras() { + shouldHideOptionExtras = true; + }; + return self; } @@ -763,6 +769,7 @@ export interface UsageInstance { getUsage(): [string, string][]; getUsageDisabled(): boolean; help(): string; + hideOptionExtras(): void; reset(localLookup: Dictionary): UsageInstance; showHelp(level?: 'error' | 'log' | ((message: string) => void)): void; showHelpOnFail(enabled?: boolean | string, message?: string): UsageInstance; diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index 2dff7e395..0f599a4de 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -843,6 +843,10 @@ export class YargsInstance { this.#options.hiddenOptions.push(key); return this; } + hideOptionExtras(): YargsInstance { + this.#usage.hideOptionExtras(); + return this; + } implies( key: string | Dictionary, value?: KeyOrPos | KeyOrPos[] From c13f2defca7dacb970ade0bb23c308a7089b4424 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 29 Mar 2022 20:46:21 -0600 Subject: [PATCH 2/6] fixes --- lib/usage.ts | 9 ++++----- lib/yargs-factory.ts | 21 +++++++++++++++++---- test/usage.cjs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/lib/usage.ts b/lib/usage.ts index cb4a5787e..32b3c01cc 100644 --- a/lib/usage.ts +++ b/lib/usage.ts @@ -443,6 +443,10 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) { desc ); + const shouldHideOptionExtras = + yargs.getInternalMethods().getUsageConfiguration()['hide-types'] === + true; + if (extra && !shouldHideOptionExtras) ui.div({text: extra, padding: [0, 0, 0, 2], align: 'right'}); else ui.div(); @@ -735,11 +739,6 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) { } }; - let shouldHideOptionExtras = false; - self.hideOptionExtras = function hideOptionExtras() { - shouldHideOptionExtras = true; - }; - return self; } diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index 0f599a4de..419f84dc5 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -92,6 +92,7 @@ const kEmitWarning = Symbol('emitWarning'); const kFreeze = Symbol('freeze'); const kGetDollarZero = Symbol('getDollarZero'); const kGetParserConfiguration = Symbol('getParserConfiguration'); +const kGetUsageConfiguration = Symbol('getUsageConfiguration'); const kGuessLocale = Symbol('guessLocale'); const kGuessVersion = Symbol('guessVersion'); const kParsePositionalNumbers = Symbol('parsePositionalNumbers'); @@ -132,6 +133,7 @@ export interface YargsInternalMethods { getLoggerInstance(): LoggerInstance; getParseContext(): Object; getParserConfiguration(): Configuration; + getUsageConfiguration(): UsageConfiguration; getUsageInstance(): UsageInstance; getValidationInstance(): ValidationInstance; hasParseCallback(): boolean; @@ -197,6 +199,7 @@ export class YargsInstance { #strictCommands = false; #strictOptions = false; #usage: UsageInstance; + #usageConfig: UsageConfiguration = {}; #versionOpt: string | null = null; #validation: ValidationInstance; @@ -843,10 +846,6 @@ export class YargsInstance { this.#options.hiddenOptions.push(key); return this; } - hideOptionExtras(): YargsInstance { - this.#usage.hideOptionExtras(); - return this; - } implies( key: string | Dictionary, value?: KeyOrPos | KeyOrPos[] @@ -1388,6 +1387,11 @@ export class YargsInstance { return this; } } + usageConfiguration(config: UsageConfiguration) { + argsert('', [config], arguments.length); + this.#usageConfig = config; + return this; + } version(opt?: string | false, msg?: string, ver?: string): YargsInstance { const defaultVersionOpt = 'version'; argsert( @@ -1551,6 +1555,9 @@ export class YargsInstance { [kGetParserConfiguration](): Configuration { return this.#parserConfig; } + [kGetUsageConfiguration](): UsageConfiguration { + return this.#usageConfig; + } [kGuessLocale]() { if (!this.#detectLocale) return; const locale = @@ -1761,6 +1768,7 @@ export class YargsInstance { getLoggerInstance: this[kGetLoggerInstance].bind(this), getParseContext: this[kGetParseContext].bind(this), getParserConfiguration: this[kGetParserConfiguration].bind(this), + getUsageConfiguration: this[kGetUsageConfiguration].bind(this), getUsageInstance: this[kGetUsageInstance].bind(this), getValidationInstance: this[kGetValidationInstance].bind(this), hasParseCallback: this[kHasParseCallback].bind(this), @@ -2284,6 +2292,11 @@ export interface Configuration extends Partial { 'sort-commands'?: boolean; } +export interface UsageConfiguration { + /** Should types be hideen when usage is displayed */ + 'hide-types'?: boolean; +} + export interface OptionDefinition { alias?: string | string[]; array?: boolean; diff --git a/test/usage.cjs b/test/usage.cjs index 1697364a6..ab8e19578 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -4755,4 +4755,46 @@ describe('usage tests', () => { ' -v, --version Custom version description [boolean]', ]); }); + + describe('usage configuration', () => { + it('allows extras to be disabled when "hide-types" is true', () => { + const r = checkUsage(() => + yargs('cmd1 --help') + .command( + 'cmd1', + 'cmd1 desc', + yargs => + yargs.option('opt1', { + type: 'string', + choices: ['foo', 'bar', 'baz'], + alias: 'o', + required: true, + description: 'A long description that might break formatting', + }), + argv => console.log(argv) + ) + // .hideOptionExtras() + .usageConfiguration({'hide-types': true}) + .strict() + .parse() + ); + r.should.have.property('result'); + r.result.should.have.property('_').with.length(1); + r.should.have.property('errors'); + r.should.have.property('logs').with.length(1); + r.should.have.property('exit').and.equal(true); + r.logs[0] + .split(/\n/) + .should.deep.equal([ + 'usage cmd1', + '', + 'cmd1 desc', + '', + 'Options:', + ' --help Show help', + ' --version Show version number', + ' -o, --opt1 A long description that might break formatting', + ]); + }); + }); }); From 1b317856e8757b452c1e5b60e5b4bb9b3e08c4ab Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 29 Mar 2022 20:47:47 -0600 Subject: [PATCH 3/6] spelling --- lib/yargs-factory.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index 419f84dc5..6560fea4e 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -2293,7 +2293,7 @@ export interface Configuration extends Partial { } export interface UsageConfiguration { - /** Should types be hideen when usage is displayed */ + /** Should types be hidden when usage is displayed */ 'hide-types'?: boolean; } From 8cb2dd25681e94167f5e48a9aff4953b7a1597eb Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 29 Mar 2022 20:49:00 -0600 Subject: [PATCH 4/6] remove comment --- test/usage.cjs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/usage.cjs b/test/usage.cjs index ab8e19578..0902e3ce1 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -4773,7 +4773,6 @@ describe('usage tests', () => { }), argv => console.log(argv) ) - // .hideOptionExtras() .usageConfiguration({'hide-types': true}) .strict() .parse() From 92ab0ca8a004556aac7d55930887fa822f80a36b Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 29 Mar 2022 20:52:55 -0600 Subject: [PATCH 5/6] cleanup --- lib/usage.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/usage.ts b/lib/usage.ts index 32b3c01cc..e54ddb858 100644 --- a/lib/usage.ts +++ b/lib/usage.ts @@ -768,7 +768,6 @@ export interface UsageInstance { getUsage(): [string, string][]; getUsageDisabled(): boolean; help(): string; - hideOptionExtras(): void; reset(localLookup: Dictionary): UsageInstance; showHelp(level?: 'error' | 'log' | ((message: string) => void)): void; showHelpOnFail(enabled?: boolean | string, message?: string): UsageInstance; From 4e313ff041920a92f4f43bfd502274b4cb859bcd Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 29 Mar 2022 21:02:00 -0600 Subject: [PATCH 6/6] console log --- test/usage.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/usage.cjs b/test/usage.cjs index 0902e3ce1..3522f7925 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -4771,7 +4771,7 @@ describe('usage tests', () => { required: true, description: 'A long description that might break formatting', }), - argv => console.log(argv) + () => {} ) .usageConfiguration({'hide-types': true}) .strict()