Class: UnimatrixParser::Parser
- Inherits:
-
Object
- Object
- UnimatrixParser::Parser
- Defined in:
- lib/unimatrix_parser.rb
Overview
Parser Class
Constant Summary collapse
- INVALID_SHORT_ARG_REGEX =
/[\d-]/
- FLOAT_REGEX =
/^-?((\d+(\.\d+)?)|(\.\d+))([eE][-+]?[\d]+)?$/
- PARAM_REGEX =
/^-(-|\.$|[^\d\.])/
Instance Attribute Summary collapse
-
#ignore_invalid_options ⇒ Object
Returns the value of attribute ignore_invalid_options.
-
#leftovers ⇒ Object
readonly
Returns the value of attribute leftovers.
-
#specs ⇒ Object
readonly
Returns the value of attribute specs.
Instance Method Summary collapse
- #conflicts(*keys) ⇒ Object
- #depends(*keys) ⇒ Object
- #die(arg, message = nil, error_code = nil) ⇒ Object
- #educate(stream = $stdout) ⇒ Object
-
#initialize(*a, &b) ⇒ Parser
constructor
A new instance of Parser.
- #options(options = []) ⇒ Object
- #parse(cmdline = ARGV) ⇒ Object
- #parse_date_parameter(param, arg) ⇒ Object
- #stop_on_unknown ⇒ Object
- #synopsis(synopsis = nil) ⇒ Object
- #width ⇒ Object
- #wrap(str, opts = {}) ⇒ Object
Constructor Details
#initialize(*a, &b) ⇒ Parser
Returns a new instance of Parser.
293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/unimatrix_parser.rb', line 293 def initialize( *a, &b ) @leftovers = [] @specs = {} @long = {} @short = {} @order = [] @constraints = [] @stop_on_unknown = false @synopsis = nil cloaker( &b ).bind( self ).call( *a ) if b end |
Instance Attribute Details
#ignore_invalid_options ⇒ Object
Returns the value of attribute ignore_invalid_options.
291 292 293 |
# File 'lib/unimatrix_parser.rb', line 291 def @ignore_invalid_options end |
#leftovers ⇒ Object (readonly)
Returns the value of attribute leftovers.
289 290 291 |
# File 'lib/unimatrix_parser.rb', line 289 def leftovers @leftovers end |
#specs ⇒ Object (readonly)
Returns the value of attribute specs.
290 291 292 |
# File 'lib/unimatrix_parser.rb', line 290 def specs @specs end |
Instance Method Details
#conflicts(*keys) ⇒ Object
341 342 343 344 345 346 |
# File 'lib/unimatrix_parser.rb', line 341 def conflicts( *keys ) keys.each do | key | raise ArgumentError, "unknown option '#{ key }'" unless @specs[ key ] end @constraints << [ :conflicts, keys ] end |
#depends(*keys) ⇒ Object
334 335 336 337 338 339 |
# File 'lib/unimatrix_parser.rb', line 334 def depends( *keys ) keys.each do | key | raise ArgumentError, "unknown option '#{ key }'" unless @specs[ key ] end @constraints << [ :depends, keys ] end |
#die(arg, message = nil, error_code = nil) ⇒ Object
514 515 516 517 518 519 520 521 522 523 |
# File 'lib/unimatrix_parser.rb', line 514 def die( arg, = nil, error_code = nil ) if $stderr.puts "Error: argument --#{ @specs[ arg ].long } #{ }." else $stderr.puts "Error: #{ arg }." end $stderr.puts educate $stderr exit( error_code || -1 ) end |
#educate(stream = $stdout) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/unimatrix_parser.rb', line 418 def educate( stream = $stdout ) width # hack: calculate it now; otherwise we have to be careful not to # call this unless the cursor's at the beginning of a line. left = {} @specs.each do | name, spec | left[ name ] = ( spec.short? ? "-#{ spec.short }, " : "" ) + "--#{ spec.long }" + case spec.type when :flag then "" when :int then "=<i>" when :ints then "=<i+>" when :string then "=<s>" when :strings then "=<s+>" when :float then "=<f>" when :floats then "=<f+>" when :io then "=<filename/uri>" when :ios then "=<filename/uri+>" when :date then "=<date>" when :dates then "=<date+>" end + ( spec.flag? && spec.default ? ", --no-#{ spec.long }" : "" ) end leftcol_width = left.values.map( &:length ).max || 0 rightcol_start = leftcol_width + 6 unless @order.size > 0 && @order.first.first == :text command_name = File.basename( $0 ).gsub( /\.[^.]+$/, '' ) stream.puts "#{ @synopsis }\n" if @synopsis stream.puts "Options:" end @order.each do | type, option | if type == :text stream.puts wrap( option ) next end spec = @specs[ option ] stream.printf " %-#{ leftcol_width }s ", left[ option ] description = spec.description + begin default_s = case spec.default when $stdout then "<stdout>" when $stdin then "<stdin>" when $stderr then "<stderr>" when Array spec.default.join( ", " ) else spec.default.to_s end if spec.default if spec.description =~ /\.$/ " (Default: #{ default_s })" else " (default: #{ default_s })" end else "" end end stream.puts wrap( description, :width => width - rightcol_start - 1, :prefix => rightcol_start ) end end |
#options(options = []) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/unimatrix_parser.rb', line 306 def ( = [] ) .each do | option | if @specs.member?( option.name ) # raise ArgumentError, "you already have an argument named '#{ option.name }'" .delete( option ) # ignore duplicate option elsif @long[ option.long ] raise ArgumentError, "long option name #{ option.long.inspect } " + "is already taken; please specify a (different) :long" elsif @short[ option.short ] raise ArgumentError, "short option name #{ option.short.inspect } " + "is already taken; please specify a (different) :short" else @long[ option.long ] = option.name @short[ option.short ] = option.name if option.short? @specs[ option.name ] = option @order << [ :option, option.name ] end end end |
#parse(cmdline = ARGV) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/unimatrix_parser.rb', line 348 def parse( cmdline = ARGV ) values = {} required = {} @specs.each do | key, option | required[ key ] = true if option.required? values[ key ] = option.default values[ key ] = [] if option.multi && !option.default end given_args = resolve_symbols( cmdline ) raise HelpNeeded if given_args.include? :help @constraints.each do | type, syms | constraint_sym = syms.find { | sym | given_args[ sym ] } next unless constraint_sym case type when :depends syms.each do | sym | unless given_args.include? sym raise CommandlineError, "--#{ @specs[ constraint_sym ].long } " + "requires --#{ @specs[ sym ].long }" end end when :conflicts syms.each do | sym | if given_args.include?( sym ) && ( sym != constraint_sym ) raise CommandlineError, "--#{ @specs[ constraint_sym ].long } " + "conflicts with --#{ @specs[ sym ].long }" end end end end required.each do | key, val | unless given_args.include? key raise CommandlineError, "option --#{ @specs[ key ].long } must be specified" end end parse_parameters( given_args, values ) cmdline.clear @leftovers.each { | leftover | cmdline << leftover } # allow openstruct-style accessors class << values def method_missing( m, *_args ) self[ m ] || self[ m.to_s ] end end values end |
#parse_date_parameter(param, arg) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/unimatrix_parser.rb', line 406 def parse_date_parameter( param, arg ) begin require 'chronic' time = Chronic.parse(param) rescue LoadError # chronic is not available end time ? Date.new( time.year, time.month, time.day ) : Date.parse( param ) rescue ArgumentError raise CommandlineError, "option '#{ arg }' needs a date" end |
#stop_on_unknown ⇒ Object
330 331 332 |
# File 'lib/unimatrix_parser.rb', line 330 def stop_on_unknown @stop_on_unknown = true end |
#synopsis(synopsis = nil) ⇒ Object
326 327 328 |
# File 'lib/unimatrix_parser.rb', line 326 def synopsis( synopsis = nil ) synopsis ? @synopsis = synopsis : @synopsis end |
#width ⇒ Object
487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/unimatrix_parser.rb', line 487 def width @width ||= if $stdout.tty? begin require 'io/console' w = IO.console.winsize.last w.to_i > 0 ? w : 80 rescue LoadError, NoMethodError, Errno::ENOTTY, Errno::EBADF, Errno::EINVAL legacy_width end else 80 end end |
#wrap(str, opts = {}) ⇒ Object
501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/unimatrix_parser.rb', line 501 def wrap( str, opts = {} ) if str == "" [ "" ] else inner = false str.split( "\n" ).map do | s | line = wrap_line s, opts.merge( :inner => inner ) inner = true line end.flatten end end |