puppetlabs/puppet · error · ArgumentError
%{option} default_to requires a block
Error message
%{option} default_to requires a block What it means
OptionBuilder#default_to registers the block that lazily computes an option's default; calling it without a block raises ArgumentError (lib/puppet/interface/option_builder.rb:98). The block is stored via Option#default= and evaluated when the action is invoked, so a literal block is mandatory.
Source
Thrown at lib/puppet/interface/option_builder.rb:98
end
# Sets whether the option is required. If no argument is given it
# defaults to setting it as a required option.
# @api public
# @dsl Faces
def required(value = true)
@option.required = value
end
# Sets a block that will be used to compute the default value for this
# option. It will be evaluated when the action is invoked. The block
# should take no arguments.
# @api public
# @dsl Faces
def default_to(&block)
unless block
# TRANSLATORS 'default_to' is a method name and should not be translated
raise ArgumentError, _("%{option} default_to requires a block") % { option: @option }
end
if @option.has_default?
raise ArgumentError, _("%{option} already has a default value") % { option: @option }
end
unless block.arity == 0
# TRANSLATORS 'default_to' is a method name and should not be translated
raise ArgumentError, _("%{option} default_to block should not take any arguments") % { option: @option }
end
@option.default = block
end
end
View on GitHub (pinned to e227c27540)
Solutions
- Pass a literal block: `default_to { 'production' }`
- For computed values keep the block argument-free: `default_to { Puppet.settings[:environment] }`
- Guard optional defaults: `default_to { config[:default] } if config.key?(:default)`
Example fix
# before
option "--environment" do
default_to
end
# after
option "--environment" do
default_to { "production" }
end Defensive patterns
Strategy: validation
Validate before calling
# when the default comes from a variable raise ArgumentError, 'default_to needs a block' if default_proc.nil? default_to(&default_proc) # inside the option block
Prevention
- Always write `default_to { ... }` with a literal, argument-free block
- Verify config-driven defaults exist before passing them with `&`
- Compute values inside the block via closures, not block parameters
When it happens
Trigger: `option '--foo' do default_to end` (no block); `default_to(&config[:default])` where the config value is nil.
Common situations: Placeholder DSL left from scaffolding; defaults sourced from a config hash whose key is absent.
Related errors
- %{option} before_action requires a block
- %{option} after_action requires a block
- %{name} can't be optional and have a default value
- default value for %{name} is a %{class_name}, not a proc
- before action hook for %{name} is a %{class_name}, not a pro
AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21).
Data as JSON: /api/errors/0a40739744e91cc8.
Report an issue: GitHub.