Class: Babushka::Cmdline::Parser

Inherits:
Object
  • Object
show all
Includes:
LogHelpers
Defined in:
lib/babushka/cmdline/parser.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Constructor Details

- (Parser) initialize(verb, argv, parse_opts = {})

Returns a new instance of Parser



20
21
22
23
24
# File 'lib/babushka/cmdline/parser.rb', line 20

def initialize verb, argv, parse_opts = {}
  @verb, @argv, @opts, @implicit_verb = verb, argv, default_opts, parse_opts[:implicit_verb]
  parse(&Handler.for('global').opt_definer)
  parse(&Handler.for(verb).opt_definer)
end

Instance Attribute Details

- (Object) argv (readonly)

Returns the value of attribute argv



8
9
10
# File 'lib/babushka/cmdline/parser.rb', line 8

def argv
  @argv
end

- (Object) opts (readonly)

Returns the value of attribute opts



8
9
10
# File 'lib/babushka/cmdline/parser.rb', line 8

def opts
  @opts
end

- (Object) verb (readonly)

Returns the value of attribute verb



8
9
10
# File 'lib/babushka/cmdline/parser.rb', line 8

def verb
  @verb
end

Class Method Details

+ (Object) for(argv)



10
11
12
13
14
15
16
17
18
# File 'lib/babushka/cmdline/parser.rb', line 10

def self.for argv
  if argv.empty? || (%w[-h --help] & argv).any?
    new 'help', argv
  elsif Handler.abbrev.has_key?(argv.first.sub(/^--/, ''))
    new Handler.abbrev[argv.shift.sub(/^--/, '')], argv
  else
    new 'meet', argv, :implicit_verb => true
  end
end

Instance Method Details

- (Object) parse(&blk)



37
38
39
# File 'lib/babushka/cmdline/parser.rb', line 37

def parse &blk
  instance_eval(&blk)
end


33
34
35
# File 'lib/babushka/cmdline/parser.rb', line 33

def print_usage
  log parser.to_s.sub(/^Usage:.*$/, '')
end

- (Object) run



26
27
28
29
30
31
# File 'lib/babushka/cmdline/parser.rb', line 26

def run
  parser.parse!(argv)
  Handler.for(verb).handler.call(self)
rescue OptionParser::ParseError => e
  log_error "The #{e.args.first} option #{error_reason(e)}. #{hint}"
end