Class: OptParsePlus
- Inherits:
-
Object
- Object
- OptParsePlus
- Defined in:
- lib/opt_parse_plus.rb
Defined Under Namespace
Classes: OptionsFound
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#summary ⇒ Object
readonly
Returns the value of attribute summary.
Instance Method Summary collapse
- #add_command(command_name, &block) ⇒ Object
- #add_options(&block) ⇒ Object
- #banner(message) ⇒ Object
- #description(message) ⇒ Object
- #group(group_name = nil) ⇒ Object
- #help ⇒ Object
-
#initialize(parent = nil) ⇒ OptParsePlus
constructor
A new instance of OptParsePlus.
- #option(*arguments) ⇒ Object
- #parse!(command_line) ⇒ Object
- #set(key, value) ⇒ Object
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 = OptionsFound.new @commands = {} @summary = "" add_default_help end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/opt_parse_plus.rb', line 5 def end |
#summary ⇒ Object (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. "Usage: #$0 #{base_command} [options]#{rest}" command_parser.(&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 (&block) instance_exec(&block) end |
#banner(message) ⇒ Object
51 52 53 |
# File 'lib/opt_parse_plus.rb', line 51 def () @parser. = end |
#description(message) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/opt_parse_plus.rb', line 63 def description() @summary = clean_up_white_space() @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 |
#help ⇒ Object
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| [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 = 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. }) end final end |
#set(key, value) ⇒ Object
70 71 72 |
# File 'lib/opt_parse_plus.rb', line 70 def set(key, value) [key] = value end |