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)
-
- (array) args
readonly
Rest of arguments after parsing.
Instance Method Summary (collapse)
-
- (void) add_command(name, desc, opts = {}, &block)
Added Command.
-
- (void) add_option(spec, desc, opts = {}, &block)
Added Option.
-
- (void) add_program_description(desc)
Same argument.
-
- (array) command_args
Remaining command args after parsing the options for the parsed command.
-
- (symbol|nil) command_name
Name of command parsed , i.e.
-
- (hash) command_options
Parsed option values for the parsed command.
-
- (symbol) current_command_name
The name of the command the next #add_option will apply to.
-
- (string) help_desc
A help message with the short descriptions.
-
- (string) help_usage
A usage message.
-
- (Parser) initialize
constructor
A new instance of Parser.
-
- (hash) options
Parsed, i.e.
-
- (array) parse(argv)
Unprocessed arguments.
Constructor Details
- (Parser) initialize
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
- (array) args (readonly)
Returns rest of arguments after parsing
24 25 26 |
# File 'lib/miniparse/parser.rb', line 24 def args @args end |
Instance Method Details
- (void) add_command(name, desc, opts = {}, &block)
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 |
- (void) add_option(spec, desc, opts = {}, &block)
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 |
- (void) add_program_description(desc)
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 |
- (array) command_args
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 |
- (symbol|nil) command_name
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 |
- (hash) command_options
Returns parsed option values for the parsed command
33 |
# File 'lib/miniparse/parser.rb', line 33 def ; commander.parsed_values; end |
- (symbol) current_command_name
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 |
- (string) help_desc
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 |
- (string) help_usage
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 |
- (hash) options
Returns parsed, i.e. specified, global options
27 |
# File 'lib/miniparse/parser.rb', line 27 def ; global_broker.parsed_values; end |
- (array) parse(argv)
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 |