Skip to content

Commit 9f36ff2

Browse files
authored
chore: cleaner comments (#90)
1 parent 0c9d718 commit 9f36ff2

File tree

3 files changed

+34
-77
lines changed

3 files changed

+34
-77
lines changed

src/cac.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ export default async function tab(
2626
instance: CAC,
2727
completionConfig?: CompletionConfig
2828
): Promise<RootCommand> {
29-
// Add all commands and their options
3029
for (const cmd of [instance.globalCommand, ...instance.commands]) {
31-
if (cmd.name === 'complete') continue; // Skip completion command
30+
if (cmd.name === 'complete') continue;
3231

33-
// Get positional args info from command usage
3432
const args = (cmd.rawName.match(/\[.*?\]|<.*?>/g) || []).map((arg) =>
3533
arg.startsWith('[')
3634
); // true if optional (wrapped in [])
@@ -40,22 +38,21 @@ export default async function tab(
4038
? completionConfig
4139
: completionConfig?.subCommands?.[cmd.name];
4240

43-
// Add command to completion using t.ts API
41+
// command
4442
const commandName = isRootCommand ? '' : cmd.name;
4543
const command = isRootCommand
4644
? t
4745
: t.command(commandName, cmd.description || '');
4846

49-
// Set args for the command
47+
// args (if has positional arguments)
5048
if (command) {
51-
// Extract argument names from command usage
5249
const argMatches =
5350
cmd.rawName.match(/<([^>]+)>|\[\.\.\.([^\]]+)\]/g) || [];
5451
const argNames = argMatches.map((match) => {
5552
if (match.startsWith('<') && match.endsWith('>')) {
56-
return match.slice(1, -1); // Remove < >
53+
return match.slice(1, -1);
5754
} else if (match.startsWith('[...') && match.endsWith(']')) {
58-
return match.slice(4, -1); // Remove [... ]
55+
return match.slice(4, -1);
5956
}
6057
return match;
6158
});
@@ -71,18 +68,17 @@ export default async function tab(
7168
});
7269
}
7370

74-
// Add command options
71+
// options
7572
for (const option of [...instance.globalCommand.options, ...cmd.options]) {
76-
// Extract short flag from the rawName if it exists (e.g., "-c, --config" -> "c")
73+
// short flag (if exists)
7774
const shortFlag = option.rawName.match(/^-([a-zA-Z]), --/)?.[1];
78-
const argName = option.name; // option.name is already clean (e.g., "config")
75+
const argName = option.name;
7976

80-
// Add option using t.ts API
8177
const targetCommand = isRootCommand ? t : command;
8278
if (targetCommand) {
8379
const handler = commandCompletionConfig?.options?.[argName];
8480

85-
// Check if option takes a value (has <> or [] in rawName, or is marked as required)
81+
// takes value (if has <> or [] in rawName, or is marked as required)
8682
const takesValue =
8783
option.required || VALUE_OPTION_RE.test(option.rawName);
8884

@@ -98,12 +94,12 @@ export default async function tab(
9894
targetCommand.option(argName, option.description || '', handler);
9995
}
10096
} else if (takesValue) {
101-
// Takes value but no custom handler = value option with no completions
97+
// value option (if takes value but no custom handler)
10298
if (shortFlag) {
10399
targetCommand.option(
104100
argName,
105101
option.description || '',
106-
async () => [], // Empty completions
102+
async () => [],
107103
shortFlag
108104
);
109105
} else {
@@ -114,7 +110,7 @@ export default async function tab(
114110
);
115111
}
116112
} else {
117-
// No custom handler and doesn't take value = boolean flag
113+
// boolean flag (if no custom handler and doesn't take value)
118114
if (shortFlag) {
119115
targetCommand.option(argName, option.description || '', shortFlag);
120116
} else {
@@ -153,13 +149,12 @@ export default async function tab(
153149
const args: string[] = extra['--'] || [];
154150
instance.showHelpOnExit = false;
155151

156-
// Parse current command context
152+
// command context
157153
instance.unsetMatchedCommand();
158154
instance.parse([execPath, processArgs[0], ...args], {
159155
run: false,
160156
});
161157

162-
// Use t.ts parse method instead of completion.parse
163158
return t.parse(args);
164159
}
165160
}

src/citty.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,14 @@ async function handleSubCommands(
4747
}
4848
const isPositional = isConfigPositional(config);
4949

50-
// Add command using t.ts API
5150
const commandName = parentCmd ? `${parentCmd} ${cmd}` : cmd;
5251
const command = t.command(commandName, meta.description);
5352

5453
// Set args for the command if it has positional arguments
5554
if (isPositional && config.args) {
56-
// Add arguments with completion handlers from subCompletionConfig args
5755
for (const [argName, argConfig] of Object.entries(config.args)) {
5856
const conf = argConfig as ArgDef;
5957
if (conf.type === 'positional') {
60-
// Check if this is a variadic argument (required: false for variadic in citty)
6158
const isVariadic = conf.required === false;
6259
const argHandler = subCompletionConfig?.args?.[argName];
6360
if (argHandler) {
@@ -69,7 +66,7 @@ async function handleSubCommands(
6966
}
7067
}
7168

72-
// Handle nested subcommands recursively
69+
// subcommands (recursive)
7370
if (subCommands) {
7471
await handleSubCommands(
7572
subCommands,
@@ -78,29 +75,29 @@ async function handleSubCommands(
7875
);
7976
}
8077

81-
// Handle arguments
78+
// args
8279
if (config.args) {
8380
for (const [argName, argConfig] of Object.entries(config.args)) {
8481
const conf = argConfig as ArgDef;
85-
// Extract alias from the config if it exists
82+
// alias (if exists)
8683
const shortFlag =
8784
typeof conf === 'object' && 'alias' in conf
8885
? Array.isArray(conf.alias)
8986
? conf.alias[0]
9087
: conf.alias
9188
: undefined;
9289

93-
// Add option using t.ts API - store without -- prefix
90+
// option (without -- prefix)
9491
const handler = subCompletionConfig?.options?.[argName];
9592
if (handler) {
96-
// Has custom handler → value option
93+
// value option (if has custom handler)
9794
if (shortFlag) {
9895
command.option(argName, conf.description ?? '', handler, shortFlag);
9996
} else {
10097
command.option(argName, conf.description ?? '', handler);
10198
}
10299
} else {
103-
// No custom handler → boolean flag
100+
// boolean flag (if no custom handler)
104101
if (shortFlag) {
105102
command.option(argName, conf.description ?? '', shortFlag);
106103
} else {
@@ -130,7 +127,7 @@ export default async function tab<TArgs extends ArgsDef>(
130127

131128
const isPositional = isConfigPositional(instance);
132129

133-
// Set args for the root command if it has positional arguments
130+
// args (if has positional arguments)
134131
if (isPositional && instance.args) {
135132
for (const [argName, argConfig] of Object.entries(instance.args)) {
136133
const conf = argConfig as PositionalArgDef;
@@ -158,25 +155,21 @@ export default async function tab<TArgs extends ArgsDef>(
158155
for (const [argName, argConfig] of Object.entries(instance.args)) {
159156
const conf = argConfig as ArgDef;
160157

161-
// Extract alias (same logic as subcommands)
162158
const shortFlag =
163159
typeof conf === 'object' && 'alias' in conf
164160
? Array.isArray(conf.alias)
165161
? conf.alias[0]
166162
: conf.alias
167163
: undefined;
168164

169-
// Add option using t.ts API - store without -- prefix
170165
const handler = completionConfig?.options?.[argName];
171166
if (handler) {
172-
// Has custom handler → value option
173167
if (shortFlag) {
174168
t.option(argName, conf.description ?? '', handler, shortFlag);
175169
} else {
176170
t.option(argName, conf.description ?? '', handler);
177171
}
178172
} else {
179-
// No custom handler → boolean flag
180173
if (shortFlag) {
181174
t.option(argName, conf.description ?? '', shortFlag);
182175
} else {
@@ -235,7 +228,6 @@ export default async function tab<TArgs extends ArgsDef>(
235228
assertDoubleDashes(name);
236229

237230
const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1);
238-
// Use t.ts parse method instead of completion.parse
239231
return t.parse(extra);
240232
}
241233
}

0 commit comments

Comments
 (0)