Skip to content

Commit

Permalink
feat: add method to hide option extras (#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
jly36963 committed Dec 28, 2022
1 parent 6cb69fb commit 2c144c4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/usage.ts
Expand Up @@ -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();
});

Expand Down
17 changes: 17 additions & 0 deletions lib/yargs-factory.ts
Expand Up @@ -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');
Expand Down Expand Up @@ -134,6 +135,7 @@ export interface YargsInternalMethods {
getLoggerInstance(): LoggerInstance;
getParseContext(): Object;
getParserConfiguration(): Configuration;
getUsageConfiguration(): UsageConfiguration;
getUsageInstance(): UsageInstance;
getValidationInstance(): ValidationInstance;
hasParseCallback(): boolean;
Expand Down Expand Up @@ -201,6 +203,7 @@ export class YargsInstance {
#strictCommands = false;
#strictOptions = false;
#usage: UsageInstance;
#usageConfig: UsageConfiguration = {};
#versionOpt: string | null = null;
#validation: ValidationInstance;

Expand Down Expand Up @@ -1405,6 +1408,11 @@ export class YargsInstance {
return this;
}
}
usageConfiguration(config: UsageConfiguration) {
argsert('<object>', [config], arguments.length);
this.#usageConfig = config;
return this;
}
version(opt?: string | false, msg?: string, ver?: string): YargsInstance {
const defaultVersionOpt = 'version';
argsert(
Expand Down Expand Up @@ -1568,6 +1576,9 @@ export class YargsInstance {
[kGetParserConfiguration](): Configuration {
return this.#parserConfig;
}
[kGetUsageConfiguration](): UsageConfiguration {
return this.#usageConfig;
}
[kGuessLocale]() {
if (!this.#detectLocale) return;
const locale =
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -2307,6 +2319,11 @@ export interface Configuration extends Partial<ParserConfiguration> {
'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;
Expand Down
41 changes: 41 additions & 0 deletions test/usage.cjs
Expand Up @@ -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(() =>
Expand Down

0 comments on commit 2c144c4

Please sign in to comment.