Skip to content

Commit 77cc76a

Browse files
committed
chore: clean up whitespace and formatting in commander files
1 parent 06a4dcc commit 77cc76a

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

demo.commander.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ program
3131
});
3232

3333
// Command with subcommands
34-
const deploy = program
35-
.command('deploy')
36-
.description('Deploy the application');
34+
const deploy = program.command('deploy').description('Deploy the application');
3735

3836
deploy
3937
.command('staging')
@@ -113,7 +111,7 @@ for (const command of completion.commands.values()) {
113111
if (process.argv[2] === 'test-completion') {
114112
const args = process.argv.slice(3);
115113
console.log('Testing completion with args:', args);
116-
114+
117115
// Special case for deploy command with a space at the end
118116
if (args.length === 1 && args[0] === 'deploy ') {
119117
console.log('staging Deploy to staging environment');
@@ -127,4 +125,4 @@ if (process.argv[2] === 'test-completion') {
127125
} else {
128126
// Parse command line arguments
129127
program.parse();
130-
}
128+
}

src/commander.ts

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function tab(instance: CommanderCommand): Completion {
2323

2424
// Process the root command
2525
processRootCommand(completion, instance, programName);
26-
26+
2727
// Process all subcommands
2828
processSubcommands(completion, instance, programName);
2929

@@ -36,15 +36,15 @@ export default function tab(instance: CommanderCommand): Completion {
3636
// Check if there are arguments after --
3737
const dashDashIndex = process.argv.indexOf('--');
3838
let extra: string[] = [];
39-
39+
4040
if (dashDashIndex !== -1) {
4141
extra = process.argv.slice(dashDashIndex + 1);
4242
// If shell is actually part of the extra args, adjust accordingly
4343
if (shell && extra.length > 0 && shell === '--') {
4444
shell = undefined;
4545
}
4646
}
47-
47+
4848
switch (shell) {
4949
case 'zsh': {
5050
const script = zsh.generate(programName, x);
@@ -72,7 +72,9 @@ export default function tab(instance: CommanderCommand): Completion {
7272
collectCommands(instance, '', commandMap);
7373
console.log('Collected commands:');
7474
for (const [path, cmd] of commandMap.entries()) {
75-
console.log(`- ${path || '<root>'}: ${cmd.description() || 'No description'}`);
75+
console.log(
76+
`- ${path || '<root>'}: ${cmd.description() || 'No description'}`
77+
);
7678
}
7779
break;
7880
}
@@ -92,20 +94,15 @@ function processRootCommand(
9294
programName: string
9395
): void {
9496
// Add the root command
95-
completion.addCommand(
96-
'',
97-
command.description() || '',
98-
[],
99-
async () => []
100-
);
97+
completion.addCommand('', command.description() || '', [], async () => []);
10198

10299
// Add root command options
103100
for (const option of command.options) {
104101
// Extract short flag from the name if it exists (e.g., "-c, --config" -> "c")
105102
const flags = option.flags;
106103
const shortFlag = flags.match(/^-([a-zA-Z]), --/)?.[1];
107104
const longFlag = flags.match(/--([a-zA-Z0-9-]+)/)?.[1];
108-
105+
109106
if (longFlag) {
110107
completion.addOption(
111108
'',
@@ -125,35 +122,30 @@ function processSubcommands(
125122
): void {
126123
// Build a map of command paths
127124
const commandMap = new Map<string, CommanderCommand>();
128-
125+
129126
// Collect all commands with their full paths
130127
collectCommands(rootCommand, '', commandMap);
131-
128+
132129
// Process each command
133130
for (const [path, cmd] of commandMap.entries()) {
134131
if (path === '') continue; // Skip root command, already processed
135-
132+
136133
// Extract positional arguments from usage
137134
const usage = cmd.usage();
138135
const args = (usage?.match(/\[.*?\]|<.*?>/g) || []).map((arg) =>
139136
arg.startsWith('[')
140137
); // true if optional (wrapped in [])
141138

142139
// Add command to completion
143-
completion.addCommand(
144-
path,
145-
cmd.description() || '',
146-
args,
147-
async () => []
148-
);
140+
completion.addCommand(path, cmd.description() || '', args, async () => []);
149141

150142
// Add command options
151143
for (const option of cmd.options) {
152144
// Extract short flag from the name if it exists (e.g., "-c, --config" -> "c")
153145
const flags = option.flags;
154146
const shortFlag = flags.match(/^-([a-zA-Z]), --/)?.[1];
155147
const longFlag = flags.match(/--([a-zA-Z0-9-]+)/)?.[1];
156-
148+
157149
if (longFlag) {
158150
completion.addOption(
159151
path,
@@ -164,16 +156,16 @@ function processSubcommands(
164156
);
165157
}
166158
}
167-
159+
168160
// For commands with subcommands, add a special handler
169161
if (cmd.commands.length > 0) {
170162
const subcommandNames = cmd.commands
171-
.filter(subcmd => subcmd.name() !== 'complete')
172-
.map(subcmd => ({
163+
.filter((subcmd) => subcmd.name() !== 'complete')
164+
.map((subcmd) => ({
173165
value: subcmd.name(),
174-
description: subcmd.description() || ''
166+
description: subcmd.description() || '',
175167
}));
176-
168+
177169
if (subcommandNames.length > 0) {
178170
const cmdObj = completion.commands.get(path);
179171
if (cmdObj) {
@@ -191,18 +183,18 @@ function collectCommands(
191183
): void {
192184
// Add this command to the map
193185
commandMap.set(parentPath, command);
194-
186+
195187
// Process subcommands
196188
for (const subcommand of command.commands) {
197189
// Skip the completion command
198190
if (subcommand.name() === 'complete') continue;
199-
191+
200192
// Build the full path for this subcommand
201-
const subcommandPath = parentPath
193+
const subcommandPath = parentPath
202194
? `${parentPath} ${subcommand.name()}`
203195
: subcommand.name();
204-
196+
205197
// Recursively collect subcommands
206198
collectCommands(subcommand, subcommandPath, commandMap);
207199
}
208-
}
200+
}

0 commit comments

Comments
 (0)