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



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

def command_klass
  self.class.commands.detect do |command_klass|
    command_klass.command == command_name
  end
end

#command_nameObject



57
58
59
# File 'lib/testjour/cli.rb', line 57

def command_name
  ARGV.first
end

#executeObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/testjour/cli.rb', line 35

def execute
  begin
    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
    $stderr.puts "ERROR: #{$!.message}"
    $stderr.puts usage
    exit 1
  end
end

#usageObject



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

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:"
  
  self.class.commands.sort_by { |c| c.command }.each do |command_klass|
    message << "  " + command_klass.command
  end
  
  message.map { |line| line.chomp }.join("\n")
end