optsparser_generator 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/optsparser_generator.rb +86 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56bfa952a4074ab23314b8199f7c72eb2ea046e0
4
+ data.tar.gz: dd5d69342938fa3c306661300acabab7c303f132
5
+ SHA512:
6
+ metadata.gz: f82fc7b445605277439b553f939129097422563e8c6c232be0abb0d2ef8374658e93b25ce76487120b87053a128f9f450afdbc4beafe1c73c14a8febd2b34e01
7
+ data.tar.gz: d017102908c5ddc89ac0627040fa1dbedbd47db2417f5ddfa807a611f2177d945cf8d401d0228e5477b94d434f4a7825d974784f108a573c87a34ab4fa0bdf3d
@@ -0,0 +1,86 @@
1
+ require 'ostruct'
2
+ require 'optparse'
3
+ require 'pp'
4
+
5
+ class OptParseGenerator
6
+ def initialize(ostruct)
7
+ @defaults = ostruct.dup.freeze
8
+
9
+ @optparser = OptionParser.new do |opts|
10
+ @defaults.each_pair do |key, val|
11
+ trigger = key.to_s.tr('_', '-')
12
+ next if trigger.end_with?('--help', '--values', '--short')
13
+ trigger[0..2] = '' if trigger.start_with?('no-')
14
+
15
+ help = @defaults[key.to_s << '__help'] || ''
16
+ help << " (Default: #{val})"
17
+ values = @defaults[key.to_s << '__values'] || []
18
+ short = @defaults[key.to_s << '__short'] || ''
19
+ arguments = []
20
+ arguments << help
21
+ arguments << short unless short.empty?
22
+ case val
23
+ when FalseClass, TrueClass
24
+ opts.on("--[no-]#{trigger}", *arguments) do |b|
25
+ @out[key] =
26
+ if key.to_s.start_with?('no_')
27
+ !b
28
+ else
29
+ b
30
+ end
31
+ end
32
+ else
33
+ arguments << val.class
34
+ arguments << values if values.any?
35
+ opts.on("--#{trigger}=ARGUMENT", *arguments) do |str|
36
+ @out[key] = str
37
+ end
38
+ end
39
+ end
40
+ opts.on('-h', '--help') do
41
+ puts opts
42
+ exit
43
+ end
44
+ end
45
+ end
46
+
47
+ def parse!(params)
48
+ @out = @defaults.dup
49
+ @optparser.parse!(params)
50
+ @out
51
+ end
52
+
53
+ def method_missing(method_sym, *arguments, &block)
54
+ # TODO: try to copy the methods into this class
55
+ # instead of method_missing hack them
56
+ if @optparser.respond_to?(method_sym)
57
+ @optparser.method(method_sym).call(*arguments, &block)
58
+ end
59
+ end
60
+
61
+ def self.respond_to?(method_sym, include_private = false)
62
+ if respond_to?(method_sym, include_private)
63
+ respond_to?(method_sym, include_private)
64
+ else
65
+ @optparser.respond_to?(method_sym, include_private)
66
+ end
67
+ end
68
+ end
69
+
70
+ # tests
71
+ # options = OpenStruct.new
72
+ # options.no_filter = false
73
+ # options.no_filter__help = 'bla bla helP!'
74
+ # options.no_filter__short = 'f'
75
+ # options.subsitute_new = true
76
+ # options.log = 'pa/th'
77
+ # options.some_option = 'pa/th'
78
+ # options.some_option__help = 'help text'
79
+ # options.some_option__values = %w(possible values for some option)
80
+ # options.fix_method_names_with_known_ones = true
81
+
82
+ # optparser = OptParseGenerator.new(options)
83
+ # puts optparser.parse!(['--no-filter']).inspect
84
+ # puts optparser.parse!(['--log=other/path']).inspect
85
+ # optparser.parse!(['--help'])
86
+ # puts 'Error: --help did not quit'
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optsparser_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Marvin Zerulla
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/optsparser_generator.rb
20
+ homepage:
21
+ licenses:
22
+ - AGPL-1.0
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.3.0
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.5.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Generates OptionParser using an OpenStruct
44
+ test_files: []
45
+ has_rdoc: