optsparser_generator 1.1 → 2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/optsparser_generator.rb +79 -53
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df87ca8d12e02a00e201b6628060ae316a41220f
|
4
|
+
data.tar.gz: 53f174afb5cf8ce84600faf9a3407821d47a5a6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 453d7de0e17a27958b62b81ff2c3fef1df0724c34c75659af8373646ef143ca4fa941b46d70058a1d8bfe16fc1ab230dc94fcc0959bef30144f92e361672567c
|
7
|
+
data.tar.gz: 70d896c1c08b9e7d5c2faca0ad57a18fe84fbe1d9f768ee44189da7748ad296ff95d61fcbc000bd0e61862dca257c80cfbed18f6a9a3f51f678e71a81cea1666
|
data/lib/optsparser_generator.rb
CHANGED
@@ -1,27 +1,60 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
require 'optparse'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
# Small lib for generating an OptionParser from an OpenStruct
|
5
|
+
module OptionParserGenerator
|
6
|
+
# raised when not given an OpenStruct
|
7
|
+
class WrongArgumentType < ArgumentError
|
8
|
+
end
|
9
|
+
# raised when there are two boolean entries in the OpenStruct
|
10
|
+
# one named no_xyz and one xyz
|
11
|
+
# only raised when the option :ignore_collisions is not given
|
12
|
+
class OptionCollision < ArgumentError
|
13
|
+
end
|
14
|
+
|
15
|
+
# @todo preprocess data here instead of doing it adhoc
|
16
|
+
# @todo raise more exceptions
|
17
|
+
# @api private
|
18
|
+
def self.handle_arguments(ostruct)
|
19
|
+
unless ostruct.is_a?(OpenStruct)
|
20
|
+
raise WrongArgumentType, 'needs an OpenStruct'
|
21
|
+
end
|
22
|
+
ostruct.dup.freeze
|
23
|
+
end
|
24
|
+
|
25
|
+
# does the magic
|
26
|
+
#
|
27
|
+
# @todo write documentation :(
|
28
|
+
# @todo split this up
|
29
|
+
# @param ostruct [OpenStruct] Default values with special values
|
30
|
+
# @param options [Hash]
|
31
|
+
# @option options [Boolean] :ignore_collisions ignore bool key collisions @see OptionCollision
|
32
|
+
def self.[](ostruct, **options)
|
33
|
+
defaults = handle_arguments(ostruct)
|
7
34
|
|
8
|
-
|
9
|
-
|
35
|
+
optparser = OptionParser.new do |opts|
|
36
|
+
defaults.each_pair do |key, val|
|
10
37
|
trigger = key.to_s.tr('_', '-')
|
11
|
-
next if trigger.end_with?('--help', '--values', '--short')
|
12
|
-
trigger[0..2] = '' if trigger.start_with?('no-')
|
38
|
+
next if trigger.end_with?('--help', '--values', '--short', '--class')
|
13
39
|
|
14
|
-
help =
|
15
|
-
|
16
|
-
|
17
|
-
short = @defaults[key.to_s << '__short'] || ''
|
40
|
+
help = "#{defaults[key.to_s << '__help']} (Default: #{val})"
|
41
|
+
values = defaults[key.to_s << '__values'] || []
|
42
|
+
short = defaults[key.to_s << '__short'] || ''
|
18
43
|
arguments = []
|
19
|
-
arguments << help
|
20
|
-
arguments << short unless short.empty?
|
21
|
-
case val
|
44
|
+
arguments << help unless help.empty?
|
45
|
+
arguments << "-#{short}" unless short.empty?
|
46
|
+
case defaults[key.to_s << '__class'] || val
|
22
47
|
when FalseClass, TrueClass
|
48
|
+
if trigger.start_with?('no-')
|
49
|
+
trigger[0..2] = ''
|
50
|
+
if defaults.each_pair.map { |v| v.first.to_s }.include?(trigger)
|
51
|
+
raise OptionCollision, "on #{trigger}" unless options[:ignore_collisions]
|
52
|
+
next
|
53
|
+
end
|
54
|
+
end
|
23
55
|
opts.on("--[no-]#{trigger}", *arguments) do |b|
|
24
|
-
|
56
|
+
out = opts.instance_variable_get(:@out)
|
57
|
+
out[key] =
|
25
58
|
if key.to_s.start_with?('no_')
|
26
59
|
!b
|
27
60
|
else
|
@@ -29,57 +62,50 @@ class OptParseGenerator
|
|
29
62
|
end
|
30
63
|
end
|
31
64
|
else
|
32
|
-
arguments << val.class
|
65
|
+
arguments << defaults[key.to_s << '__class'] || val.class
|
33
66
|
arguments << values if values.any?
|
34
|
-
opts.on("--#{trigger}=
|
35
|
-
|
67
|
+
opts.on("--#{trigger}=ARG", *arguments) do |str|
|
68
|
+
out = opts.instance_variable_get(:@out)
|
69
|
+
out[key] = str
|
36
70
|
end
|
37
71
|
end
|
38
72
|
end
|
73
|
+
|
39
74
|
opts.on('-h', '--help') do
|
40
75
|
puts opts
|
41
76
|
exit
|
42
77
|
end
|
43
78
|
end
|
44
|
-
end
|
45
79
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
80
|
+
# add default values
|
81
|
+
optparser.instance_variable_set(:@defaults, defaults)
|
82
|
+
optparser.extend(OptParsePatch)
|
83
|
+
optparser
|
50
84
|
end
|
51
85
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
86
|
+
## patch for OptionParser redefines parse and parse!
|
87
|
+
# @api private
|
88
|
+
module OptParsePatch
|
89
|
+
# @return [OpenStruct]
|
90
|
+
def parse!(*params)
|
91
|
+
@out = @defaults.dup
|
92
|
+
super
|
93
|
+
@out
|
57
94
|
end
|
58
|
-
end
|
59
95
|
|
60
|
-
|
61
|
-
|
96
|
+
# @return [OpenStruct]
|
97
|
+
def parse(*params)
|
98
|
+
@out = @defaults.dup
|
99
|
+
super
|
100
|
+
@out
|
101
|
+
end
|
62
102
|
end
|
63
103
|
end
|
64
|
-
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
# options.log = 'pa/th'
|
73
|
-
# options.some_option = 'pa/th'
|
74
|
-
# options.some_option__help = 'help text'
|
75
|
-
# options.some_option__values = %w(possible values for some option)
|
76
|
-
# options.fix_method_names_with_known_ones = true
|
77
|
-
|
78
|
-
# optparser = OptParseGenerator.new(options)
|
79
|
-
# raise 'broken' unless optparser.respond_to? :to_s
|
80
|
-
# raise 'broken' if optparser.respond_to? :notthere
|
81
|
-
|
82
|
-
# puts optparser.parse!(['--no-filter']).inspect
|
83
|
-
# puts optparser.parse!(['--log=other/path']).inspect
|
84
|
-
# optparser.parse!(['--help'])
|
85
|
-
# raise 'Error: --help did not quit'
|
104
|
+
# global shorthand
|
105
|
+
# alias to OptionParserGenerator[arg, opt]
|
106
|
+
# @return [OptionParser]
|
107
|
+
# @see OptionParserGenerator
|
108
|
+
def OptionParserGenerator(arg, **opt)
|
109
|
+
OptionParserGenerator[arg, opt]
|
110
|
+
end
|
111
|
+
alias OptParseGen OptionParserGenerator
|