Class: Testjour::CLI::Parser
- Inherits:
-
Object
- Object
- Testjour::CLI::Parser
- Defined in:
- lib/testjour/cli.rb
Class Attribute Summary collapse
-
.commands ⇒ Object
Returns the value of attribute commands.
Class Method Summary collapse
Instance Method Summary collapse
- #command_klass ⇒ Object
- #command_listing ⇒ Object
- #command_name ⇒ Object
- #execute ⇒ Object
- #exit_with_usage ⇒ Object
- #usage ⇒ Object
Class Attribute Details
.commands ⇒ Object
Returns the value of attribute commands.
26 27 28 |
# File 'lib/testjour/cli.rb', line 26 def commands @commands end |
Class Method Details
.register_command(klass) ⇒ Object
28 29 30 |
# File 'lib/testjour/cli.rb', line 28 def register_command(klass) @commands << klass end |
Instance Method Details
#command_klass ⇒ Object
47 48 49 50 51 |
# File 'lib/testjour/cli.rb', line 47 def command_klass self.class.commands.detect do |command_klass| command_klass.command == command_name end end |
#command_listing ⇒ Object
74 75 76 77 78 |
# File 'lib/testjour/cli.rb', line 74 def command_listing self.class.commands.sort_by { |c| c.command }.map do |command_klass| " " + command_klass.command end end |
#command_name ⇒ Object
53 54 55 |
# File 'lib/testjour/cli.rb', line 53 def command_name ARGV.first end |
#execute ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/testjour/cli.rb', line 35 def execute raise NoCommandGiven if ARGV.empty? raise UnknownCommand.new(command_name) unless command_klass args = ARGV.dup args.shift # Remove subcommand name command_klass.new(self, args).run rescue NoCommandGiven, UnknownCommand exit_with_usage end |
#exit_with_usage ⇒ Object
57 58 59 60 61 |
# File 'lib/testjour/cli.rb', line 57 def exit_with_usage $stderr.puts "ERROR: #{$!.message}" $stderr.puts usage exit 1 end |
#usage ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/testjour/cli.rb', line 63 def usage = [] << "usage: testjour <SUBCOMMAND> [OPTIONS] [ARGS...]" << "Type 'testjour help <SUBCOMMAND>' for help on a specific subcommand." << "Type 'testjour version' to get this program's version." << "" << "Available subcommands are:" += command_listing .map { |line| line.chomp }.join("\n") end |