Class: CommandMapper::Gen::Parsers::Help
- Inherits:
-
Object
- Object
- CommandMapper::Gen::Parsers::Help
- Defined in:
- lib/command_mapper/gen/parsers/help.rb
Direct Known Subclasses
Constant Summary collapse
- IGNORED_ARGUMENT_NAMES =
List of argument names to ignore
%w[option options opts]
- USAGE_PREFIX =
/^usage:\s+/i
- USAGE_LINE =
/#{USAGE_PREFIX}[a-z][a-z0-9_-]*/i
- USAGE_SECTION =
/^usage:$/i
- INDENT =
/^\s{2,}/
- OPTION_LINE =
/#{INDENT}-(?:[A-Za-z0-9]|-[A-Za-z0-9])/
- SUBCOMMAND =
/[a-z][a-z0-9]*(?:[_-][a-z0-9]+)*/
- SUBCOMMAND_LINE =
/^\s{2,}(#{SUBCOMMAND})(?:,\s[a-z][a-z0-9_-]*)?(?:\t|\s{2,}|$)/
Instance Attribute Summary collapse
- #command ⇒ Command readonly
-
#parser_error_callback ⇒ Proc(String, Parslet::ParserFailed)?
readonly
The callback to pass any parser errors.
Class Method Summary collapse
-
.parse(output, command, &block) ⇒ Command
Parses the
--help
output for the given command. -
.run(command, &block) ⇒ Command?
Runs the parser on the command's
--help
output.
Instance Method Summary collapse
-
#ignore_argument?(name) ⇒ Boolean
Determines whether to skip an argument based on it's name.
-
#initialize(command) {|line, parser_error| ... } ⇒ Help
constructor
Initializes the
--help
output parser. -
#parse(output) ⇒ Object
Parses
--help
output into #command. -
#parse_argument(argument, **kwargs) ⇒ Object
Parses an individual argument node.
-
#parse_argument_node(node, **kwargs) ⇒ Object
Parses a node within the arguments node.
-
#parse_arguments(arguments, **kwargs) ⇒ Object
Parses a collection of arguments.
-
#parse_option_line(line) ⇒ Object
Parses an option line (ex:
-o, --opt VALUE Blah blah blah
) into #command. - #parse_subcommand_line(line) ⇒ Object
-
#parse_usage(usage) ⇒ Object
Parses a
usage: ...
string into #command.
Constructor Details
#initialize(command) {|line, parser_error| ... } ⇒ Help
Initializes the --help
output parser.
34 35 36 37 38 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 34 def initialize(command,&block) @command = command @parser_error_callback = block end |
Instance Attribute Details
#command ⇒ Command (readonly)
12 13 14 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 12 def command @command end |
#parser_error_callback ⇒ Proc(String, Parslet::ParserFailed)? (readonly)
The callback to pass any parser errors.
17 18 19 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 17 def parser_error_callback @parser_error_callback end |
Class Method Details
.parse(output, command, &block) ⇒ Command
Parses the --help
output for the given command.
52 53 54 55 56 57 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 52 def self.parse(output,command,&block) parser = new(command,&block) parser.parse(output) return command end |
.run(command, &block) ⇒ Command?
Runs the parser on the command's --help
output.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 71 def self.run(command,&block) output = nil begin output = `#{command.command_string} --help 2>&1` rescue Errno::ENOENT # command not found raise(CommandNotInstalled,"command #{command.command_name.inspect} is not installed") end if output.empty? # --help not supported, fallback to trying -h output = `#{command.command_string} -h 2>&1` end parse(output,command,&block) unless output.empty? end |
Instance Method Details
#ignore_argument?(name) ⇒ Boolean
Determines whether to skip an argument based on it's name.
101 102 103 104 105 106 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 101 def ignore_argument?(name) name == @command.command_name || IGNORED_ARGUMENT_NAMES.any? { |suffix| name == suffix || name.end_with?(suffix) } end |
#parse(output) ⇒ Object
Parses --help
output into #command.
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 325 def parse(output) usage_on_next_line = false output.each_line do |line| if line =~ USAGE_SECTION usage_on_next_line = true elsif usage_on_next_line if line =~ INDENT parse_usage(line.strip) else usage_on_next_line = false end else if line =~ USAGE_LINE usage = line.sub(USAGE_PREFIX,'').chomp parse_usage(usage) elsif line =~ OPTION_LINE parse_option_line(line.chomp) elsif line =~ SUBCOMMAND_LINE parse_subcommand_line(line.chomp) end end end end |
#parse_argument(argument, **kwargs) ⇒ Object
Parses an individual argument node.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 117 def parse_argument(argument,**kwargs) name = argument[:name].to_s.downcase keywords = kwargs if argument[:repeats] keywords[:repeats] = true end # ignore [OPTIONS] or [opts] unless ignore_argument?(name) @command.argument(name.to_sym,**keywords) end end |
#parse_argument_node(node, **kwargs) ⇒ Object
Parses a node within the arguments node.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 136 def parse_argument_node(node,**kwargs) keywords = kwargs if node[:repeats] keywords[:repeats] = true end if node[:optional] keywords[:required] = false parse_arguments(node[:optional], **keywords) elsif node[:argument] parse_argument(node[:argument], **keywords) end end |
#parse_arguments(arguments, **kwargs) ⇒ Object
Parses a collection of arguments.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 157 def parse_arguments(arguments,**kwargs) case arguments when Array keywords = kwargs if arguments.delete({repeats: '...'}) keywords[:repeats] = true end arguments.each do |node| parse_argument_node(node,**keywords) end when Hash parse_argument_node(arguments,**kwargs) end end |
#parse_option_line(line) ⇒ Object
Parses an option line (ex: -o, --opt VALUE Blah blah blah
)
into #command.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 205 def parse_option_line(line) parser = Parsers::Options.new tree = begin parser.parse(line) rescue Parslet::ParseFailed => error if @parser_error_callback @parser_error_callback.call(line,error) end return end flag = tree[:long_flag] || tree[:short_flag] keywords = {} if tree[:equals] keywords[:equals] = true end if tree[:optional] if tree[:optional][:equals] keywords[:equals] = :optional end value_node = tree[:optional][:value] keywords[:value] = {required: false} elsif tree[:value] value_node = tree[:value] keywords[:value] = {required: true} end if value_node if value_node[:list] separator = value_node[:list][:separator] keywords[:value][:type] = Types::List.new( separator: separator.to_s ) elsif value_node[:key_value] separator = value_node[:key_value][:separator] keywords[:value][:type] = Types::KeyValue.new( separator: separator.to_s ) elsif value_node[:literal_values] literal_values = [] value_node[:literal_values].each do |node| literal_values << node[:string].to_s end # perform some value coercion type = case literal_values when %w[YES NO] Types::Map.new(true => 'YES', false => 'NO') when %w[Yes No] Types::Map.new(true => 'Yes', false => 'No') when %w[yes no] Types::Map.new(true => 'yes', false => 'no') when %w[Y N] Types::Map.new(true => 'Y', false => 'N') when %w[y n] Types::Map.new(true => 'y', false => 'n') when %w[ENABLED DISABLED] Types::Map.new(true => 'ENABLED', false => 'DISABLED') when %w[Enabled Disabled] Types::Map.new(true => 'Enabled', false => 'Disabled') when %w[enabled disabled] Types::Map.new(true => 'enabled', false => 'disabled') else Types::Enum.new(literal_values.map(&:to_sym)) end keywords[:value][:type] = type elsif value_node[:name] case value_node[:name] when 'NUM' keywords[:value][:type] = Types::Num.new end end end if flag @command.option(flag.to_s, **keywords) else warn "could not detect option flag: #{line}" end end |
#parse_subcommand_line(line) ⇒ Object
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 308 def parse_subcommand_line(line) if (match = line.match(SUBCOMMAND)) subcommand_name = match[0] # filter out self-referetial subcommands unless subcommand_name == @command.command_name @command.subcommand(subcommand_name) end end end |
#parse_usage(usage) ⇒ Object
Parses a usage: ...
string into #command.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/command_mapper/gen/parsers/help.rb', line 179 def parse_usage(usage) parser = Usage.new # remove the command name and any subcommands args = usage.sub("#{@command.command_string} ",'') tree = begin parser.args.parse(args) rescue Parslet::ParseFailed => error if @parser_error_callback @parser_error_callback.call(usage,error) end return end parse_arguments(tree) end |