class Bovem::Application
This is the main class for a Bovem
application.
Basically is the same of a command, but it adds support for application version.
@attribute version
@return [String] The version of the application.
@attribute shell
@return [Bovem::Shell] A shell helper.
@attribute console
@return [Bovem::Console] A console helper.
@attribute skip_commands
@return [Boolean] If to skip commands run via {#run}.
@attribute show_commands
@return [Boolean] If to show command lines run via {#run}.
@attribute output_commands
@return [Boolean] If to show the output of the commands run via {#run}.
Constants
- LOCALE_ROOT
The location of the locales translation files.
Attributes
Public Class Methods
Initializes a new Bovem
application.
In options, you can override the command line arguments with `:__args__`, and you can skip execution by specifying `run: false`.
@see Command#setup_with
@param options [Hash] The settings to initialize the application with. @return [Application] The created application.
# File lib/bovem/application.rb, line 43 def self.create(options = {}, &block) unless block_given? raise Bovem::Errors::Error.new( Bovem::Application, :missing_block, Bovem::I18n.new(options[:locale], root: "bovem.application", path: LOCALE_ROOT).missing_app_block ) end run, args, options = setup_application_option(options) begin create_application(run, args, options, &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end end
Create the application.
@param run [Boolean] If to run the application. @param args [Hash] The arguments to use for running. @param options [Hash] The options of the application. @return [Application] The new application.
# File lib/bovem/application.rb, line 81 def self.create_application(run, args, options, &block) application = new(options, &block) application.execute(args) if application && run application end
Creates a new application.
@param options [Hash] The settings to initialize the application with.
Bovem::Command::new
# File lib/bovem/application.rb, line 90 def initialize(options = {}, &block) super(options, &block) @shell = Bovem::Shell.instance @console = @shell.console @skip_commands = false @show_commands = false @output_commands = false help_option end
Setup options for application creation.
@param options [Hash] The options to setups. @return [Array] If to run the application, the arguments and the specified options.
# File lib/bovem/application.rb, line 65 def self.setup_application_option(options) base_options = { name: Bovem::I18n.new(options[:locale], root: "bovem.application", path: LOCALE_ROOT).default_application_name, parent: nil, application: nil } options = base_options.merge(options.ensure_hash) run = options.delete(:run) [(!run.nil? ? run : true).to_boolean, options.delete(:__args__), options] end
Public Instance Methods
Shows a help about a command.
@param command [Command] The command to show help for.
# File lib/bovem/application.rb, line 137 def command_help(command) fetch_commands_for_help(command).each do |arg| # Find the command across next_command = Bovem::Parser.find_command(arg, command, args: []) break unless next_command command = command.commands[next_command[:name]] end command.show_help end
The name of the current executable.
@return [String] The name of the current executable.
# File lib/bovem/application.rb, line 130 def executable_name $PROGRAM_NAME end
Executes this application.
@param args [Array] The command line to pass to this application. Defaults to `ARGV`.
Bovem::Command#execute
# File lib/bovem/application.rb, line 114 def execute(args = nil) super(args || ARGV) end
Adds a help command and a help option to this application.
# File lib/bovem/application.rb, line 119 def help_option command(:help, description: i18n.help_command_description) do action { |command| application.command_help(command) } end option(:help, [i18n.help_option_short_form, i18n.help_option_long_form], help: i18n.help_message) { |application, _| application.show_help } end
Runs a command into the shell.
@param command [String] The string to run. @param message [String] A message to show before running. @param show_exit [Boolean] If show the exit status. @param fatal [Boolean] If quit in case of fatal errors. @return [Hash] An hash with `status` and `output` keys.
# File lib/bovem/application.rb, line 155 def run(command, message = nil, show_exit = true, fatal = true) @shell.run(command, message, !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end