Class: Shepherd::Cli
- Inherits:
-
Object
- Object
- Shepherd::Cli
- Defined in:
- lib/shepherd/cli.rb
Overview
Command Line Interface class
Usage
module Shepherd
Cli.new.run!
end
or
Shepherd::Cli.new.run!
Exit statuses
-
0 Everything went just fine :)
-
1 User said ^C :]
-
2 User wanted a UnknownCommand
-
3 The database file was not found
-
4 User wanted to init another sheep/project with the same name and/or path
-
5 User wanted to init a project in a path that doesn’t exist
-
6 User wanted to see a sheep that was not inited
Defined Under Namespace
Classes: UnknownCommand
Constant Summary collapse
Instance Attribute Summary collapse
-
#command ⇒ Object
A command which is about to be run.
Instance Method Summary collapse
-
#command_exists? ⇒ Boolean
Check if command really exists.
-
#commands_list ⇒ String
Get a list of available commands to be printed.
-
#execute(command) ⇒ Object
Executes a command.
-
#run! ⇒ Object
Rruns t’e Cli!.
Instance Attribute Details
#command ⇒ Object
A command which is about to be run
33 34 35 |
# File 'lib/shepherd/cli.rb', line 33 def command @command end |
Instance Method Details
#command_exists? ⇒ Boolean
Check if command really exists
83 84 85 |
# File 'lib/shepherd/cli.rb', line 83 def command_exists? COMMANDS.include? @command end |
#commands_list ⇒ String
Get a list of available commands to be printed. (Almost) every line is separated by new line mark - n
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/shepherd/cli.rb', line 49 def commands_list out = "" # If there are no commands set if COMMANDS.empty? out << " ooops! commands are not here!" else # Get the longest command's name, so we can output it nice 'n' clean # This '+ int' at the end is a distance (in spaces) from the longest # command to descriptions longest = COMMANDS.max_by(&:size).size + 8 COMMANDS.each do |cmd| # Calc, calc. spaces = longest - cmd.size # Check if there is a 'desc' method desc = if eval "Command::#{cmd}.new.respond_to? 'desc'" # If there is - execute it eval "Command::#{cmd}.new.desc" else # If there is not "---" end out << " " << cmd.downcase.to_s << " " * spaces << desc # If this command is the last one, don't make a new line unless cmd == COMMANDS.last out << "\n" end end end out end |
#execute(command) ⇒ Object
Executes a command
135 136 137 138 139 140 141 |
# File 'lib/shepherd/cli.rb', line 135 def execute command if command_exists? eval "Command::#{command}.new.init" else raise UnknownCommand, "Error: unknown command '#{command.downcase.to_s}'.\nTry --help for help." end end |
#run! ⇒ Object
Rruns t’e Cli!
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 |
# File 'lib/shepherd/cli.rb', line 88 def run! # Nice, cool 'n' AWESOME --options parsing with Trollop[http://trollop.rubyforge.org/]! # # @return [Hash] array full of options $opts = Trollop:: do version "shepherd version #{Version::STRING}" <<-EOB usage: shep [options] <command> commands are: #{Cli.new.commands_list} shep <command> --help/-h for more info about specified command options are: EOB opt :version, "show version and exit", :short => '-v' opt :help, "show me and exit", :short => '-h' stop_on COMMANDS.collect { |e| e.downcase.to_s } end # Get the command @command = ARGV.shift.capitalize.to_sym begin execute @command exit 0 rescue UnknownCommand => e puts e. exit 2 rescue Db::DatabaseNotFound => e puts e. exit 3 rescue Interrupt puts "\n\n!# interrupted" exit 1 end end |