Class: Miniparse::Parser
- Inherits:
-
Object
- Object
- Miniparse::Parser
- Defined in:
- lib/miniparse/parser.rb
Overview
this is the key class to the miniparse library, please find below an example of use:
require 'miniparse'
parser = Miniparse::Parser.new
parser.add_option "--debug", "activate debugging"
parser.parse ARGV
if parser.[:debug]
puts "DEBUG ACTIVATED!"
else
puts "run silently"
end
Instance Attribute Summary collapse
-
#args ⇒ array
readonly
Rest of arguments after parsing.
Instance Method Summary collapse
-
#add_command(name, desc, opts = {}, &block) ⇒ void
Added Command.
-
#add_option(spec, desc, opts = {}, &block) ⇒ void
Added Option.
-
#add_program_description(desc) ⇒ void
Same argument.
-
#command_args ⇒ array
Remaining command args after parsing the options for the parsed command.
-
#command_name ⇒ symbol|nil
Name of command parsed , i.e.
-
#command_options ⇒ hash
Parsed option values for the parsed command.
-
#current_command_name ⇒ symbol
The name of the command the next #add_option will apply to.
-
#help_desc ⇒ string
A help message with the short descriptions.
-
#help_usage ⇒ string
A usage message.
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
-
#options ⇒ hash
Parsed, i.e.
-
#parse(argv) ⇒ array
Unprocessed arguments.
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/miniparse/parser.rb', line 41 def initialize @commander = Commander.new @program_desc = nil @global_broker = OptionBroker.new do print program_desc + "\n" if program_desc print help_usage + "\n" print help_desc + "\n" exit ERR_HELP_REQ end end |
Instance Attribute Details
#args ⇒ array (readonly)
Returns rest of arguments after parsing.
24 25 26 |
# File 'lib/miniparse/parser.rb', line 24 def args @args end |
Instance Method Details
#add_command(name, desc, opts = {}, &block) ⇒ void
This method returns an undefined value.
Returns added Command.
80 81 82 83 |
# File 'lib/miniparse/parser.rb', line 80 def add_command(name, desc, opts={}, &block) args = opts.merge(spec: name, desc: desc) commander.add_command(args, &block) end |
#add_option(spec, desc, opts = {}, &block) ⇒ void
This method returns an undefined value.
Returns added Option.
68 69 70 71 |
# File 'lib/miniparse/parser.rb', line 68 def add_option(spec, desc, opts={}, &block) args = opts.merge(spec: spec, desc: desc) current_broker.add_option(args, &block) end |
#add_program_description(desc) ⇒ void
This method returns an undefined value.
Returns same argument.
54 55 56 |
# File 'lib/miniparse/parser.rb', line 54 def add_program_description(desc) @program_desc = desc end |
#command_args ⇒ array
Returns remaining command args after parsing the options for the parsed command.
36 |
# File 'lib/miniparse/parser.rb', line 36 def command_args; commander.parsed_args; end |
#command_name ⇒ symbol|nil
Returns name of command parsed , i.e. specified, (or nil if none).
30 |
# File 'lib/miniparse/parser.rb', line 30 def command_name; commander.parsed_command_name; end |
#command_options ⇒ hash
Returns parsed option values for the parsed command.
33 |
# File 'lib/miniparse/parser.rb', line 33 def ; commander.parsed_values; end |
#current_command_name ⇒ symbol
Returns the name of the command the next #add_option will apply to.
39 |
# File 'lib/miniparse/parser.rb', line 39 def current_command_name; commander.current_command_name; end |
#help_desc ⇒ string
Returns a help message with the short descriptions.
106 107 108 109 110 111 112 113 114 |
# File 'lib/miniparse/parser.rb', line 106 def help_desc text = "" if (global = global_broker.help_desc).size > 0 text += "\nOptions:\n" text += global end text += commander.help_desc text end |
#help_usage ⇒ string
Returns a usage message.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/miniparse/parser.rb', line 117 def help_usage if Miniparse.control(:detailed_usage) right_text = @global_broker.help_usage elsif current_command_name right_text = "[global_options]" else right_text = "[options]" end if current_command_name right_text += " <command> [command_options]" end right_text += " <args>" Miniparse.help_usage_format(right_text) end |
#options ⇒ hash
Returns parsed, i.e. specified, global options.
27 |
# File 'lib/miniparse/parser.rb', line 27 def ; global_broker.parsed_values; end |
#parse(argv) ⇒ array
Returns unprocessed arguments.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/miniparse/parser.rb', line 87 def parse(argv) if Miniparse.control(:help_cmdline_empty) && argv.empty? puts help_usage exit ERR_HELP_REQ end try_argument do global_argv, cmd_name, cmd_argv = commander.split_argv(argv) @args = global_broker.parse_argv(global_argv) commander.parse_argv(cmd_name, cmd_argv) if cmd_name if Miniparse.control(:raise_global_args) && !args.empty? error = current_command_name ? "unrecognized command" : "extra arguments" raise ArgumentError, "#{error} '#{args[0]}'" end args end end |