Class: CommandMapper::Gen::CLI

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Initializes the command.



48
49
50
51
52
53
54
55
# File 'lib/command_mapper/gen/cli.rb', line 48

def initialize
  @output  = nil
  @parsers = PARSERS.values
  @debug   = false
  @command = nil

  @option_parser = option_parser
end

Instance Attribute Details

#commandCommand (readonly)

The parsed command.

Returns:



38
39
40
# File 'lib/command_mapper/gen/cli.rb', line 38

def command
  @command
end

#debugBoolean? (readonly)

Specifies whether debug output should be printed.

Returns:

  • (Boolean, nil)


33
34
35
# File 'lib/command_mapper/gen/cli.rb', line 33

def debug
  @debug
end

#option_parserOptionParser (readonly)

The option parser.

Returns:

  • (OptionParser)


43
44
45
# File 'lib/command_mapper/gen/cli.rb', line 43

def option_parser
  @option_parser
end

#outputFile? (readonly)

The output file or nil for stdout.

Returns:

  • (File, nil)


23
24
25
# File 'lib/command_mapper/gen/cli.rb', line 23

def output
  @output
end

#parsersArray<Parsers::Help, Parsers::Man> (readonly)

The parsers to run.

Returns:



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.

Parameters:

  • argv (Array<String>) (defaults to: ARGV)

    Command-line arguments.

Returns:

  • (Integer)

    The exit status of 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

Prints a backtrace to stderr.

Parameters:

  • exception (Exception)

    The exception.



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.full_message}"
  $stderr.puts "```"
end

Prints an error message to stderr.

Parameters:

  • error (String)

    The error message.



183
184
185
# File 'lib/command_mapper/gen/cli.rb', line 183

def print_error(error)
  $stderr.puts "#{PROGRAM_NAME}: #{error}"
end

Prints a parsing error to stderr.

Parameters:

  • command (Command)

    The command that was being populated.

  • string (String)

    The text that could not be parsed.

  • error (Parslet::ParseError)

    The parsing error.



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.message
  end

  $stderr.puts
end

#run(argv = ARGV) ⇒ Integer

Runs the command.

Parameters:

  • argv (Array<String>) (defaults to: ARGV)

    Command-line arguments.

Returns:

  • (Integer)

    The exit status of 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.message)
           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.message)
    return -1
  end

  if (@command.options.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