Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Dec 21, 2024
1 parent 8f3bae1 commit 90fb1b9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,19 +1114,33 @@ Expecting one of '${allowedValues.join("', '")}'`);
}
}

/**
* 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() {
this._savedState = {
// name is stable if supplied by author, but may be unspecified for root command and deduced during parsing
_name: this._name,
// option values before parse have default values (including false for negated options)
// shallow clones
_optionValues: { ...this._optionValues },
_optionValueSources: { ...this._optionValueSources },
};
}

/**
* 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() {
if (this._storeOptionsAsProperties)
throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
- make a new Command for each call to parse, or stop storing options as properties`);
- either make a new Command for each call to parse, or stop storing options as properties`);

// clear state from _prepareUserArgs
this._name = this._savedState._name;
Expand Down

0 comments on commit 90fb1b9

Please sign in to comment.