Skip to content

Commit d360e47

Browse files
authored
Merge pull request #1 from g9wp/main
fix capital option
2 parents b54a2e5 + faa0d70 commit d360e47

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/parse_args.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,28 @@ Deno.test({
187187
},
188188
});
189189

190+
Deno.test({
191+
name: "capital option",
192+
fn() {
193+
class Tool {
194+
@alias("H")
195+
helmet = false;
196+
main() {}
197+
}
198+
const res = cliteParse(Tool, {
199+
args: ["-H"],
200+
});
201+
assertEquals(res.command, "main");
202+
assertEquals(res.obj.helmet, true);
203+
204+
const res2 = cliteParse(Tool, {
205+
args: ["-h"],
206+
});
207+
assertEquals(res2.command, "--help");
208+
assertEquals(res2.obj.helmet, false);
209+
},
210+
});
211+
190212
Deno.test({
191213
name: "object option",
192214
fn() {

src/parse_args.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ export function parseArgs<O extends Obj>(
6969
}
7070

7171
const stdRes = stdParseArgs(args, {
72-
negatable: negatable.map(toKebabCase),
73-
string: stringProp.map(toKebabCase),
74-
boolean: booleanProp.map(toKebabCase),
75-
collect: arrayProp.map(toKebabCase),
72+
negatable: negatable,
73+
string: stringProp,
74+
boolean: booleanProp,
75+
collect: arrayProp,
7676
default: defaultValues,
7777
alias,
7878
stopEarly: true,
@@ -88,7 +88,6 @@ export function parseArgs<O extends Obj>(
8888
}
8989

9090
const fields = Object.keys(metadata.fields);
91-
const fieldsKebabCase = fields.map(toKebabCase);
9291
const aliasKey = Object.values(alias).flat();
9392

9493
for (const [key, value] of Object.entries(stdRes)) {
@@ -103,7 +102,6 @@ export function parseArgs<O extends Obj>(
103102
} else {
104103
if (
105104
key !== "help" &&
106-
!fieldsKebabCase.includes(key) &&
107105
!fields.includes(key) &&
108106
!aliasKey.includes(key) &&
109107
!((config?.configCli || metadata.jsonConfig) && key === "config")
@@ -112,7 +110,16 @@ export function parseArgs<O extends Obj>(
112110
cause: { clite: true },
113111
});
114112
}
115-
argsResult.options[toCamelCase(key)] = value;
113+
if ((config?.configCli || metadata.jsonConfig) && key === "config") {
114+
argsResult.options[key] = value;
115+
} else {
116+
for (const [name, aliases] of Object.entries(alias)) {
117+
if (name === key || aliases.includes(key)) {
118+
argsResult.options[name] = value;
119+
break;
120+
}
121+
}
122+
}
116123
}
117124
}
118125
return argsResult;

0 commit comments

Comments
 (0)