diff --git a/lib/completion.ts b/lib/completion.ts index 207028831..1a9075696 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -193,7 +193,8 @@ export class Completion implements CompletionInstance { return (this.customCompletionFunction as FallbackCompletionFunction)( current, argv, - () => this.defaultCompletion(args, argv, current, done), + (onCompleted = done) => + this.defaultCompletion(args, argv, current, onCompleted), completions => { done(null, completions); } @@ -278,7 +279,7 @@ interface FallbackCompletionFunction { ( current: string, argv: Arguments, - defaultCompletion: () => any, + completionFilter: (onCompleted?: CompletionCallback) => any, done: (completions: string[]) => any ): any; } diff --git a/test/completion.cjs b/test/completion.cjs index 9fc774bd3..10b90661c 100644 --- a/test/completion.cjs +++ b/test/completion.cjs @@ -341,7 +341,7 @@ describe('Completion', () => { r.logs.should.include('success!'); }); - it('allows the custom completion function to use the standard one', done => { + it('allows the custom completion function to use the default completion w/o filter', done => { checkUsage( () => { yargs(['./completion', '--get-yargs-completions']) @@ -349,8 +349,8 @@ describe('Completion', () => { .command('apple', 'banana') .completion( 'completion', - (current, argv, defaultCompletion, done) => { - defaultCompletion(); + (current, argv, completionFilter, done) => { + completionFilter(); } ) .parse(); @@ -365,6 +365,35 @@ describe('Completion', () => { ); }); + it('allows custom completion to be combined with default completion, using filter', done => { + checkUsage( + () => { + yargs(['./completion', '--get-yargs-completions']) + .command('foo', 'bar') + .command('apple', 'banana') + .completion( + 'completion', + (current, argv, completionFilter, done) => { + completionFilter((err, completions) => { + const filteredCompletions = completions.filter( + completion => completion === 'foo' + ); + done(filteredCompletions); + }); + } + ) + .parse(); + }, + null, + (err, r) => { + if (err) throw err; + r.logs.should.include('foo'); + r.logs.should.not.include('apple'); + return done(); + } + ); + }); + it('allows calling callback instead of default completion function', done => { checkUsage( () => { @@ -373,7 +402,7 @@ describe('Completion', () => { .command('apple', 'banana') .completion( 'completion', - (current, argv, defaultCompletion, done) => { + (current, argv, completionFilter, done) => { done(['orange']); } )