optsparser_generator 4.4 → 4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -5
- data/lib/optsparser_generator.rb +16 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7002bd5bc1ed12354e14e4db3d4d7e0ded00d2b6984fa94f72095c5ede3676b9
|
4
|
+
data.tar.gz: 5cd3e2448f62f62c5f57db2f37ee1f6cdbc719281bd62262a81766592cd572ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a57ebea15a0b6c8f6af4d5aa3b64c24e67135c01d81f4a926c0b3084723f833792431b1c293132c7dfbae96e5e35fdad9150409f53e9028f282b09da09b4680
|
7
|
+
data.tar.gz: 72f91bfe36beec53a0cdc85c4514bc5893966d17fe5263361be287aad2038f148ca0b7cc91b35c23ce00fdcfcf8e009bd1bf5bbeed28fcdc15d0358458c18fd8
|
data/README.md
CHANGED
@@ -17,6 +17,7 @@ os.default = 'value'
|
|
17
17
|
os.val = 123
|
18
18
|
os.val__values = [1, 1.5, 2, 123]
|
19
19
|
os.val__class = Numeric
|
20
|
+
os.val__required = true
|
20
21
|
os.bool = true
|
21
22
|
os.bool__help = 'description of argument'
|
22
23
|
os.bool__short = 'b'
|
@@ -36,11 +37,12 @@ OptParseGen.parse(os) # takes ARGV or an array
|
|
36
37
|
```
|
37
38
|
|
38
39
|
## Special values
|
39
|
-
* __help
|
40
|
-
* __values
|
41
|
-
* __short
|
42
|
-
* __class
|
43
|
-
* __proc
|
40
|
+
* __help defines the description for a property
|
41
|
+
* __values defines possible values in an Array
|
42
|
+
* __short defines the short trigger
|
43
|
+
* __class defines the Class which OptionParser then tries to coerce to
|
44
|
+
* __proc a Proc which will be executed to compute the value
|
45
|
+
* __required parameter is required, raises error if missing
|
44
46
|
|
45
47
|
# Version numbers
|
46
48
|
I choose two digit version numbers.
|
data/lib/optsparser_generator.rb
CHANGED
@@ -5,11 +5,12 @@ require 'optparse'
|
|
5
5
|
module OptionParserGenerator
|
6
6
|
# Special postfixes for Hash keys
|
7
7
|
SPECIAL_POSTFIXES = [
|
8
|
+
'--class',
|
8
9
|
'--help',
|
9
|
-
'--
|
10
|
+
'--proc',
|
11
|
+
'--required',
|
10
12
|
'--short',
|
11
|
-
'--
|
12
|
-
'--proc'
|
13
|
+
'--values',
|
13
14
|
].freeze
|
14
15
|
|
15
16
|
# Raised when not given an OpenStruct
|
@@ -26,6 +27,8 @@ module OptionParserGenerator
|
|
26
27
|
# @api private
|
27
28
|
module OpenStructExtension
|
28
29
|
# extracts a special value from the openstruct
|
30
|
+
# @param key [String]
|
31
|
+
# @param string [String]
|
29
32
|
def special_value(key, string)
|
30
33
|
self["#{key}__#{string}"]
|
31
34
|
end
|
@@ -36,6 +39,7 @@ module OptionParserGenerator
|
|
36
39
|
# @todo preprocess data here instead of doing it adhoc
|
37
40
|
# @todo raise more exceptions
|
38
41
|
# @api private
|
42
|
+
# @param ostruct [OpenStruct]
|
39
43
|
def self.handle_arguments(ostruct)
|
40
44
|
unless ostruct.is_a?(OpenStruct)
|
41
45
|
raise WrongArgumentType, 'needs an OpenStruct'
|
@@ -51,14 +55,13 @@ module OptionParserGenerator
|
|
51
55
|
# @todo write documentation :(
|
52
56
|
# @todo split this up
|
53
57
|
# @param ostruct [OpenStruct] Default values with special values
|
54
|
-
# @param
|
55
|
-
# @
|
56
|
-
# :ignore_collisions ignore bool key collisions see OptionCollision
|
57
|
-
# :generate_no_help when set to true donesn't generates help command
|
58
|
+
# @param :ignore_collisions [Boolean] ignore bool key collisions see OptionCollision
|
59
|
+
# @param :generate_no_help [Boolean] when set to true donesn't generates help command
|
58
60
|
def self.[](ostruct, **options)
|
59
61
|
defaults = handle_arguments(ostruct)
|
60
62
|
|
61
63
|
optparser = OptionParser.new do |opts|
|
64
|
+
opts.instance_variable_set(:@required, {})
|
62
65
|
defaults.each_pair do |key, val|
|
63
66
|
trigger = key.to_s.tr('_', '-')
|
64
67
|
next if trigger.end_with?(*SPECIAL_POSTFIXES)
|
@@ -93,8 +96,12 @@ module OptionParserGenerator
|
|
93
96
|
block = proc
|
94
97
|
end
|
95
98
|
opts.on(*arguments) do |arg|
|
99
|
+
opts.instance_variable_get(:@required)[key] = true
|
96
100
|
opts.instance_variable_get(:@out)[key] = block.call(arg)
|
97
101
|
end
|
102
|
+
if defaults.special_value(key, 'required')
|
103
|
+
opts.instance_variable_get(:@required)[key] = false
|
104
|
+
end
|
98
105
|
end
|
99
106
|
|
100
107
|
unless options[:generate_no_help]
|
@@ -144,6 +151,7 @@ module OptionParserGenerator
|
|
144
151
|
def parse!(*params, **opts)
|
145
152
|
@out = @defaults.dup
|
146
153
|
super
|
154
|
+
@required.reject{|k,v|v}.each{|k,v|raise OptionParser::MissingArgument, k}
|
147
155
|
@out
|
148
156
|
end
|
149
157
|
|
@@ -151,6 +159,7 @@ module OptionParserGenerator
|
|
151
159
|
def parse(*params, **opts)
|
152
160
|
@out = @defaults.dup
|
153
161
|
super
|
162
|
+
@required.reject{|k,v|v}.each{|k,v|raise OptionParser::MissingArgument, k}
|
154
163
|
@out
|
155
164
|
end
|
156
165
|
end
|