dry_option_parser 1.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -13
- data/examples/dry_option_parser_example.rb +74 -74
- data/lib/dry_option_parser.rb +4 -8
- data/lib/dry_option_parser/version.rb +1 -1
- data/spec/refactored_optionparser_spec.rb +99 -98
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 947212d35a4b437baf028d923388104dc03945d7
|
4
|
+
data.tar.gz: a0e1aa4210067af71b4b4067e54e2be348dcf6d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a911e925477bd53d7e5169ec30129ccc93c36a6dd73f94c90cc97e0c6cc309563090b9d408a0bf7e672cfde543eff736a676a84a22cd092364fc3c604821e440
|
7
|
+
data.tar.gz: 9a4b79384696608a57f0583bd59a94f3b08bdb7c5ed013ebc0bd6178a4cd9278aab34b7b2dae231dc37c8562ec6c3a1bbf7885a3bf405f5dba8643dedd76ded0
|
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
DRY OPTION PARSER
|
2
2
|
=================
|
3
3
|
|
4
|
-
*
|
4
|
+
*A Super-simple super-thin wrapper around OptionParser that makes it less verbose**
|
5
|
+
|
5
6
|
|
6
7
|
Instead of
|
7
8
|
|
@@ -27,7 +28,7 @@ do
|
|
27
28
|
require 'dry_option_parser'
|
28
29
|
include DryOptionParser
|
29
30
|
|
30
|
-
opt_parser = DryOptionParsers.new
|
31
|
+
opt_parser = DryOptionParsers.new do
|
31
32
|
self.banner = "Usage: example.rb [options]"
|
32
33
|
assign(:verbose,"-v", "--[no-]verbose", "Run verbosely")
|
33
34
|
end.parse!
|
@@ -37,7 +38,7 @@ p ARGV
|
|
37
38
|
```
|
38
39
|
|
39
40
|
Basically, a `DryOptionParser` manages an `OptionParser` instance to which it delegates all instance method calls. Additionally it manages its own `options` object accessible via `:options` accessor methods.
|
40
|
-
(A total of 2 instance variables). Block arguments to new are `instance_eval`ed within the new `DryOptionParser` instance. `DryOptionParsers` also provides the `assign` private method, which simplifies option specs that simply do:
|
41
|
+
(A total of 2 instance variables: @options and `SimpleDelegator`'s instance variable). Block arguments to new are `instance_eval`ed within the new `DryOptionParser` instance. `DryOptionParsers` also provides the `assign` private method, which simplifies option specs that simply do:
|
41
42
|
|
42
43
|
```ruby
|
43
44
|
opts.on(...) do |value|
|
@@ -53,15 +54,5 @@ assign(:attribute,...)
|
|
53
54
|
|
54
55
|
Otherwise, all `OptionParser` documentation applies.
|
55
56
|
|
56
|
-
###One More Thing:
|
57
|
-
By including `DryOptionParser` you also get the `cli_options` helper method which simplifies calling `DryOptionParser.new() {}`, allowing you to ultimately shorten the basic example to:
|
58
|
-
|
59
|
-
```ruby
|
60
|
-
opt_parser = cli_options do
|
61
|
-
self.banner = "Usage: example.rb [options]"
|
62
|
-
assign(:verbose,"-v", "--[no-]verbose", "Run verbosely")
|
63
|
-
end.parse!
|
64
|
-
```
|
65
57
|
###The long example
|
66
|
-
|
67
58
|
Check out the **examples** directory to see how much you can shorten the long **OptionParser** example from http://www.ruby-doc.org/stdlib-2.1.3/libdoc/optparse/rdoc/OptionParser.html by using **DryOptionParser**.
|
@@ -4,80 +4,80 @@
|
|
4
4
|
require 'dry_option_parser'
|
5
5
|
require 'ostruct'
|
6
6
|
require 'optparse/time'
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
options
|
14
|
-
options.
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
7
|
+
extend DryOptionParser
|
8
|
+
|
9
|
+
|
10
|
+
#DryOptionsParsers
|
11
|
+
opt_parser = DryOptionParser.new do
|
12
|
+
|
13
|
+
self.options = OpenStruct.new
|
14
|
+
options.library = []
|
15
|
+
options.inplace = false
|
16
|
+
options.encoding = "utf8"
|
17
|
+
options.transfer_type = :auto
|
18
|
+
options.verbose = false
|
19
|
+
codes = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
|
20
|
+
code_aliases = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
|
21
|
+
|
22
|
+
self.banner = "Usage: example.rb [options]"
|
23
|
+
|
24
|
+
separator ""
|
25
|
+
separator "Specific options:"
|
26
|
+
|
27
|
+
# Mandatory argument.
|
28
|
+
on("-r", "--require LIBRARY",
|
29
|
+
"Require the LIBRARY before executing your script") do |lib|
|
30
|
+
options.library << lib
|
31
|
+
end
|
32
|
+
|
33
|
+
# Optional argument; multi-line description.
|
34
|
+
on("-i", "--inplace [EXTENSION]", "Edit ARGV files in place", " (make backup if EXTENSION supplied)") do |ext|
|
35
|
+
options.inplace = true
|
36
|
+
options.extension = ext || ''
|
37
|
+
options.extension.sub!(/\A\.?(?=.)/, ".") # Ensure extension begins with dot.
|
38
|
+
end
|
39
|
+
|
40
|
+
# Cast 'delay' argument to a Float.
|
41
|
+
assign("delay", "--delay N", Float, "Delay N seconds before executing")
|
42
|
+
# Cast 'time' argument to a Time object.
|
43
|
+
assign("time", "-t", "--time [TIME]", Time, "Begin execution at given time")
|
44
|
+
|
45
|
+
# Cast to octal integer.
|
46
|
+
assign("record_separator","-F", "--irs [OCTAL]", OptionParser::OctalInteger,
|
47
|
+
"Specify record separator (default \\0)")
|
48
|
+
|
49
|
+
# List of arguments.
|
50
|
+
assign("list","--list x,y,z", Array, "Example 'list' of arguments")
|
51
|
+
|
52
|
+
# Keyword completion. We are specifying a specific set of arguments (codes
|
53
|
+
# and code_aliases - notice the latter is a Hash), and the user may provide
|
54
|
+
# the shortest unambiguous text.
|
55
|
+
code_list = (code_aliases.keys + codes).join(',')
|
56
|
+
assign("encoding","--code CODE", codes, code_aliases, "Select encoding",
|
57
|
+
" (#{code_list})")
|
58
|
+
|
59
|
+
# Optional argument with keyword completion.
|
60
|
+
on("--type [TYPE]", [:text, :binary, :auto],
|
61
|
+
"Select transfer type (text, binary, auto)","transfer_type")
|
62
|
+
|
63
|
+
# Boolean switch.
|
64
|
+
on("-v", "--[no-]verbose", "Run verbosely", "verbose")
|
65
|
+
|
66
|
+
separator ""
|
67
|
+
separator "Common options:"
|
68
|
+
|
69
|
+
# No argument, shows at tail. This will print an options summary.
|
70
|
+
# Try it and see!
|
71
|
+
on_tail("-h", "--help", "Show this message") do
|
72
|
+
puts self
|
73
|
+
exit
|
74
|
+
end
|
75
|
+
|
76
|
+
# Another typical switch to print the version.
|
77
|
+
on_tail("--version", "Show version") do
|
78
|
+
puts ::Version.join('.')
|
79
|
+
exit
|
80
|
+
end
|
81
81
|
end
|
82
82
|
|
83
83
|
opt_parser.parse!(ARGV)
|
data/lib/dry_option_parser.rb
CHANGED
@@ -9,22 +9,18 @@ require 'delegate'
|
|
9
9
|
module DryOptionParser
|
10
10
|
class DryOptionParser < SimpleDelegator
|
11
11
|
|
12
|
+
attr_accessor :options
|
13
|
+
|
12
14
|
def initialize(options={},&blk)
|
13
15
|
@options = options
|
14
16
|
super(OptionParser.new)
|
15
|
-
instance_eval
|
17
|
+
instance_eval(&blk)
|
16
18
|
end
|
17
19
|
|
18
20
|
def assign(*args)
|
19
21
|
attribute = args.shift
|
20
|
-
on(*args) {|val| @options[attribute]=val}
|
22
|
+
on(*args) {|val| @options[attribute.to_sym]=val}
|
21
23
|
end
|
22
24
|
|
23
|
-
attr_accessor :options
|
24
|
-
#
|
25
25
|
end # class OptparseExample
|
26
|
-
|
27
|
-
def cli_options(options={},&blk)
|
28
|
-
DryOptionParser.new(options,&blk)
|
29
|
-
end
|
30
26
|
end
|
@@ -4,113 +4,114 @@
|
|
4
4
|
require 'dry_option_parser'
|
5
5
|
require 'ostruct'
|
6
6
|
require 'optparse/time'
|
7
|
-
include DryOptionParser
|
8
|
-
|
9
|
-
describe "DryOptionParser--the long OptionParser example" do
|
10
|
-
before do
|
11
|
-
@options = OpenStruct.new
|
12
|
-
@options.library = []
|
13
|
-
@options.inplace = false
|
14
|
-
@options.encoding = "utf8"
|
15
|
-
@options.transfer_type = :auto
|
16
|
-
@options.verbose = false
|
17
|
-
end
|
18
|
-
|
19
|
-
# Refactor the long optionparser example using DryOptionParser
|
20
|
-
subject do
|
21
|
-
@opt_parser = cli_options(@options) do
|
22
|
-
codes = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
|
23
|
-
code_aliases = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
|
24
|
-
|
25
|
-
self.banner = "Usage: example.rb [options]"
|
26
|
-
|
27
|
-
separator ""
|
28
|
-
separator "Specific options:"
|
29
7
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
8
|
+
module DryOptionParser
|
9
|
+
describe "DryOptionParser--the long OptionParser example" do
|
10
|
+
before {
|
11
|
+
@options = OpenStruct.new
|
12
|
+
@options.library = []
|
13
|
+
@options.inplace = false
|
14
|
+
@options.encoding = "utf8"
|
15
|
+
@options.transfer_type = :auto
|
16
|
+
@options.verbose = false
|
17
|
+
}
|
18
|
+
|
19
|
+
# Refactor the long optionparser example using DryOptionParser
|
20
|
+
subject do
|
21
|
+
opts = @options
|
22
|
+
@opt_parser = DryOptionParser.new do
|
23
|
+
self.options = opts
|
24
|
+
codes = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
|
25
|
+
code_aliases = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
|
26
|
+
|
27
|
+
self.banner = "Usage: example.rb [options]"
|
28
|
+
|
29
|
+
separator ""
|
30
|
+
separator "Specific options:"
|
31
|
+
|
32
|
+
# Mandatory argument.
|
33
|
+
on( "-r", "--require LIBRARY",
|
34
|
+
"Require the LIBRARY before executing your script") do |lib|
|
35
|
+
options.library << lib
|
36
|
+
end
|
37
|
+
|
38
|
+
# Optional argument; multi-line description.
|
39
|
+
on("-i", "--inplace [EXTENSION]", "Edit ARGV files in place",
|
40
|
+
" (make backup if EXTENSION supplied)") do |ext|
|
41
|
+
options.inplace = true
|
42
|
+
options.extension = ext || ''
|
43
|
+
options.extension.sub!(/\A\.?(?=.)/, ".") # Ensure extension begins with dot.
|
44
|
+
end
|
45
|
+
|
46
|
+
# Cast 'delay' argument to a Float.
|
47
|
+
assign("delay", "--delay N", Float, "Delay N seconds before executing")
|
48
|
+
# Cast 'time' argument to a Time object.
|
49
|
+
assign("time", "-t", "--time [TIME]", Time, "Begin execution at given time")
|
50
|
+
|
51
|
+
# Cast to octal integer.
|
52
|
+
assign("record_separator","-F", "--irs [OCTAL]", OptionParser::OctalInteger,
|
53
|
+
"Specify record separator (default \\0)")
|
54
|
+
|
55
|
+
# List of arguments.
|
56
|
+
assign("list","--list x,y,z", Array, "Example 'list' of arguments")
|
57
|
+
|
58
|
+
# Keyword completion. We are specifying a specific set of arguments (codes
|
59
|
+
# and code_aliases - notice the latter is a Hash), and the user may provide
|
60
|
+
# the shortest unambiguous text.
|
61
|
+
code_list = (code_aliases.keys + codes).join(',')
|
62
|
+
assign("encoding","--code CODE", codes, code_aliases, "Select encoding",
|
63
|
+
" (#{code_list})")
|
64
|
+
|
65
|
+
# Optional argument with keyword completion.
|
66
|
+
on("--type [TYPE]", [:text, :binary, :auto],
|
67
|
+
"Select transfer type (text, binary, auto)","transfer_type")
|
68
|
+
|
69
|
+
# Boolean switch.
|
70
|
+
on("-v", "--[no-]verbose", "Run verbosely", "verbose")
|
71
|
+
|
72
|
+
separator ""
|
73
|
+
separator "Common options:"
|
74
|
+
|
75
|
+
# No argument, shows at tail. This will print an options summary.
|
76
|
+
# Try it and see!
|
77
|
+
on_tail("-h", "--help", "Show this message") do
|
78
|
+
puts self
|
79
|
+
exit
|
80
|
+
end
|
81
|
+
|
82
|
+
# Another typical switch to print the version.
|
83
|
+
on_tail("--version", "Show version") do
|
84
|
+
puts ::Version.join('.')
|
85
|
+
exit
|
86
|
+
end
|
34
87
|
end
|
88
|
+
end
|
89
|
+
#End of refactored long example
|
35
90
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
" (make backup if EXTENSION supplied)") do |ext|
|
40
|
-
options.inplace = true
|
41
|
-
options.extension = ext || ''
|
42
|
-
options.extension.sub!(/\A\.?(?=.)/, ".") # Ensure extension begins with dot.
|
43
|
-
end
|
44
|
-
|
45
|
-
# Cast 'delay' argument to a Float.
|
46
|
-
assign("delay", "--delay N", Float, "Delay N seconds before executing")
|
47
|
-
# Cast 'time' argument to a Time object.
|
48
|
-
assign("time", "-t", "--time [TIME]", Time, "Begin execution at given time")
|
49
|
-
|
50
|
-
# Cast to octal integer.
|
51
|
-
assign("record_separator","-F", "--irs [OCTAL]", OptionParser::OctalInteger,
|
52
|
-
"Specify record separator (default \\0)")
|
53
|
-
|
54
|
-
# List of arguments.
|
55
|
-
assign("list","--list x,y,z", Array, "Example 'list' of arguments")
|
56
|
-
|
57
|
-
# Keyword completion. We are specifying a specific set of arguments (codes
|
58
|
-
# and code_aliases - notice the latter is a Hash), and the user may provide
|
59
|
-
# the shortest unambiguous text.
|
60
|
-
code_list = (code_aliases.keys + codes).join(',')
|
61
|
-
assign("encoding","--code CODE", codes, code_aliases, "Select encoding",
|
62
|
-
" (#{code_list})")
|
63
|
-
|
64
|
-
# Optional argument with keyword completion.
|
65
|
-
on("--type [TYPE]", [:text, :binary, :auto],
|
66
|
-
"Select transfer type (text, binary, auto)","transfer_type")
|
67
|
-
|
68
|
-
# Boolean switch.
|
69
|
-
on("-v", "--[no-]verbose", "Run verbosely", "verbose")
|
70
|
-
|
71
|
-
separator ""
|
72
|
-
separator "Common options:"
|
73
|
-
|
74
|
-
# No argument, shows at tail. This will print an options summary.
|
75
|
-
# Try it and see!
|
76
|
-
on_tail("-h", "--help", "Show this message") do
|
77
|
-
puts self
|
78
|
-
exit
|
79
|
-
end
|
80
|
-
|
81
|
-
# Another typical switch to print the version.
|
82
|
-
on_tail("--version", "Show version") do
|
83
|
-
puts ::Version.join('.')
|
84
|
-
exit
|
85
|
-
end
|
91
|
+
#Some tests
|
92
|
+
it "should successfully evaluate the full example" do
|
93
|
+
should_not be_nil
|
86
94
|
end
|
87
|
-
end
|
88
|
-
#End of refactored long example
|
89
95
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
96
|
+
context "parsing with various args" do
|
97
|
+
it "should return default options if no args given" do
|
98
|
+
subject.parse!([])
|
99
|
+
expect(subject.options.each_pair.all? {|k,v| @options[k]==v}).to be_truthy
|
100
|
+
end
|
94
101
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
102
|
+
it "-r something should add library" do
|
103
|
+
subject.parse!(%w[-r foo])
|
104
|
+
library = subject.options[:library]
|
105
|
+
expect(library).to be_an(Array)
|
106
|
+
expect(subject.options[:library]).to include("foo")
|
107
|
+
end
|
108
|
+
it "--delay 7 should assign a delay of 7.0" do
|
109
|
+
subject.parse!(%w[--delay 7])
|
110
|
+
expect(subject.options[:delay]).to eq(7.0)
|
111
|
+
end
|
99
112
|
end
|
100
113
|
|
101
|
-
it "-r something should add library" do
|
102
|
-
subject.parse!(%w[-r foo])
|
103
|
-
library = subject.options[:library]
|
104
|
-
expect(library).to be_an(Array)
|
105
|
-
expect(subject.options[:library]).to include("foo")
|
106
|
-
end
|
107
|
-
it "--delay 7 should assign a delay of 7.0" do
|
108
|
-
subject.parse!(%w[--delay 7])
|
109
|
-
expect(subject.options[:delay]).to eq(7.0)
|
110
|
-
end
|
111
114
|
end
|
112
|
-
|
113
|
-
|
114
115
|
end
|
115
116
|
|
116
117
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry_option_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Petr Skocik
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.2.
|
96
|
+
rubygems_version: 2.2.2
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: Save keystrokes with OptionParser while using more or less the same API.
|