Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Apr 28, 2023
2 parents 2281ace + 3566b84 commit 48bc718
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [17.7.2](https://github.com/yargs/yargs/compare/v17.7.1...v17.7.2) (2023-04-27)


### Bug Fixes

* do not crash completion when having negated options ([#2322](https://github.com/yargs/yargs/issues/2322)) ([7f42848](https://github.com/yargs/yargs/commit/7f428485e75e9b1b0db1320216d1c31469770563))

## [17.7.1](https://github.com/yargs/yargs/compare/v17.7.0...v17.7.1) (2023-02-21)


Expand Down
44 changes: 25 additions & 19 deletions lib/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ export class Completion implements CompletionInstance {
!options.hiddenOptions.includes(key) &&
!this.argsContainKey(args, key, negable)
) {
this.completeOptionKey(key, completions, current);
if (negable && !!options.default[key])
this.completeOptionKey(`no-${key}`, completions, current);
this.completeOptionKey(
key,
completions,
current,
negable && !!options.default[key]
);
}
});
}
Expand Down Expand Up @@ -248,28 +251,31 @@ export class Completion implements CompletionInstance {
private completeOptionKey(
key: string,
completions: string[],
current: string
current: string,
negable: boolean
) {
const descs = this.usage.getDescriptions();
const startsByTwoDashes = (s: string) => /^--/.test(s);
const isShortOption = (s: string) => /^[^0-9]$/.test(s);
const dashes =
!startsByTwoDashes(current) && isShortOption(key) ? '-' : '--';
if (!this.zshShell) {
completions.push(dashes + key);
} else {
const aliasKey = this?.aliases?.[key].find(alias => {
let keyWithDesc = key;
if (this.zshShell) {
const descs = this.usage.getDescriptions();
const aliasKey = this?.aliases?.[key]?.find(alias => {
const desc = descs[alias];
return typeof desc === 'string' && desc.length > 0;
});
const descFromAlias = aliasKey ? descs[aliasKey] : undefined;
const desc = descs[key] ?? descFromAlias ?? '';
completions.push(
dashes +
`${key.replace(/:/g, '\\:')}:${desc
.replace('__yargsString__:', '')
.replace(/(\r\n|\n|\r)/gm, ' ')}`
);
keyWithDesc = `${key.replace(/:/g, '\\:')}:${desc
.replace('__yargsString__:', '')
.replace(/(\r\n|\n|\r)/gm, ' ')}`;
}

const startsByTwoDashes = (s: string) => /^--/.test(s);
const isShortOption = (s: string) => /^[^0-9]$/.test(s);
const dashes =
!startsByTwoDashes(current) && isShortOption(key) ? '-' : '--';

completions.push(dashes + keyWithDesc);
if (negable) {
completions.push(dashes + 'no-' + keyWithDesc);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yargs",
"version": "17.7.1",
"version": "17.7.2",
"description": "yargs the modern, pirate-themed, successor to optimist.",
"main": "./index.cjs",
"exports": {
Expand Down
22 changes: 22 additions & 0 deletions test/completion.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,28 @@ describe('Completion', () => {
r.logs.should.include('--foo:bar');
});

it('completes with no- prefix flags defaulting to true when boolean-negation is set', () => {
process.env.SHELL = '/bin/zsh';

const r = checkUsage(
() =>
yargs(['./completion', '--get-yargs-completions', '--'])
.options({
foo: {describe: 'foo flag', type: 'boolean', default: true},
bar: {describe: 'bar flag', type: 'boolean'},
})
.parserConfiguration({'boolean-negation': true}).argv
);

r.logs.should.eql([
'--help:Show help',
'--version:Show version number',
'--foo:foo flag',
'--no-foo:foo flag',
'--bar:bar flag',
]);
});

it('bails out early when full command matches', () => {
process.env.SHELL = '/bin/zsh';
const r = checkUsage(() => {
Expand Down

0 comments on commit 48bc718

Please sign in to comment.