diff options
-rw-r--r-- | .travis.yml | 3 | ||||
-rw-r--r-- | CHANGELOG.md | 6 | ||||
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | lib/slop.rb | 2 | ||||
-rw-r--r-- | lib/slop/types.rb | 7 | ||||
-rw-r--r-- | test/types_test.rb | 28 |
6 files changed, 49 insertions, 4 deletions
diff --git a/.travis.yml b/.travis.yml index 4573dc8..0781d55 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,9 +17,10 @@ rvm: - 2.5.7 - 2.6.5 - 2.7.0 - - jruby-9.2.9.0 + - jruby-9.2.16.0 - jruby-head - ruby-head + - truffleruby-head jdk: - openjdk8 notifications: diff --git a/CHANGELOG.md b/CHANGELOG.md index ab46f71..c84fc58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Changelog ========= +v4.9.0 (2021-05-11) +------------------- + +Features: + * Add SymbolOption [#263](https://github.com/leejarvis/slop/pull/263) + v4.8.2 (2020-07-10) ------------------- @@ -18,6 +18,7 @@ opts = Slop.parse do |o| o.string '-h', '--host', 'a hostname' o.integer '--port', 'custom port', default: 80 o.string '-l', '--login', required: true + o.symbol '-m', '--method', default: :get o.bool '-v', '--verbose', 'enable verbose mode' o.bool '-q', '--quiet', 'suppress output (quiet mode)' o.bool '-c', '--check-ssl-certificate', 'check SSL certificate for host' @@ -27,15 +28,16 @@ opts = Slop.parse do |o| end end -ARGV #=> -v --login alice --host 192.168.0.1 --check-ssl-certificate +ARGV #=> -v --login alice --host 192.168.0.1 -m post --check-ssl-certificate opts[:host] #=> 192.168.0.1 opts[:login] #=> alice +opts[:method] #=> :post opts.verbose? #=> true opts.quiet? #=> false opts.check_ssl_certificate? #=> true -opts.to_hash #=> { host: "192.168.0.1", login: "alice", port: 80, verbose: true, quiet: false, check_ssl_certificate: true } +opts.to_hash #=> { host: "192.168.0.1", port: 80, login: "alice", method: :post, verbose: true, quiet: false, check_ssl_certificate: true } ``` Note that the block we've added to the `--version` flag will be executed @@ -56,6 +58,7 @@ o.integer #=> Slop::IntegerOption, expects an argument, aliased to IntOption o.float #=> Slop::FloatOption, expects an argument o.array #=> Slop::ArrayOption, expects an argument o.regexp #=> Slop::RegexpOption, expects an argument +o.symbol #=> Slop::SymbolOption, expects an argument o.null #=> Slop::NullOption, no argument and ignored from `to_hash` o.on #=> alias for o.null ``` diff --git a/lib/slop.rb b/lib/slop.rb index 3d7a95c..7c466a3 100644 --- a/lib/slop.rb +++ b/lib/slop.rb @@ -6,7 +6,7 @@ require 'slop/types' require 'slop/error' module Slop - VERSION = '4.8.2' + VERSION = '4.9.0' # Parse an array of options (defaults to ARGV). Accepts an # optional hash of configuration options and block. diff --git a/lib/slop/types.rb b/lib/slop/types.rb index c53319c..4ccd42e 100644 --- a/lib/slop/types.rb +++ b/lib/slop/types.rb @@ -6,6 +6,13 @@ module Slop end end + # Cast the option argument to a symbol. + class SymbolOption < Option + def call(value) + value.to_sym + end + end + # Cast the option argument to true or false. # Override default_value to default to false instead of nil. # This option type does not expect an argument. However, the API diff --git a/test/types_test.rb b/test/types_test.rb index d71c737..a45302a 100644 --- a/test/types_test.rb +++ b/test/types_test.rb @@ -1,5 +1,33 @@ require 'test_helper' +describe Slop::StringOption do + before do + @options = Slop::Options.new + @age = @options.string "--name" + @minus = @options.string "--zipcode" + @result = @options.parse %w(--name Foo --zipcode 12345) + end + + it "returns the value as a string" do + assert_equal "Foo", @result[:name] + assert_equal "12345", @result[:zipcode] + end +end + +describe Slop::SymbolOption do + before do + @options = Slop::Options.new + @age = @options.symbol "--name" + @minus = @options.symbol "--zipcode" + @result = @options.parse %w(--name Foo --zipcode 12345) + end + + it "returns the value as a symbol" do + assert_equal :Foo, @result[:name] + assert_equal :'12345', @result[:zipcode] + end +end + describe Slop::BoolOption do before do @options = Slop::Options.new |