{"id":"bd153fea1f8e0c4a","repo":"tj/commander.js","slug":"call-storeoptionsasproperties-before-setting-op","errorCode":null,"errorMessage":"call .storeOptionsAsProperties() before setting option values","messagePattern":"call \\.storeOptionsAsProperties\\(\\) before setting option values","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/command.js","lineNumber":901,"sourceCode":"        `passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`,\n      );\n    }\n  }\n\n  /**\n   * Whether to store option values as properties on command object,\n   * or store separately (specify false). In both cases the option values can be accessed using .opts().\n   *\n   * @param {boolean} [storeAsProperties=true]\n   * @return {Command} `this` command for chaining\n   */\n\n  storeOptionsAsProperties(storeAsProperties = true) {\n    if (this.options.length) {\n      throw new Error('call .storeOptionsAsProperties() before adding options');\n    }\n    if (Object.keys(this._optionValues).length) {\n      throw new Error(\n        'call .storeOptionsAsProperties() before setting option values',\n      );\n    }\n    this._storeOptionsAsProperties = !!storeAsProperties;\n    return this;\n  }\n\n  /**\n   * Retrieve option value.\n   *\n   * @param {string} key\n   * @return {object} value\n   */\n\n  getOptionValue(key) {\n    if (this._storeOptionsAsProperties) {\n      return this[key];\n    }","sourceCodeStart":883,"sourceCodeEnd":919,"githubUrl":"https://github.com/tj/commander.js/blob/ba6d13ddb4243e5913367734f8c159089ffe7834/lib/command.js#L883-L919","documentation":"Thrown by Command.storeOptionsAsProperties() at lib/command.js:900-904 when option values have already been set (Object.keys(this._optionValues).length > 0) at the time the method is called. Switching storage mode after values exist would orphan them, so Commander refuses.","triggerScenarios":"Calling storeOptionsAsProperties after program.parse() has run (parse populates _optionValues), or after manually setting a default. Less common than error[11] but same root: storage mode must be decided before any value exists.","commonSituations":"Late initialization in a test that already parsed once; reusing a Command instance across parses with a mode switch in between; setting option defaults imperatively before declaring storage mode.","solutions":["Set storage mode at construction time, before parse and before setting any values.","Use a fresh Command instance per parse if you need different modes (also required because storeOptionsAsProperties=true disallows re-parse — see error[15]).","Prefer the modern default (storeOptionsAsProperties(false)) and access via .opts()."],"exampleFix":"// before (throws)\nprogram.parse(argv);\nprogram.storeOptionsAsProperties();\n\n// after\nprogram.storeOptionsAsProperties(); // before any parse\nprogram.parse(argv);","handlingStrategy":"validation","validationCode":"function configureStorage(cmd, asProperties) {\n  if (Object.keys(cmd._optionValues ?? {}).length) {\n    throw new Error('storage mode set after option values exist');\n  }\n  return cmd.storeOptionsAsProperties(asProperties);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set storage mode before program.parse() and before any default is applied.","Use a fresh Command per parse in tests.","Avoid storeOptionsAsProperties(true) entirely in new code."],"tags":["commander","option-storage","ordering","configuration"],"analyzedSha":"ba6d13ddb4243e5913367734f8c159089ffe7834","analyzedAt":"2026-08-03T20:26:04.326Z","schemaVersion":2}