Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add method to hide option extras #2156

Merged
merged 10 commits into from Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/usage.ts
Expand Up @@ -450,7 +450,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 @@ -1398,6 +1401,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 @@ -1561,6 +1569,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 @@ -1771,6 +1782,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 @@ -2300,6 +2312,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 @@ -4837,4 +4837,45 @@ 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',
}),
() => {}
)
.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',
]);
});
});
});