Class: Ace::Support::Cli::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/cli/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(command_class, command_name: nil) ⇒ Parser

Returns a new instance of Parser.



10
11
12
13
# File 'lib/ace/support/cli/parser.rb', line 10

def initialize(command_class, command_name: nil)
  @command_class = command_class
  @command_name = command_name
end

Instance Method Details

#parse(args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ace/support/cli/parser.rb', line 15

def parse(args)
  options = build_defaults
  remaining = args.dup

  if help_requested?(remaining) && rich_help?
    render_help(remaining)
  end

  parser = OptionParser.new
  parser.banner = "Usage: #{File.basename($0)} #{command_label} [options]"

  configure_options(parser, options)

  parser.parse!(remaining)

  apply_positionals(options, remaining)
  validate_required_options!(options)

  options
rescue OptionParser::ParseError => e
  raise ParseError, parse_error_message(e)
rescue ArgumentError => e
  raise ParseError, e.message
end