optsparser_generator 1.1 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/optsparser_generator.rb +79 -53
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcbe1e73825ba5ba097c4bd260424f2eaf0fe286
4
- data.tar.gz: b8595c9abc7ad0f13fa0c41c04f0845308a0262d
3
+ metadata.gz: df87ca8d12e02a00e201b6628060ae316a41220f
4
+ data.tar.gz: 53f174afb5cf8ce84600faf9a3407821d47a5a6a
5
5
  SHA512:
6
- metadata.gz: 08e11abeebc8b5ffd471488fee1ea592b52b3f11552f36b398d1f45b0e59127751d1dd3be74bc611baab2c755e39537616dc501dcecfd48aaad1c3646e003669
7
- data.tar.gz: 14871d9b25637519b5e629ee8268cf52dc58994e336c1e5104a5a9a0747cceb2064ae16553daafcb34473ac605b073f63816e7c0d7ec9be5498d234d9192f856
6
+ metadata.gz: 453d7de0e17a27958b62b81ff2c3fef1df0724c34c75659af8373646ef143ca4fa941b46d70058a1d8bfe16fc1ab230dc94fcc0959bef30144f92e361672567c
7
+ data.tar.gz: 70d896c1c08b9e7d5c2faca0ad57a18fe84fbe1d9f768ee44189da7748ad296ff95d61fcbc000bd0e61862dca257c80cfbed18f6a9a3f51f678e71a81cea1666
@@ -1,27 +1,60 @@
1
1
  require 'ostruct'
2
2
  require 'optparse'
3
3
 
4
- class OptParseGenerator
5
- def initialize(ostruct)
6
- @defaults = ostruct.dup.freeze
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
- @optparser = OptionParser.new do |opts|
9
- @defaults.each_pair do |key, val|
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 = @defaults[key.to_s << '__help'] || ''
15
- help << " (Default: #{val})"
16
- values = @defaults[key.to_s << '__values'] || []
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
- @out[key] =
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}=ARGUMENT", *arguments) do |str|
35
- @out[key] = str
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
- def parse!(params)
47
- @out = @defaults.dup
48
- @optparser.parse!(params)
49
- @out
80
+ # add default values
81
+ optparser.instance_variable_set(:@defaults, defaults)
82
+ optparser.extend(OptParsePatch)
83
+ optparser
50
84
  end
51
85
 
52
- def method_missing(method_sym, *arguments, &block)
53
- # TODO: try to copy the methods into this class
54
- # instead of method_missing hack them
55
- if @optparser.respond_to?(method_sym)
56
- @optparser.method(method_sym).call(*arguments, &block)
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
- def respond_to?(method_sym, include_private = false)
61
- @optparser.respond_to?(method_sym, include_private) || super
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
- # tests
66
- # require 'optsparser_generator'
67
- # options = OpenStruct.new
68
- # options.no_filter = false
69
- # options.no_filter__help = 'bla bla helP!'
70
- # options.no_filter__short = 'f'
71
- # options.subsitute_new = true
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optsparser_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: '2.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marvin Zerulla