{"record":{"id":"0775aaaccb854c3d","repo":"puppetlabs/puppet","slug":"name-can-t-be-optional-and-have-a-default-value","errorCode":null,"errorMessage":"%{name} can't be optional and have a default value","messagePattern":"%(.+?) can't be optional and have a default value","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/interface/option.rb","lineNumber":128,"sourceCode":"  def takes_argument?\n    !!@argument\n  end\n\n  def optional_argument?\n    !!@optional_argument\n  end\n\n  def required?\n    !!@required\n  end\n\n  def has_default?\n    !!@default\n  end\n\n  def default=(proc)\n    if required\n      raise ArgumentError, _(\"%{name} can't be optional and have a default value\") % { name: self }\n    end\n\n    unless proc.is_a? Proc\n      # TRANSLATORS 'proc' is a Ruby block of code\n      raise ArgumentError, _(\"default value for %{name} is a %{class_name}, not a proc\") %\n                           { name: self, class_name: proc.class.name.inspect }\n    end\n    @default = proc\n  end\n\n  def default\n    @default and @default.call\n  end\n\n  attr_reader :parent, :name, :aliases, :optparse, :required\n\n  def required=(value)\n    if has_default?","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/interface/option.rb#L110-L146","documentation":"Raised by Puppet::Interface::Option#default= (lib/puppet/interface/option.rb:128) when a default-value proc is assigned to an option that is already marked required. An option cannot be both required (caller must pass it) and defaulted (face fills it in when absent), so Puppet rejects the combination at face-definition time. Despite the wording, the guard fires when `required` is truthy: required and default_to are mutually exclusive, whichever is set second loses.","triggerScenarios":"Inside a Faces option block: `option '--environment' do required; default_to { 'production' } end` (required is set first, then default_to assigns via default=). Or calling `opt.default = proc` directly on a Puppet::Interface::Option whose `required?` returns true.","commonSituations":"Editing a Face where an option changed from required to optional and the old `required` line was left behind; copy-pasting an option block that already contained `required`; programmatically building Options from a config hash that sets required before default.","solutions":["Remove either `required` or `default_to` from the option block — usually drop `required`, since a default already covers the unset case","If the option truly must be supplied, keep `required` and delete `default_to`, letting the face fail when the flag is missing","When driving Option objects directly, only call `default=` when `required?` is false"],"exampleFix":"# before\noption \"--environment\" do\n  required\n  default_to { \"production\" }\nend\n\n# after\noption \"--environment\" do\n  default_to { \"production\" }\nend","handlingStrategy":"validation","validationCode":"# before assigning a default to an Option object\nraise ArgumentError, \"#{opt} is required; a default is not allowed\" if opt.required?\nopt.default = -> { 'production' }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never combine `required` with `default_to` in the same option block — pick one per option","When building Option objects manually, check `required?` before `default=` and `has_default?` before `required=`","Run `puppet help <face>` after editing a face to surface load-time definition errors early"],"tags":["puppet","faces","dsl","option","argumenterror"],"backgroundTag":"mutually-exclusive-options","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}