Class: Miniparse::Parser
- Inherits:
-
Object
- Object
- Miniparse::Parser
- Defined in:
- lib/miniparse/parser.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
After parsing (i.e. specified) rest of arguments.
-
#program_desc ⇒ Object
readonly
After parsing (i.e. specified) rest of arguments.
Instance Method Summary collapse
-
#add_command(name, desc, opts = {}, &block) ⇒ Object
:no_options indicates the command has no command line options.
-
#add_option(spec, desc, opts = {}, &block) ⇒ Object
in the command line (ex. “–debug” or “–verbose LEVEL”).
- #command ⇒ Object
- #command_args ⇒ Object
- #command_options ⇒ Object
-
#current_command ⇒ Object
The command the next add_option will apply to.
-
#help_desc ⇒ Object
A help message with the short descriptions.
-
#help_usage ⇒ Object
A usage message.
-
#initialize(program_description = nil) ⇒ Parser
constructor
A new instance of Parser.
- #options ⇒ Object
-
#parse(argv) ⇒ Object
Unprocessed arguments.
Constructor Details
#initialize(program_description = nil) ⇒ Parser
Returns a new instance of Parser.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/miniparse/parser.rb', line 22 def initialize(program_description = nil) @commander = Commander.new @program_desc = program_description @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 ⇒ Object (readonly)
Returns after parsing (i.e. specified) rest of arguments.
9 10 11 |
# File 'lib/miniparse/parser.rb', line 9 def args @args end |
#program_desc ⇒ Object (readonly)
Returns after parsing (i.e. specified) rest of arguments.
9 10 11 |
# File 'lib/miniparse/parser.rb', line 9 def program_desc @program_desc end |
Instance Method Details
#add_command(name, desc, opts = {}, &block) ⇒ Object
:no_options indicates the command has no command line options
53 54 55 56 |
# File 'lib/miniparse/parser.rb', line 53 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) ⇒ Object
in the command line (ex. “–debug” or “–verbose LEVEL”)
:default :negatable (used only for switches) :shortable
42 43 44 45 |
# File 'lib/miniparse/parser.rb', line 42 def add_option(spec, desc, opts={}, &block) args = opts.merge(spec: spec, desc: desc) current_broker.add_option(args, &block) end |
#command ⇒ Object
14 |
# File 'lib/miniparse/parser.rb', line 14 def command; commander.parsed_command; end |
#command_args ⇒ Object
16 |
# File 'lib/miniparse/parser.rb', line 16 def command_args; commander.parsed_args; end |
#command_options ⇒ Object
15 |
# File 'lib/miniparse/parser.rb', line 15 def ; commander.parsed_values; end |
#current_command ⇒ Object
Returns the command the next add_option will apply to.
19 |
# File 'lib/miniparse/parser.rb', line 19 def current_command; commander.current_command; end |
#help_desc ⇒ Object
Returns a help message with the short descriptions.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/miniparse/parser.rb', line 81 def help_desc #FIXME text = "" if (global = global_broker.help_desc).size > 0 text += "\nOptions:\n" text += global end text += commander.help_desc text end |
#help_usage ⇒ Object
Returns a usage message.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/miniparse/parser.rb', line 93 def help_usage #FIXME if Miniparse.control(:detailed_usage) right_text = @global_broker.help_usage elsif current_command right_text = "[global_options]" else right_text = "[options]" end if current_command right_text += " <command> [command_options]" end right_text += " <args>" Miniparse.help_usage_format(right_text) end |
#options ⇒ Object
11 |
# File 'lib/miniparse/parser.rb', line 11 def ; global_broker.parsed_values; end |
#parse(argv) ⇒ Object
Returns unprocessed arguments.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/miniparse/parser.rb', line 60 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) if cmd_name commander.parse_argv(cmd_name, cmd_argv) end if Miniparse.control(:raise_global_args) && (! args.empty?) # FIXME review this logic later error = current_command ? "unrecognized command" : "extra arguments" raise ArgumentError, "#{error} '#{args[0]}'" end args end end |