Skip to content

Commit a00de33

Browse files
committed
Add Commander dev command options and custom completions expected by tests
1 parent b6d4942 commit a00de33

3 files changed

Lines changed: 153 additions & 39 deletions

File tree

examples/demo.commander.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,24 @@ if (logLevelOption) {
110110
};
111111
}
112112

113+
// Options on dev command
114+
const devCommandInstance = completion.commands.get('dev');
115+
if (devCommandInstance) {
116+
const portOption = devCommandInstance.options.get('port');
117+
if (portOption) {
118+
portOption.handler = (complete) => {
119+
complete('3000', 'Development server port');
120+
complete('8080', 'Alternative port');
121+
};
122+
}
123+
const hostOption = devCommandInstance.options.get('host');
124+
if (hostOption) {
125+
hostOption.handler = (complete) => {
126+
complete('localhost', 'Localhost');
127+
complete('127.0.0.1', 'Localhost IP');
128+
};
129+
}
130+
}
131+
113132
// Parse command line arguments
114133
program.parse();

tests/__snapshots__/cli.test.ts.snap

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,50 @@ my-tool My tool
626626
"
627627
`;
628628

629+
exports[`cli completion tests for commander > --config option tests > should complete --config option values 1`] = `
630+
"vite.config.ts Vite config file
631+
vite.config.js Vite config file
632+
:4
633+
"
634+
`;
635+
636+
exports[`cli completion tests for commander > --config option tests > should complete --config option with equals sign 1`] = `
637+
"vite.config.ts Vite config file
638+
vite.config.js Vite config file
639+
:4
640+
"
641+
`;
642+
643+
exports[`cli completion tests for commander > --config option tests > should complete --config option with partial input 1`] = `
644+
"vite.config.ts Vite config file
645+
vite.config.js Vite config file
646+
:4
647+
"
648+
`;
649+
650+
exports[`cli completion tests for commander > --config option tests > should complete short flag -c option values 1`] = `
651+
"vite.config.ts Vite config file
652+
vite.config.js Vite config file
653+
:4
654+
"
655+
`;
656+
657+
exports[`cli completion tests for commander > --config option tests > should complete short flag -c option with partial input 1`] = `
658+
"vite.config.ts Vite config file
659+
vite.config.js Vite config file
660+
:4
661+
"
662+
`;
663+
664+
exports[`cli completion tests for commander > --config option tests > should not suggest --config after it has been used 1`] = `
665+
"--version output the version number
666+
--config Use specified config file
667+
--mode Set env mode
668+
--logLevel Specify log level
669+
:4
670+
"
671+
`;
672+
629673
exports[`cli completion tests for commander > cli option completion tests > should complete option for partial input '{ partial: '--p', expected: '--port' }' 1`] = `
630674
"--port Specify port
631675
:4
@@ -660,6 +704,39 @@ exports[`cli completion tests for commander > cli option value handling > should
660704
"
661705
`;
662706

707+
exports[`cli completion tests for commander > cli option value handling > should resolve config option values correctly 1`] = `
708+
"vite.config.ts Vite config file
709+
vite.config.js Vite config file
710+
:4
711+
"
712+
`;
713+
714+
exports[`cli completion tests for commander > cli option value handling > should resolve port value correctly 1`] = `
715+
"3000 Development server port
716+
:4
717+
"
718+
`;
719+
720+
exports[`cli completion tests for commander > edge case completions for end with space > should keep suggesting the --port option if user typed partial but didn't end with space 1`] = `
721+
"--port Specify port
722+
:4
723+
"
724+
`;
725+
726+
exports[`cli completion tests for commander > edge case completions for end with space > should suggest port values if user ends with space after \`--port\` 1`] = `
727+
"3000 Development server port
728+
8080 Alternative port
729+
:4
730+
"
731+
`;
732+
733+
exports[`cli completion tests for commander > edge case completions for end with space > should suggest port values if user typed \`--port=\` and hasn't typed a space or value yet 1`] = `
734+
"3000 Development server port
735+
8080 Alternative port
736+
:4
737+
"
738+
`;
739+
663740
exports[`cli completion tests for commander > root command option tests > should complete root command --logLevel option values 1`] = `
664741
"info Info level
665742
warn Warn level
@@ -719,6 +796,33 @@ production Production mode
719796
"
720797
`;
721798

799+
exports[`cli completion tests for commander > short flag handling > should handle global short flags 1`] = `
800+
"-c Use specified config file
801+
:4
802+
"
803+
`;
804+
805+
exports[`cli completion tests for commander > short flag handling > should handle short flag value completion 1`] = `
806+
"-p Specify port
807+
:4
808+
"
809+
`;
810+
811+
exports[`cli completion tests for commander > short flag handling > should handle short flag with equals sign 1`] = `
812+
"3000 Development server port
813+
:4
814+
"
815+
`;
816+
817+
exports[`cli completion tests for commander > short flag handling > should not show duplicate options when short flag is used 1`] = `
818+
"--version output the version number
819+
--config Use specified config file
820+
--mode Set env mode
821+
--logLevel Specify log level
822+
:4
823+
"
824+
`;
825+
722826
exports[`cli completion tests for commander > should complete cli options 1`] = `
723827
"dev Start dev server
724828
serve Start the server

tests/cli.test.ts

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,23 @@ describe.each(cliTools)('cli completion tests for %s', (cliTool) => {
5959
});
6060

6161
describe('cli option value handling', () => {
62-
it.runIf(!shouldSkipTest)(
63-
'should resolve port value correctly',
64-
async () => {
65-
const command = `${commandPrefix} dev --port=3`;
66-
const output = await runCommand(command);
67-
expect(output).toMatchSnapshot();
68-
}
69-
);
62+
it('should resolve port value correctly', async () => {
63+
const command = `${commandPrefix} dev --port=3`;
64+
const output = await runCommand(command);
65+
expect(output).toMatchSnapshot();
66+
});
7067

7168
it('should not show duplicate options', async () => {
7269
const command = `${commandPrefix} --config vite.config.js --`;
7370
const output = await runCommand(command);
7471
expect(output).toMatchSnapshot();
7572
});
7673

77-
it.runIf(!shouldSkipTest)(
78-
'should resolve config option values correctly',
79-
async () => {
80-
const command = `${commandPrefix} --config vite.config`;
81-
const output = await runCommand(command);
82-
expect(output).toMatchSnapshot();
83-
}
84-
);
74+
it('should resolve config option values correctly', async () => {
75+
const command = `${commandPrefix} --config vite.config`;
76+
const output = await runCommand(command);
77+
expect(output).toMatchSnapshot();
78+
});
8579

8680
it('should handle unknown options with no completions', async () => {
8781
const command = `${commandPrefix} --unknownoption`;
@@ -124,7 +118,7 @@ describe.each(cliTools)('cli completion tests for %s', (cliTool) => {
124118
});
125119
});
126120

127-
describe.runIf(!shouldSkipTest)('option API overload tests', () => {
121+
describe('option API overload tests', () => {
128122
it('should handle basic option (name + description only) as boolean flag', async () => {
129123
// This tests the case: option('quiet', 'Suppress output')
130124
const command = `${commandPrefix} dev --quiet ""`;
@@ -198,7 +192,7 @@ describe.each(cliTools)('cli completion tests for %s', (cliTool) => {
198192
});
199193
});
200194

201-
describe.runIf(!shouldSkipTest)('--config option tests', () => {
195+
describe('--config option tests', () => {
202196
it('should complete --config option values', async () => {
203197
const command = `${commandPrefix} --config ""`;
204198
const output = await runCommand(command);
@@ -312,30 +306,27 @@ describe.each(cliTools)('cli completion tests for %s', (cliTool) => {
312306
});
313307
});
314308

315-
describe.runIf(!shouldSkipTest)(
316-
'edge case completions for end with space',
317-
() => {
318-
it('should suggest port values if user ends with space after `--port`', async () => {
319-
const command = `${commandPrefix} dev --port ""`;
320-
const output = await runCommand(command);
321-
expect(output).toMatchSnapshot();
322-
});
309+
describe('edge case completions for end with space', () => {
310+
it('should suggest port values if user ends with space after `--port`', async () => {
311+
const command = `${commandPrefix} dev --port ""`;
312+
const output = await runCommand(command);
313+
expect(output).toMatchSnapshot();
314+
});
323315

324-
it("should keep suggesting the --port option if user typed partial but didn't end with space", async () => {
325-
const command = `${commandPrefix} dev --po`;
326-
const output = await runCommand(command);
327-
expect(output).toMatchSnapshot();
328-
});
316+
it("should keep suggesting the --port option if user typed partial but didn't end with space", async () => {
317+
const command = `${commandPrefix} dev --po`;
318+
const output = await runCommand(command);
319+
expect(output).toMatchSnapshot();
320+
});
329321

330-
it("should suggest port values if user typed `--port=` and hasn't typed a space or value yet", async () => {
331-
const command = `${commandPrefix} dev --port=`;
332-
const output = await runCommand(command);
333-
expect(output).toMatchSnapshot();
334-
});
335-
}
336-
);
322+
it("should suggest port values if user typed `--port=` and hasn't typed a space or value yet", async () => {
323+
const command = `${commandPrefix} dev --port=`;
324+
const output = await runCommand(command);
325+
expect(output).toMatchSnapshot();
326+
});
327+
});
337328

338-
describe.runIf(!shouldSkipTest)('short flag handling', () => {
329+
describe('short flag handling', () => {
339330
it('should handle short flag value completion', async () => {
340331
const command = `${commandPrefix} dev -p `;
341332
const output = await runCommand(command);

0 commit comments

Comments
 (0)