{"id":"3182cf9d6090946b","repo":"tj/commander.js","slug":"can-not-call-parse-again-when-storeoptionsasproper","errorCode":null,"errorMessage":"Can not call parse again when storeOptionsAsProperties is true.\n- either make a new Command for each call to parse, or stop storing options as properties","messagePattern":"Can not call parse again when storeOptionsAsProperties is true\\.\n- either make a new Command for each call to parse, or stop storing options as properties","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/command.js","lineNumber":1173,"sourceCode":"    this._savedState = {\n      // name is stable if supplied by author, but may be unspecified for root command and deduced during parsing\n      _name: this._name,\n      // option values before parse have default values (including false for negated options)\n      // shallow clones\n      _optionValues: { ...this._optionValues },\n      _optionValueSources: { ...this._optionValueSources },\n    };\n  }\n\n  /**\n   * Restore state before parse for calls after the first.\n   * Not usually called directly, but available for subclasses to save their custom state.\n   *\n   * This is called in a lazy way. Only commands used in parsing chain will have state restored.\n   */\n  restoreStateBeforeParse() {\n    if (this._storeOptionsAsProperties)\n      throw new Error(`Can not call parse again when storeOptionsAsProperties is true.\n- either make a new Command for each call to parse, or stop storing options as properties`);\n\n    // clear state from _prepareUserArgs\n    this._name = this._savedState._name;\n    this._scriptPath = null;\n    this.rawArgs = [];\n    // clear state from setOptionValueWithSource\n    this._optionValues = { ...this._savedState._optionValues };\n    this._optionValueSources = { ...this._savedState._optionValueSources };\n    // clear state from _parseCommand\n    this.args = [];\n    // clear state from _processArguments\n    this.processedArgs = [];\n  }\n\n  /**\n   * Throw if expected executable is missing. Add lots of help for author.\n   *","sourceCodeStart":1155,"sourceCodeEnd":1191,"githubUrl":"https://github.com/tj/commander.js/blob/ba6d13ddb4243e5913367734f8c159089ffe7834/lib/command.js#L1155-L1191","documentation":"Thrown by Command.restoreStateBeforeParse() at lib/command.js:1171-1174 when parse() is called a second time on the same Command while storeOptionsAsProperties is true. With property-storage, option values live directly on `this` and cannot be cleanly reset across parses (the saved state only deep-restores _optionValues/_optionValueSources). Commander forbids the combo rather than risk stale or merged state.","triggerScenarios":"`program.storeOptionsAsProperties(); program.parse(); /* later */ program.parse();` — second parse hits the guard. Common in tests that parse twice, or long-lived REPL/server embedding Commander.","commonSituations":"Test suites reusing the program instance; interactive tools that re-parse user input; migration from Commander v4 where property storage + re-parse happened to work.","solutions":["Stop storing options as properties: `program.storeOptionsAsProperties(false)` (or just omit the call) and access via `.opts()`. Then re-parse is allowed.","Or construct a fresh Command for each parse call.","In tests, use beforeEach to rebuild the program rather than reusing."],"exampleFix":"// before (throws on second parse)\nprogram.storeOptionsAsProperties();\nprogram.parse();\nprogram.parse();\n\n// after (re-parse allowed, use .opts())\nprogram.parse();\nprogram.parse();\nconst opts = program.opts();","handlingStrategy":"fallback","validationCode":"// Detect the risky combo before second parse\nfunction assertCanReparse(cmd) {\n  if (cmd._storeOptionsAsProperties) {\n    throw new Error('Cannot re-parse with storeOptionsAsProperties=true; rebuild the Command or use .opts()');\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to storeOptionsAsProperties(false) and read via .opts().","Rebuild the Command in tests via beforeEach instead of re-parsing one instance.","Document in code comments anywhere parse() may be called more than once."],"tags":["commander","parse","option-storage","reparse","configuration"],"analyzedSha":"ba6d13ddb4243e5913367734f8c159089ffe7834","analyzedAt":"2026-08-03T20:26:04.326Z","schemaVersion":2}