Class: Mercury::CLI::Base
- Inherits:
-
Cri::Base
- Object
- Cri::Base
- Mercury::CLI::Base
- Defined in:
- lib/mercury/cli/base.rb
Overview
Details the global options applicable to all commands, and the code necessary to process those options. Each sub-command also needs to be registered with this class for it to work: strange things will happen if the sub-command is not set-up here!
When creating a new sub-command, the constructor for that sub-command needs to be added to the #initialize function of this class as well. Be sure to also add the path to the file where the sub-command is defined to the file lib/mercury/cli/commands.rb
, otherwise you will get warnings of unknown classes.
Instance Method Summary collapse
-
#global_option_definitions ⇒ Object
Returns the list of global option definitionss.
-
#handle_option(option) ⇒ Object
Process the global options, and set/change the application state from them.
-
#initialize ⇒ Base
constructor
Instantiates the sub-commands by creating a single reference to each known sub-command.
Constructor Details
#initialize ⇒ Base
This means that if your sub-command is not in this constructor it *will not* be found, and *will not* appear as a valid sub-command. If something is missing from the ‘help’ command, check this method!
Instantiates the sub-commands by creating a single reference to each known sub-command.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mercury/cli/base.rb', line 46 def initialize super('mercury') # Add help command self.help_command = Mercury::CLI::Commands::Help.new add_command(self.help_command) # Add other commands add_command(Mercury::CLI::Commands::Bootstrap.new) add_command(Mercury::CLI::Commands::Show.new) end |
Instance Method Details
#global_option_definitions ⇒ Object
Returns the list of global option definitionss.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mercury/cli/base.rb', line 59 def global_option_definitions [ { :long => 'help', :short => 'h', :argument => :forbidden, :desc => 'show this help message and quit' }, { :long => 'no-color', :short => 'C', :argument => :forbidden, :desc => 'disable color' }, { :long => 'version', :short => 'v', :argument => :forbidden, :desc => 'show version information and quit' }, { :long => 'verbose', :short => 'V', :argument => :forbidden, :desc => 'make mercury output more detailed' } ] end |
#handle_option(option) ⇒ Object
Process the global options, and set/change the application state from them
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mercury/cli/base.rb', line 81 def handle_option(option) # Handle version option if option == :version puts "Mercury Bootstrap Client #{Mercury::VERSION} (c) 2011 David Love." puts "Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) running on #{RUBY_PLATFORM}" exit 0 # Handle verbose option elsif option == :verbose Mercury::CLI::Logger.instance.level = :low # Handle no-color option elsif option == :'no-color' Mercury::CLI::Logger.instance.color = false # Handle help option elsif option == :help show_help exit 0 end end |