options_by_example 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/options_by_example/version.rb +8 -1
- data/lib/options_by_example.rb +22 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f90dee5b19415bddb809efe80e4fc2c6f39daf7b82259a0c247caa789d21ff23
|
4
|
+
data.tar.gz: b74ece7699aee6f785e9a2fa5149f2a2ee6f2c7f5a4365a429e958d676e9aaf1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4e235ad68624c57e3efe1915a31c006128351f6c870fa5aefa8f6b2856ec0bab33c24107c1d99b308d9b5fa54cabdff901d3669413e4021c839d7778ecb9cb7
|
7
|
+
data.tar.gz: a5c7213462bc16e1b88411b92c28c9be5b71cf9838bcad217316586b28318398441e1cc69acf56e223812c7c0fb51dcf381403158a35283f58ea2b0ce6f777bc
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class OptionsByExample
|
4
|
-
VERSION = '3.
|
4
|
+
VERSION = '3.1.0'
|
5
5
|
end
|
6
6
|
|
7
7
|
|
@@ -11,12 +11,19 @@ __END__
|
|
11
11
|
# Minor version bump when backward-compatible changes or enhancements
|
12
12
|
# Patch version bump when backward-compatible bug fixes, security updates etc
|
13
13
|
|
14
|
+
3.1.0
|
15
|
+
|
16
|
+
- Support dash in argument and option names
|
17
|
+
- Method #if_present passes argument to block if present
|
18
|
+
- Method #include? return true if option is present
|
19
|
+
|
14
20
|
3.0.0
|
15
21
|
|
16
22
|
- Support options with default values
|
17
23
|
- Improved support for one-line usage messages
|
18
24
|
- Expand combined shorthand options into their separate components
|
19
25
|
- Shorthand options must be single letter only
|
26
|
+
- Support options with typed arguments
|
20
27
|
|
21
28
|
2.0.0
|
22
29
|
|
data/lib/options_by_example.rb
CHANGED
@@ -25,8 +25,8 @@ class OptionsByExample
|
|
25
25
|
|
26
26
|
text =~ /Usage: (\$0|\w+)(?: \[options\])?((?: \[\w+\])*)((?: \w+)*)/
|
27
27
|
raise RuntimeError, "Expected usage string, got none" unless $1
|
28
|
-
@argument_names_optional = $2.to_s.split.map { |match| match.tr('[]', '')
|
29
|
-
@argument_names_required = $3.to_s.split.map
|
28
|
+
@argument_names_optional = $2.to_s.split.map { |match| sanitize match.tr('[]', '') }
|
29
|
+
@argument_names_required = $3.to_s.split.map { |match| sanitize match }
|
30
30
|
|
31
31
|
# --- 2) Parse option names ---------------------------------------
|
32
32
|
#
|
@@ -41,10 +41,10 @@ class OptionsByExample
|
|
41
41
|
|
42
42
|
@option_names = {}
|
43
43
|
@default_values = {}
|
44
|
-
text.scan(/(?:(-\w), ?)?(--(\w+))(?: (\w+))?(?:.*\(default:? (\w+)\))?/) do
|
44
|
+
text.scan(/(?:(-\w), ?)?(--([\w-]+))(?: (\w+))?(?:.*\(default:? (\w+)\))?/) do
|
45
45
|
flags = [$1, $2].compact
|
46
|
-
flags.each { |each| @option_names[each] = [$3, $4] }
|
47
|
-
@default_values[$3] = $5 if $5
|
46
|
+
flags.each { |each| @option_names[each] = [(sanitize $3), $4] }
|
47
|
+
@default_values[sanitize $3] = $5 if $5
|
48
48
|
end
|
49
49
|
|
50
50
|
initialize_argument_accessors
|
@@ -76,6 +76,17 @@ class OptionsByExample
|
|
76
76
|
return self
|
77
77
|
end
|
78
78
|
|
79
|
+
def if_present(name)
|
80
|
+
raise ArgumentError, 'block missing' unless block_given?
|
81
|
+
|
82
|
+
value = @arguments[name]
|
83
|
+
value.nil? ? value : (yield value)
|
84
|
+
end
|
85
|
+
|
86
|
+
def include?(name)
|
87
|
+
@options.include?(name)
|
88
|
+
end
|
89
|
+
|
79
90
|
private
|
80
91
|
|
81
92
|
def initialize_argument_accessors
|
@@ -86,7 +97,7 @@ class OptionsByExample
|
|
86
97
|
].each do |argument_name|
|
87
98
|
instance_eval %{
|
88
99
|
def argument_#{argument_name}
|
89
|
-
val = @arguments[
|
100
|
+
val = @arguments[:#{argument_name}]
|
90
101
|
val && block_given? ? (yield val) : val
|
91
102
|
end
|
92
103
|
}
|
@@ -97,10 +108,14 @@ class OptionsByExample
|
|
97
108
|
@option_names.each_value do |option_name, _|
|
98
109
|
instance_eval %{
|
99
110
|
def include_#{option_name}?
|
100
|
-
@options.include?
|
111
|
+
@options.include? :#{option_name}
|
101
112
|
end
|
102
113
|
}
|
103
114
|
end
|
104
115
|
end
|
116
|
+
|
117
|
+
def sanitize(string)
|
118
|
+
string.tr('^a-zA-Z0-9', '_').downcase.to_sym
|
119
|
+
end
|
105
120
|
end
|
106
121
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: options_by_example
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrian Kuhn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|