Skip to content

Commit

Permalink
Add typings
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Dec 21, 2024
1 parent 90fb1b9 commit 5058a07
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,8 @@ Expecting one of '${allowedValues.join("', '")}'`);
* Parse options from `argv` removing known options,
* and return argv split into operands and unknown arguments.
*
* Side effects: modifies command by storing options. Does not reset state if called again.
*
* Examples:
*
* argv => operands, unknown
Expand Down
18 changes: 18 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,28 @@ export class Command {
parseOptions?: ParseOptions,
): Promise<this>;

/**
* Called the first time parse is called to save state and allow a restore before subsequent calls to parse.
* Not usually called directly, but available for subclasses to save their custom state.
*
* This is called in a lazy way. Only commands used in parsing chain will have state saved.
*/
saveStateBeforeParse(): void;

/**
* Restore state before parse for calls after the first.
* Not usually called directly, but available for subclasses to save their custom state.
*
* This is called in a lazy way. Only commands used in parsing chain will have state restored.
*/
restoreStateBeforeParse(): void;

/**
* Parse options from `argv` removing known options,
* and return argv split into operands and unknown arguments.
*
* Side effects: modifies command by storing options. Does not reset state if called again.
*
* argv => operands, unknown
* --known kkk op => [op], []
* op --known kkk => [op], []
Expand Down
4 changes: 4 additions & 0 deletions typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ expectType<{ operands: string[]; unknown: string[] }>(
program.parseOptions(['node', 'script.js', 'hello']),
);

// save/restore state
expectType<void>(program.saveStateBeforeParse());
expectType<void>(program.restoreStateBeforeParse());

// opts
const opts = program.opts();
expectType<commander.OptionValues>(opts);
Expand Down

0 comments on commit 5058a07

Please sign in to comment.