Class: OptParsePlus

Inherits:
Object
  • Object
show all
Defined in:
lib/opt_parse_plus.rb

Defined Under Namespace

Classes: OptionsFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ OptParsePlus



19
20
21
22
23
24
25
26
27
# File 'lib/opt_parse_plus.rb', line 19

def initialize(parent = nil)
  @parent = parent || self
  @parser = OptionParser.new
  @options = OptionsFound.new
  @commands = {}
  @summary = ""

  add_default_help
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/opt_parse_plus.rb', line 5

def options
  @options
end

#summaryObject (readonly)

Returns the value of attribute summary.



5
6
7
# File 'lib/opt_parse_plus.rb', line 5

def summary
  @summary
end

Instance Method Details

#add_command(command_name, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/opt_parse_plus.rb', line 33

def add_command(command_name, &block)
  command_parser = OptParsePlus.new(self)

  base_command = base_command_name(command_name.to_s)
  rest = command_name.gsub(base_command, '')

  command_parser.banner "Usage: #$0 #{base_command} [options]#{rest}"
  command_parser.add_options(&block) if block_given?
  @commands[base_command] = command_parser
end

#add_options(&block) ⇒ Object



29
30
31
# File 'lib/opt_parse_plus.rb', line 29

def add_options(&block)
  instance_exec(&block)
end


51
52
53
# File 'lib/opt_parse_plus.rb', line 51

def banner(message)
  @parser.banner = message
end

#description(message) ⇒ Object



63
64
65
66
67
68
# File 'lib/opt_parse_plus.rb', line 63

def description(message)
  @summary = clean_up_white_space(message)
  @parser.separator("")
  @parser.separator(@summary)
  @parser.separator("")
end

#group(group_name = nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/opt_parse_plus.rb', line 55

def group(group_name = nil)
  if group_name
    @group = group_name
  else
    @group
  end
end

#helpObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/opt_parse_plus.rb', line 93

def help
  help_parts   = []
  help_parts << @parser.to_s

  if @commands.any?
    help_parts << ["Known Commands", ""]
    command_list = group_and_sort_command_help

    command_list.each do |cmd_line|
      help_parts << cmd_line
    end

    help_parts << ""
  end

  help_parts << ""
  help_parts.flatten.join("\n")
end

#option(*arguments) ⇒ Object



44
45
46
47
48
49
# File 'lib/opt_parse_plus.rb', line 44

def option(*arguments)
  argument_name = parse_argument_name(arguments)
  @parser.on(*arguments) do |arg|
    @options[argument_name] = arg
  end
end

#parse!(command_line) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/opt_parse_plus.rb', line 74

def parse!(command_line)
  @parser.order!(command_line)
  final = @options

  next_token = command_line.first

  if command_parser = @commands[next_token]
    command_line.shift
    command_parser.parse!(command_line)

    final.command_found = next_token
    final.merge!({
      next_token => command_parser.options
    })
  end

  final
end

#set(key, value) ⇒ Object



70
71
72
# File 'lib/opt_parse_plus.rb', line 70

def set(key, value)
  @options[key] = value
end