summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Gracyk <jgracyk@squareup.com>2021-05-10 14:06:35 -0700
committerJoe Gracyk <jgracyk@squareup.com>2021-05-10 14:06:35 -0700
commit6e6ed91d0d5c3e2197f0207894a01226446759de (patch)
tree87095784fdb633dc656ffe78f55f344775ed2189
parent3cbd0e9dfaa6804d6f46fbbec08155a680193cd7 (diff)
downloadslop-6e6ed91d0d5c3e2197f0207894a01226446759de.tar.gz
Add symbols to option types
-rw-r--r--README.md7
-rw-r--r--lib/slop/types.rb7
-rw-r--r--test/types_test.rb14
3 files changed, 26 insertions, 2 deletions
diff --git a/README.md b/README.md
index 97dd557..7e8223a 100644
--- a/README.md
+++ b/README.md
@@ -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/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..ed13d5c 100644
--- a/test/types_test.rb
+++ b/test/types_test.rb
@@ -1,5 +1,19 @@
require 'test_helper'
+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