Class: CommandMapper::Gen::CLI
- Inherits:
-
Object
- Object
- CommandMapper::Gen::CLI
- Defined in:
- lib/command_mapper/gen/cli.rb
Constant Summary collapse
- PROGRAM_NAME =
"command_mapper-gen"- PARSERS =
{ 'help' => Parsers::Help, 'man' => Parsers::Man }
- BUG_REPORT_URL =
"https://github.com/postmodern/command_mapper-gen.rb/issues/new"
Instance Attribute Summary collapse
-
#command ⇒ Command
readonly
The parsed command.
-
#debug ⇒ Boolean?
readonly
Specifies whether debug output should be printed.
-
#option_parser ⇒ OptionParser
readonly
The option parser.
-
#output ⇒ File?
readonly
The output file or
nilfor stdout. -
#parsers ⇒ Array<Parsers::Help, Parsers::Man>
readonly
The parsers to run.
Class Method Summary collapse
-
.run(argv = ARGV) ⇒ Integer
Initializes and runs the command.
Instance Method Summary collapse
-
#initialize ⇒ CLI
constructor
Initializes the command.
-
#print_backtrace(exception) ⇒ Object
Prints a backtrace to stderr.
-
#print_error(error) ⇒ Object
Prints an error message to stderr.
-
#print_parser_error(command, string, error) ⇒ Object
Prints a parsing error to stderr.
-
#run(argv = ARGV) ⇒ Integer
Runs the command.
Constructor Details
Instance Attribute Details
#command ⇒ Command (readonly)
The parsed command.
38 39 40 |
# File 'lib/command_mapper/gen/cli.rb', line 38 def command @command end |
#debug ⇒ Boolean? (readonly)
Specifies whether debug output should be printed.
33 34 35 |
# File 'lib/command_mapper/gen/cli.rb', line 33 def debug @debug end |
#option_parser ⇒ OptionParser (readonly)
The option parser.
43 44 45 |
# File 'lib/command_mapper/gen/cli.rb', line 43 def option_parser @option_parser end |
#output ⇒ File? (readonly)
The output file or nil for stdout.
23 24 25 |
# File 'lib/command_mapper/gen/cli.rb', line 23 def output @output end |
#parsers ⇒ Array<Parsers::Help, Parsers::Man> (readonly)
The parsers to run.
28 29 30 |
# File 'lib/command_mapper/gen/cli.rb', line 28 def parsers @parsers end |
Class Method Details
.run(argv = ARGV) ⇒ Integer
Initializes and runs the command.
66 67 68 69 70 71 72 73 74 |
# File 'lib/command_mapper/gen/cli.rb', line 66 def self.run(argv=ARGV) new().run(argv) rescue Interrupt # https://tldp.org/LDP/abs/html/exitcodes.html return 130 rescue Errno::EPIPE # STDOUT pipe broken return 0 end |
Instance Method Details
#print_backtrace(exception) ⇒ Object
Prints a backtrace to stderr.
222 223 224 225 226 227 228 229 |
# File 'lib/command_mapper/gen/cli.rb', line 222 def print_backtrace(exception) $stderr.puts "Oops! Looks like you've found a bug!" $stderr.puts "Please report the following to: #{BUG_REPORT_URL}" $stderr.puts $stderr.puts "```" $stderr.puts "#{exception.}" $stderr.puts "```" end |
#print_error(error) ⇒ Object
Prints an error message to stderr.
183 184 185 |
# File 'lib/command_mapper/gen/cli.rb', line 183 def print_error(error) $stderr.puts "#{PROGRAM_NAME}: #{error}" end |
#print_parser_error(command, string, error) ⇒ Object
Prints a parsing error to stderr.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/command_mapper/gen/cli.rb', line 199 def print_parser_error(command,string,error) $stderr.puts "Failed to parse line in `#{command.command_string} --help`:" $stderr.puts $stderr.puts " #{string}" $stderr.puts if @debug error.parse_failure_cause.ascii_tree.each_line do |backtrace_line| $stderr.puts " #{backtrace_line}" end else $stderr.puts error. end $stderr.puts end |
#run(argv = ARGV) ⇒ Integer
Runs the command.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/command_mapper/gen/cli.rb', line 85 def run(argv=ARGV) argv = begin @option_parser.parse(argv) rescue OptionParser::ParseError => error print_error(error.) return -1 end if argv.empty? print_error "expects a COMMAND_NAME" return -1 end begin @command = Command.new(argv.first) @parsers.each do |parser| parse_command = ->(command) { parser.run(command) do |line,parse_error| print_parser_error(command,line,parse_error) end command.subcommands.each_value do |subcommand| parse_command.call(subcommand) end } parse_command.call(@command) end rescue Error => error print_error(error.) return -1 end if (@command..empty? && @command.arguments.empty? && @command.subcommands.empty?) print_error "no options or arguments detected" return -2 end if @output then @command.save(@output) else puts command.to_ruby end return 0 rescue => error print_backtrace(error) return -1 end |