Skip to content

Commit dc60691

Browse files
committed
apply suggestions
1 parent 0f7bbf8 commit dc60691

5 files changed

Lines changed: 11 additions & 34 deletions

File tree

‎src/commander.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import t, {
44
type RootCommand,
55
type OptionHandler,
66
} from './t';
7-
import { setOptionLookupMode } from './option-scope';
87

98
// rawArgs is available on (just) the Commander root command, but is not included in the TypeScript types.
109
interface CommandWithRawArgs extends CommanderCommand {
@@ -23,7 +22,7 @@ export default function tab(
2322
const programName = instance.name();
2423
const positionalOptions = (instance as CommandWithPositionalOptions)
2524
._enablePositionalOptions;
26-
setOptionLookupMode(t, positionalOptions ? 'local' : 'parent-first');
25+
t.optionLookupMode = positionalOptions ? 'local' : 'parent-first';
2726

2827
// Make a `completion` command with a required command-argument.
2928
const completionCommandName =

‎src/option-scope.ts‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎src/t.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ import * as bash from './bash';
135135
import * as fish from './fish';
136136
import * as powershell from './powershell';
137137
import assert from 'node:assert';
138-
import { getOptionLookupMode } from './option-scope';
139138

140139
export class RootCommand extends Command {
141140
commands = new Map<string, Command>();
142141
completions: Completion[] = [];
143142
directive = ShellCompDirective.ShellCompDirectiveDefault;
143+
optionLookupMode?: 'parent-first' | 'local';
144144

145145
constructor() {
146146
super('', '');
@@ -154,10 +154,6 @@ export class RootCommand extends Command {
154154
return c;
155155
}
156156

157-
private get optionLookupMode() {
158-
return getOptionLookupMode(this);
159-
}
160-
161157
private scanArguments(args: string[]): {
162158
command: Command;
163159
parts: string[];

‎tests/commander-option-scope.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ describe('Commander option scope', () => {
3939
]);
4040
});
4141

42-
it('keeps a non-conflicting leaf long option available', async () => {
42+
it('keeps a non-conflicting serve long option available', async () => {
4343
await expect(
44-
complete(['serve', '--leaf-choice', ''], false)
45-
).resolves.toEqual(['leaf', 'local']);
44+
complete(['serve', '--serve-choice', ''], false)
45+
).resolves.toEqual(['serve', 'local']);
4646
});
4747

4848
it('keeps root option values available after a subcommand', async () => {
@@ -74,7 +74,7 @@ describe('Commander option scope', () => {
7474
describe('positional options', () => {
7575
it('uses the subcommand option when it reuses a root short flag', async () => {
7676
await expect(complete(['serve', '-c', ''], true)).resolves.toEqual([
77-
'leaf',
77+
'serve',
7878
'local',
7979
]);
8080
});
@@ -92,10 +92,10 @@ describe('Commander option scope', () => {
9292
it('still accepts root options before the subcommand', async () => {
9393
await expect(
9494
complete(['--root-choice', 'top', 'serve', '-c', ''], true)
95-
).resolves.toEqual(['leaf', 'local']);
95+
).resolves.toEqual(['serve', 'local']);
9696
});
9797

98-
it('uses leaf option arity for a colliding short flag', async () => {
98+
it('uses serve option arity for a colliding short flag', async () => {
9999
await expect(complete(['serve', '-b', ''], true)).resolves.toEqual([
100100
'blue',
101101
'green',

‎tests/fixtures/commander-option-scope.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ const serve = program
2020
.command('serve')
2121
.description('Serve the app')
2222
.addOption(
23-
new Option('-c, --leaf-choice <value>', 'Leaf choice').choices([
24-
'leaf',
23+
new Option('-c, --serve-choice <value>', 'Serve choice').choices([
24+
'serve',
2525
'local',
2626
])
2727
)
2828
.addOption(
29-
new Option('-b, --leaf-value <value>', 'Leaf value').choices([
29+
new Option('-b, --serve-value <value>', 'Serve value').choices([
3030
'blue',
3131
'green',
3232
])

0 commit comments

Comments
 (0)