Skip to content

Commit

Permalink
Implement nargs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangoree committed Apr 22, 2024
1 parent 9742d9d commit 47bf81f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 10 additions & 8 deletions packages/clide-js/src/core/options/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PromptOptions } from 'src/core/client';
import { MaybeReadonly } from 'src/utils/types';

/**
Expand All @@ -21,7 +20,10 @@ export interface OptionConfig<
alias?: MaybeReadonly<TAlias[]>;
/** The type of the option. */
type: T;
/** Whether the option is a string (optional, inferred from `type`). */
/**
* Whether the option is a string (optional, inferred from `type`). Useful
* for array options to specify the type of the array values.
*/
string?: boolean;
/** The number of arguments the option accepts (optional). */
nargs?: number;
Expand Down Expand Up @@ -69,9 +71,9 @@ export type OptionPrimitiveType<T extends OptionType = OptionType> = T extends
| 'secret'
? string
: T extends 'number'
? number
: T extends 'boolean'
? boolean
: T extends 'array'
? string[]
: never;
? number
: T extends 'boolean'
? boolean
: T extends 'array'
? string[]
: never;
7 changes: 5 additions & 2 deletions packages/clide-js/src/core/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ export function parseCommand(
boolean: [],
number: [],
string: [],
narg: {},
};

for (const [key, option] of Object.entries(optionsConfig)) {
if (option.alias) {
parseOptions.alias![key] = option.alias as string[];
}

if (option.nargs) {
parseOptions.narg![key] = option.nargs;
}

switch (option.type) {
case 'array':
parseOptions.array!.push(key as any);
Expand All @@ -81,8 +86,6 @@ export function parseCommand(
}
}

// TODO: const nargs: Record<string, number> = {};

// Parse the command string with yargs-parser
let { _: tokens, ...options } = parse(commandString, {
...parseOptions,
Expand Down

0 comments on commit 47bf81f

Please sign in to comment.