davetron5000 / gli

Make awesome command-line applications the easy way
http://davetron5000.github.io/gli
Apache License 2.0
1.26k stars 102 forks source link

When using `accept`, the values from the config are not routed through the type conversions #326

Open davetron5000 opened 1 month ago

davetron5000 commented 1 month ago

Because GLI is using OptionParser (from the stdlib) to do type conversions, they only happen when arguments are parsed from the command line. When arguments are parsed from the config file, they will not be type-checked.

This is a bug, but also points to the sorta bad design of the configuration system.

The following test shows the bug (copied from #190):

# test/unit/support/gli_test_config.yml
# last line is the addition to show the behavior
--- 
commands: 
  :command: 
    :g: bar
    :s: true
  :other_command: 
    :g: foobar
    :f: barfoo
:f: foo
:bleorgh: true
:t: false
:arr: foo,bar
# test/unit/gli_test.rb
  def test_init_from_config_with_accept
    failure = nil
    @app.reset
    @app.config_file(File.expand_path(File.dirname(File.realpath(__FILE__)) + '/config.yaml'))
    @app.accept(Array) do |value|
      value.split(/,/).map(&:strip)
    end
    called = false
    @app.flag :arr, :accept => Array
    @app.flag :work, :accept => Array
    @app.command :command do |c|
      c.action do |g,o,a|
        begin
          called = true
          assert_equal ['foo','bar'], g[:arr]
          assert_equal ['bar','foo'], g[:work]
        rescue Exception => ex
          failure = ex
        end
      end
    end
    @app.run(['--work bar,foo command'])
    assert called
    raise failure if !failure.nil?
  end