Class: Testjour::CLI::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/testjour/cli.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.commandsObject

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_klassObject



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_listingObject



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_nameObject



53
54
55
# File 'lib/testjour/cli.rb', line 53

def command_name
  ARGV.first
end

#executeObject



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_usageObject



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

#usageObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/testjour/cli.rb', line 63

def usage
  message = []
  message << "usage: testjour <SUBCOMMAND> [OPTIONS] [ARGS...]"
  message << "Type 'testjour help <SUBCOMMAND>' for help on a specific subcommand."
  message << "Type 'testjour version' to get this program's version."
  message << ""
  message << "Available subcommands are:"
  message += command_listing
  message.map { |line| line.chomp }.join("\n")
end