Skip to content

Commit

Permalink
fix: correctly parse positional arguments (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastiaan van Arkens <[email protected]>
  • Loading branch information
eskydar and Sebastiaan van Arkens authored Mar 24, 2023
1 parent 01bea6c commit 514f9b8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export function parseArgs(rawArgs: string[], argsDef: ArgsDef): ParsedArgs {
}

const parsed = parseRawArgs(rawArgs, parseOptions);
const [, ...positionalArguments] = parsed._;

for (const [i, arg] of args.entries()) {
for (const [, arg] of args.entries()) {
if (arg.type === "positional") {
if (parsed._[i] !== undefined) {
parsed[arg.name] = parsed._[i];
const nextPositionalArgument = positionalArguments.shift();
if (nextPositionalArgument !== undefined) {
parsed[arg.name] = nextPositionalArgument;
} else if (arg.default !== undefined) {
parsed[arg.name] = arg.default;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function runCommand(

// Handle sub command
const subCommands = await resolveValue(cmd.subCommands);
if (subCommands && Object.keys(subCommands.length > 0)) {
if (subCommands && Object.keys(subCommands).length > 0) {
const subCommandArgIndex = opts.rawArgs.findIndex(
(arg) => !arg.startsWith("-")
);
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function resolveSubCommand(
parent?: CommandDef
): Promise<[CommandDef, CommandDef?]> {
const subCommands = await resolveValue(cmd.subCommands);
if (subCommands && Object.keys(subCommands.length > 0)) {
if (subCommands && Object.keys(subCommands).length > 0) {
const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));
const subCommandName = rawArgs[subCommandArgIndex];
const subCommand = await resolveValue(subCommands[subCommandName]);
Expand Down

0 comments on commit 514f9b8

Please sign in to comment.