Skip to content

Commit 514f9b8

Browse files
eskydarSebastiaan van Arkens
and
Sebastiaan van Arkens
authored
fix: correctly parse positional arguments (#16)
Co-authored-by: Sebastiaan van Arkens <[email protected]>
1 parent 01bea6c commit 514f9b8

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/args.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ export function parseArgs(rawArgs: string[], argsDef: ArgsDef): ParsedArgs {
2626
}
2727

2828
const parsed = parseRawArgs(rawArgs, parseOptions);
29+
const [, ...positionalArguments] = parsed._;
2930

30-
for (const [i, arg] of args.entries()) {
31+
for (const [, arg] of args.entries()) {
3132
if (arg.type === "positional") {
32-
if (parsed._[i] !== undefined) {
33-
parsed[arg.name] = parsed._[i];
33+
const nextPositionalArgument = positionalArguments.shift();
34+
if (nextPositionalArgument !== undefined) {
35+
parsed[arg.name] = nextPositionalArgument;
3436
} else if (arg.default !== undefined) {
3537
parsed[arg.name] = arg.default;
3638
} else {

src/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function runCommand(
3131

3232
// Handle sub command
3333
const subCommands = await resolveValue(cmd.subCommands);
34-
if (subCommands && Object.keys(subCommands.length > 0)) {
34+
if (subCommands && Object.keys(subCommands).length > 0) {
3535
const subCommandArgIndex = opts.rawArgs.findIndex(
3636
(arg) => !arg.startsWith("-")
3737
);
@@ -68,7 +68,7 @@ export async function resolveSubCommand(
6868
parent?: CommandDef
6969
): Promise<[CommandDef, CommandDef?]> {
7070
const subCommands = await resolveValue(cmd.subCommands);
71-
if (subCommands && Object.keys(subCommands.length > 0)) {
71+
if (subCommands && Object.keys(subCommands).length > 0) {
7272
const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));
7373
const subCommandName = rawArgs[subCommandArgIndex];
7474
const subCommand = await resolveValue(subCommands[subCommandName]);

0 commit comments

Comments
 (0)