markdown_exec 1.6 → 1.8

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.
@@ -1,209 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # encoding=utf-8
4
-
5
- require 'optparse'
6
- require 'yaml'
7
-
8
- require_relative 'object_present'
9
-
10
- # add Hash.sym_keys
11
- #
12
- class Hash
13
- unless defined?(sym_keys)
14
- def sym_keys
15
- transform_keys(&:to_sym)
16
- # map do |key, value|
17
- # [key.to_sym, value]
18
- # end.to_h
19
- end
20
- end
21
- end
22
-
23
- # parse application configuration from command-line options and environment variables
24
- #
25
- class EnvironmentOptParse
26
- attr_reader :options, :remainder
27
-
28
- # utility functions to create menu
29
- #
30
- module Menu
31
- def menu_all(menu_data, lambdas, config)
32
- config.tap_yaml 'config'
33
- input_option_values, remainder, = menu_parse(add_proc(menu_data, lambdas))
34
- options = menu_default_option_values(menu_data)
35
- .merge(config)
36
- .merge(input_option_values)
37
-
38
- [options, remainder]
39
- end
40
-
41
- def add_proc(menu_data, lambdas)
42
- menu_data.each do |menu_item|
43
- menu_item.tap_yaml 'menu_item'
44
- procname = menu_item[:procname]
45
- next if procname.nil?
46
-
47
- menu_item[:proccode] =
48
- lambdas.fetch(procname.to_sym, menu_item[:procname])
49
- end.tap_yaml
50
- end
51
-
52
- def menu_default_option_values(menu_data)
53
- menu_data.map do |item|
54
- item_default = item[:default]
55
- next if item_default.nil?
56
- next unless item[:opt_name].present?
57
-
58
- [item[:opt_name].to_sym, item_default]
59
- end.compact.to_h
60
- end
61
-
62
- def menu_help(menu_data)
63
- options = {}
64
- option_parser = OptionParser.new do |opts|
65
- opts.banner = [
66
- "#{APP_NAME} - #{APP_DESC} (#{VERSION})",
67
- "Usage: #{File.basename($PROGRAM_NAME)} [options]"
68
- ].join("\n")
69
-
70
- menu_data.each do |item|
71
- menu_option_append opts, options, item
72
- end
73
- end
74
-
75
- option_parser.help
76
- end
77
-
78
- def menu_option_append(opts, options, item)
79
- return unless item[:long_name].present? || item[:short_name].present?
80
-
81
- mmoo = [
82
- # long name
83
- if item[:long_name].present?
84
- # if "--#{item[:long_name]}#{item[:arg_name]".present?
85
- # " #{item[:arg_name]}"
86
- # else
87
- # "''}"
88
- # end
89
- end,
90
-
91
- # short name
92
- item[:short_name].present? ? "-#{item[:short_name]}" : nil,
93
-
94
- # description and default
95
- [
96
- item[:description],
97
- item[:default].present? ? "[#{value_for_menu item[:default]}]" : nil
98
- ].compact.join(' '),
99
-
100
- # apply proccode, if present, to value
101
- # save value to options hash if option is named
102
- #
103
- lambda { |value|
104
- (item[:proccode] ? item[:proccode].call(value) : value).tap do |converted|
105
- opt_name = item[:opt_name]
106
- next if opt_name.nil?
107
-
108
- options[opt_name.to_sym] = converted if item[:opt_name]
109
- end
110
- }
111
- ].compact
112
- opts.on(*mmoo)
113
- end
114
-
115
- def menu_parse(menu_options)
116
- options = {}
117
- option_parser = OptionParser.new do |opts|
118
- menu_options.each do |item|
119
- item[:opt_name] = item[:opt_name]&.to_sym
120
- menu_option_append opts, options, item
121
- end
122
- end
123
-
124
- # filename defaults to basename of the program
125
- # without suffix in a directory ~/.options
126
- option_parser.load
127
- option_parser.environment # env defaults to the basename of the program.
128
- remainder = option_parser.parse!
129
-
130
- [options, remainder, option_parser.help]
131
- end
132
-
133
- # skip :reek:UtilityFunction
134
- def value_for_menu(value)
135
- case value.class.to_s
136
- when 'String'
137
- value
138
- when 'FalseClass', 'TrueClass'
139
- value ? '1' : '0'
140
- else
141
- value.to_s
142
- end
143
- end
144
- end
145
-
146
- include Menu
147
-
148
- def initialize(menu: {}, lambdas: nil, options: nil, version: nil)
149
- @menu = if menu.instance_of?(::String)
150
- filetext = File.read(menu).tap_yaml 'filetext'
151
- fileyaml = YAML.load(filetext)
152
- fileyaml.map(&:sym_keys)
153
- else
154
- menu
155
- end.tap_yaml '@menu'
156
- @lambdas = lambdas
157
- @version = version || '0.1'
158
- # @options = {}
159
- @options = if options.instance_of?(::String)
160
- YAML.safe_load(File.read(options)).sym_keys.tap_yaml '@options'
161
- else
162
- {}
163
- end #.tap_yaml '@options'
164
-
165
- parse!
166
- end
167
-
168
- def parse!
169
- @options, @remainder = menu_all(
170
- @menu,
171
- # @menu.map do |menu_item|
172
- # menu_item.tap_inspect 'menu_item'
173
- # mion = menu_item[:opt_name]&.to_sym.tap_inspect 'mion'
174
- # omion = @options[mion].tap_inspect 'omion'
175
- # unless omion.nil?
176
- # @options[menu_item[:default]] = omion
177
- # end
178
- # menu_item
179
- # end,
180
- {
181
- debug: ->(value) { tap_config value: value },
182
-
183
- # stdout_configuration: lambda { |_| self.options.tap_puts 'eop' },
184
- # stdout_configuration: (lambda { |options|
185
- # lambda { |v| options.tap_puts 'eop_l' }
186
- # }).call(@options),
187
-
188
- stdout_defaults: lambda { |_|
189
- menu_default_option_values(@menu).to_yaml.tap_puts
190
- },
191
- stdout_help: lambda { |_|
192
- menu_help(@menu).tap_puts
193
- exit
194
- },
195
- val_as_bool: lambda { |value|
196
- value.instance_of?(::String) ? (value.chomp != '0') : value
197
- },
198
- val_as_int: ->(value) { value.to_i },
199
- val_as_str: ->(value) { value.to_s },
200
- version: lambda { |_|
201
- @version.tap_puts
202
- exit
203
- }
204
- }.merge(@lambdas || {}),
205
- @options
206
- )
207
- @options #.tap_yaml '@options'
208
- end
209
- end