diff --git a/lib/usage.ts b/lib/usage.ts index 6bacf5320..c1189e153 100644 --- a/lib/usage.ts +++ b/lib/usage.ts @@ -453,7 +453,12 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) { desc ); - if (extra) ui.div({text: extra, padding: [0, 0, 0, 2], align: 'right'}); + 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(); }); diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index c093dbc36..2bfc5224a 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -93,6 +93,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'); @@ -134,6 +135,7 @@ export interface YargsInternalMethods { getLoggerInstance(): LoggerInstance; getParseContext(): Object; getParserConfiguration(): Configuration; + getUsageConfiguration(): UsageConfiguration; getUsageInstance(): UsageInstance; getValidationInstance(): ValidationInstance; hasParseCallback(): boolean; @@ -201,6 +203,7 @@ export class YargsInstance { #strictCommands = false; #strictOptions = false; #usage: UsageInstance; + #usageConfig: UsageConfiguration = {}; #versionOpt: string | null = null; #validation: ValidationInstance; @@ -1405,6 +1408,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( @@ -1568,6 +1576,9 @@ export class YargsInstance { [kGetParserConfiguration](): Configuration { return this.#parserConfig; } + [kGetUsageConfiguration](): UsageConfiguration { + return this.#usageConfig; + } [kGuessLocale]() { if (!this.#detectLocale) return; const locale = @@ -1778,6 +1789,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), @@ -2307,6 +2319,11 @@ export interface Configuration extends Partial { 'sort-commands'?: boolean; } +export interface UsageConfiguration { + /** Should types be hidden 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 d80770879..9b4c50ec0 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -4847,6 +4847,47 @@ describe('usage tests', () => { ]); }); + 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', + }), + () => {} + ) + .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', + ]); + }); + }); + // https://github.com/yargs/yargs/issues/2169 it('allows multiple option calls to not clobber description', () => { const r = checkUsage(() =>