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

#initialize(verb, argv, parse_opts = {}) ⇒ Parser

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

#argvObject (readonly)

Returns the value of attribute argv.



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

def argv
  @argv
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

#verbObject (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

.for(argv) ⇒ Object



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 'meet', argv, :implicit_verb => true
  else
    new Handler.abbrev[argv.shift.sub(/^--/, '')], argv
  end
end

Instance Method Details

#parse(&blk) ⇒ Object



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

#runObject



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