Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly parse positional arguments #16

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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