puppetlabs/puppet · error · CommandlineError

option '%{arg}' needs a floating-point number

Error message

option '%{arg}' needs a floating-point number

What it means

Options typed :float or :floats validate each parameter against FLOAT_RE = /^-?((\d+(\.\d+)?)|(\.\d+))([eE][-+]?\d+)?$/ (trollop.rb:36) inside parse_float_parameter; a mismatch raises CommandlineError "option '--x' needs a floating-point number" at line 665. The regex accepts an optional leading minus, digits with an optional fractional part, or a bare fraction like '.5', plus an optional exponent — but not comma decimals, trailing dots without digits ('1.'), incomplete exponents ('1e'), or non-numeric strings.

Source

Thrown at lib/puppet/util/command_line/trollop.rb:665

            return remains
          else
            remains << args[i]
            i += 1
          end
        end
      end

      remains
    end

    def parse_integer_parameter param, arg
      raise CommandlineError, _("option '%{arg}' needs an integer") % { arg: arg } unless param =~ /^\d+$/

      param.to_i
    end

    def parse_float_parameter param, arg
      raise CommandlineError, _("option '%{arg}' needs a floating-point number") % { arg: arg } unless param =~ FLOAT_RE

      param.to_f
    end

    def parse_io_parameter param, arg
      case param
      when /^(stdin|-)$/i; $stdin
      else
        require 'open-uri'
        begin
          URI.parse(param).open
        rescue SystemCallError => e
          raise CommandlineError, _("file or url for option '%{arg}' cannot be opened: %{value0}") % { arg: arg, value0: e.message }, e.backtrace
        end
      end
    end

    def collect_argument_parameters args, start_at

View on GitHub (pinned to e227c27540)

Solutions

  1. Pass a plain decimal such as `--ratio 0.75` or `--ratio -0.75`
  2. Strip whitespace and normalize commas to dots before invoking the command
  3. When generating command lines, validate values against a float regex in the caller first

Example fix

# before
$ mytool --ratio 1,5
Error: option '--ratio' needs a floating-point number

# after
$ mytool --ratio 1.5
Defensive patterns

Strategy: validation

Validate before calling

# Mirror FLOAT_RE = /^-?((\d+(\.\d+)?)|(\.\d+))([eE][-+]?\d+)?$/ before invoking
FLOAT_RE = /^-?((\d+(\.\d+)?)|(\.\d+))([eE][-+]?\d+)?$/
value = argv[i + 1]
abort "#{value.inspect} is not a valid float (watch commas and trailing dots)" unless value =~ FLOAT_RE

Type guard

def trollop_float?(s)
  s.is_a?(String) && s =~ /^-?((\d+(\.\d+)?)|(\.\d+))([eE][-+]?\d+)?$/
end

Try / catch

begin
  opts = parser.parse(argv)
rescue Puppet::Util::CommandLine::Trollop::CommandlineError => e
  raise unless e.message.include?('needs a floating-point number')
  name = e.message[/option '(.+)' needs/, 1]
  abort "#{name} needs a decimal like 0.75, -0.75, .5, or 1e3 (no commas)"
end

Prevention

When it happens

Trigger: `--ratio abc`; `--ratio 1,5` (comma decimal separator); `--ratio 1.` (trailing dot, no digits); `--ratio 1e` (incomplete exponent); values with embedded whitespace.

Common situations: Locales that use a comma as the decimal separator; whitespace left in generated values; scientific notation with a malformed exponent.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/80280cbb015a6c88. Report an issue: GitHub.