lib/shepherd/cli.rb in shepherd-0.1.2 vs lib/shepherd/cli.rb in shepherd-0.1.3
- old
+ new
@@ -1,19 +1,41 @@
require "trollop"
module Shepherd
# 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
+ #
class Cli
# Kinda self explanatory
class UnknownCommand < RuntimeError; end
# A command which is about to be run
attr_accessor :command
# Require *all* command files
+ # TODO: Is it possible to make it use autoload? It'd be cool! :)
Dir[File.join(File.dirname(__FILE__), "commands", "*.rb")].each do |all_command_files|
require all_command_files
end
# Handle the commands list
@@ -41,21 +63,21 @@
desc = if eval "Command::#{cmd}.new.respond_to? 'desc'"
# If there is - execute it
eval "Command::#{cmd}.new.desc"
else
# If there is not
- "~ no description provided ~"
+ "---"
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
+ end # commands_list:Method
# Check if command really exists
#
# @return [Boolean] whether the command exists or not
def command_exists?
@@ -67,19 +89,19 @@
# Nice, cool 'n' AWESOME --options parsing with Trollop[http://trollop.rubyforge.org/]!
#
# @return [Hash] array full of options
$opts = Trollop::options do
- version "Shepherd be t'e version #{Version::STRING}"
+ version "shepherd version #{Version::STRING}"
banner <<-EOB
usage: shep [options] <command>
commands are:
#{Cli.new.commands_list}
- shep <command> --help for more info about specified command
+ shep <command> --help/-h for more info about specified command
options are:
EOB
opt :version, "show version and exit", :short => '-v'
@@ -94,15 +116,15 @@
begin
execute @command
exit 0
rescue UnknownCommand => e
puts e.message
- exit 1
+ exit 2
rescue Db::DatabaseNotFound => e
puts e.message
- exit 1
+ exit 3
rescue Interrupt
- puts "\n\n~> interrupted"
+ puts "\n\n!# interrupted"
exit 1
end
end # run!:Method
# Executes a command