Class: ShellOpts::ShellOpts
- Inherits:
-
Object
- Object
- ShellOpts::ShellOpts
- Defined in:
- lib/shellopts.rb
Overview
The compilation object
Instance Attribute Summary collapse
-
#ast ⇒ Object
readonly
The AST resulting from parsing the command line arguments.
-
#grammar ⇒ Object
readonly
The grammar compiled from the usage string.
-
#program_name ⇒ Object
readonly
Name of program.
Instance Method Summary collapse
-
#args ⇒ Object
List of remaining non-option command line arguments.
-
#each(&block) ⇒ Object
Iterate the result as name/value pairs.
-
#error(*msgs) ⇒ Object
Print error message and usage string and exit with status 1.
-
#fail(*msgs) ⇒ Object
Print error message and exit with status 1.
-
#initialize(usage, argv, program_name: File.basename($0)) ⇒ ShellOpts
constructor
Compile a usage string into a grammar and use that to parse command line arguments.
-
#to_a ⇒ Object
Unroll the AST into a nested array.
-
#usage ⇒ Object
Prettified usage string used by #error and #fail.
Constructor Details
#initialize(usage, argv, program_name: File.basename($0)) ⇒ ShellOpts
Compile a usage string into a grammar and use that to parse command line arguments
usage is the usage string, and argv the command line (typically the global ARGV array). program_name is the name of the program and is used in error messages. It defaults to the basename of the program
Errors in the usage string raise a CompilerError exception. Errors in the argv arguments terminates the program with an error message
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/shellopts.rb', line 163 def initialize(usage, argv, program_name: File.basename($0)) @program_name = program_name begin @grammar = Grammar.compile(program_name, usage) @ast = Ast.parse(@grammar, argv) rescue Grammar::Compiler::Error => ex raise CompilerError.new(5, ex.) rescue Ast::Parser::Error => ex error(ex.) end end |
Instance Attribute Details
#ast ⇒ Object (readonly)
The AST resulting from parsing the command line arguments
149 150 151 |
# File 'lib/shellopts.rb', line 149 def ast @ast end |
#grammar ⇒ Object (readonly)
The grammar compiled from the usage string. If #ast is defined, it’s equal to ast.grammar
146 147 148 |
# File 'lib/shellopts.rb', line 146 def grammar @grammar end |
#program_name ⇒ Object (readonly)
Name of program
139 140 141 |
# File 'lib/shellopts.rb', line 139 def program_name @program_name end |
Instance Method Details
#args ⇒ Object
List of remaining non-option command line arguments. Shorthand for ast.arguments
152 |
# File 'lib/shellopts.rb', line 152 def args() @ast.arguments end |
#each(&block) ⇒ Object
Iterate the result as name/value pairs. See ShellOpts.process for a detailed description
182 183 184 185 186 187 188 |
# File 'lib/shellopts.rb', line 182 def each(&block) if block_given? to_a.each { |*args| yield(*args) } else to_a # FIXME: Iterator end end |
#error(*msgs) ⇒ Object
Print error message and usage string and exit with status 1. This method should be called in response to user-errors (eg. specifying an illegal option)
193 194 195 |
# File 'lib/shellopts.rb', line 193 def error(*msgs) ::ShellOpts.emit_and_exit(program_name, true, usage, msgs) end |
#fail(*msgs) ⇒ Object
Print error message and exit with status 1. This method should not be called in response to user-errors but system errors (like disk full)
199 200 201 |
# File 'lib/shellopts.rb', line 199 def fail(*msgs) ::ShellOpts.emit_and_exit(program_name, false, nil, msgs) end |
#to_a ⇒ Object
Unroll the AST into a nested array
176 177 178 |
# File 'lib/shellopts.rb', line 176 def to_a @ast.values end |
#usage ⇒ Object
Prettified usage string used by #error and #fail. Shorthand for grammar.usage
142 |
# File 'lib/shellopts.rb', line 142 def usage() @grammar.usage end |