Class: Bovem::Application
Overview
This is the main class for a Bovem application.
Basically is the same of a command, but it adds support for application version.
Constant Summary collapse
- LOCALE_ROOT =
The location of the locales translation files.
::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/").freeze
Instance Attribute Summary collapse
-
#console ⇒ Bovem::Console
A console helper.
-
#output_commands ⇒ Boolean
If to show the output of the commands run via #run.
-
#shell ⇒ Bovem::Shell
A shell helper.
-
#show_commands ⇒ Boolean
If to show command lines run via #run.
-
#skip_commands ⇒ Boolean
If to skip commands run via #run.
-
#version(value = nil) ⇒ String|nil
Reads and optionally sets the version of this application.
Attributes inherited from Command
#action, #after, #application, #arguments, #banner, #before, #commands, #description, #i18n, #name, #options, #parent, #synopsis
Attributes included from CommandMethods::Children
Class Method Summary collapse
-
.create(options = {}, &block) ⇒ Application
Initializes a new Bovem application.
-
.create_application(run, args, options, &block) ⇒ Application
Create the application.
-
.setup_application_option(options) ⇒ Array
Setup options for application creation.
Instance Method Summary collapse
-
#command_help(command) ⇒ Object
Shows a help about a command.
-
#executable_name ⇒ String
The name of the current executable.
-
#execute(args = nil) ⇒ Object
Executes this application.
-
#help_option ⇒ Object
Adds a help command and a help option to this application.
-
#initialize(options = {}, &block) ⇒ Application
constructor
Creates a new application.
-
#run(command, message = nil, show_exit = true, fatal = true) ⇒ Hash
Runs a command into the shell.
Methods inherited from Command
#application?, #banner?, #description?, #full_name, #setup_with
Methods included from CommandMethods::Children
#argument, #arguments, #clear_commands, #clear_options, #command, #commands?, #get_options, #option, #options?
Methods included from CommandMethods::Help
Constructor Details
#initialize(options = {}, &block) ⇒ Application
Creates a new application.
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bovem/application.rb', line 90 def initialize( = {}, &block) super(, &block) @shell = Bovem::Shell.instance @console = @shell.console @skip_commands = false @show_commands = false @output_commands = false help_option end |
Instance Attribute Details
#console ⇒ Bovem::Console
Returns A console helper.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 79 80 81 82 83 84 85 86 87 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/bovem/application.rb', line 24 class Application < Bovem::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # The location of the locales translation files. LOCALE_ROOT = ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/").freeze # 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. def self.create( = {}, &block) unless block_given? raise Bovem::Errors::Error.new( Bovem::Application, :missing_block, Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).missing_app_block ) end run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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. def self.setup_application_option() = { name: Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).default_application_name, parent: nil, application: nil } = .merge(.ensure_hash) run = .delete(:run) [(!run.nil? ? run : true).to_boolean, .delete(:__args__), ] 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. def self.create_application(run, args, , &block) application = new(, &block) application.execute(args) if application && run application end # Creates a new application. # # @param options [Hash] The settings to initialize the application with. def initialize( = {}, &block) super(, &block) @shell = Bovem::Shell.instance @console = @shell.console @skip_commands = false @show_commands = false @output_commands = false help_option end # Reads and optionally sets the version of this application. # # @param value [String|nil] The new version of this application. # @return [String|nil] The version of this application. def version(value = nil) @version = value.ensure_string unless value.nil? @version end # Executes this application. # # @param args [Array] The command line to pass to this application. Defaults to `ARGV`. def execute(args = nil) super(args || ARGV) end # Adds a help command and a help option to this application. 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.) { |application, _| application.show_help } end # The name of the current executable. # # @return [String] The name of the current executable. def executable_name $PROGRAM_NAME end # Shows a help about a command. # # @param command [Command] The command to show help for. 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 # 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. def run(command, = nil, show_exit = true, fatal = true) @shell.run(command, , !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end private # :nodoc: def fetch_commands_for_help(command) command.arguments.map { |c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
#output_commands ⇒ Boolean
Returns If to show the output of the commands run via #run.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 79 80 81 82 83 84 85 86 87 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/bovem/application.rb', line 24 class Application < Bovem::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # The location of the locales translation files. LOCALE_ROOT = ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/").freeze # 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. def self.create( = {}, &block) unless block_given? raise Bovem::Errors::Error.new( Bovem::Application, :missing_block, Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).missing_app_block ) end run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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. def self.setup_application_option() = { name: Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).default_application_name, parent: nil, application: nil } = .merge(.ensure_hash) run = .delete(:run) [(!run.nil? ? run : true).to_boolean, .delete(:__args__), ] 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. def self.create_application(run, args, , &block) application = new(, &block) application.execute(args) if application && run application end # Creates a new application. # # @param options [Hash] The settings to initialize the application with. def initialize( = {}, &block) super(, &block) @shell = Bovem::Shell.instance @console = @shell.console @skip_commands = false @show_commands = false @output_commands = false help_option end # Reads and optionally sets the version of this application. # # @param value [String|nil] The new version of this application. # @return [String|nil] The version of this application. def version(value = nil) @version = value.ensure_string unless value.nil? @version end # Executes this application. # # @param args [Array] The command line to pass to this application. Defaults to `ARGV`. def execute(args = nil) super(args || ARGV) end # Adds a help command and a help option to this application. 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.) { |application, _| application.show_help } end # The name of the current executable. # # @return [String] The name of the current executable. def executable_name $PROGRAM_NAME end # Shows a help about a command. # # @param command [Command] The command to show help for. 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 # 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. def run(command, = nil, show_exit = true, fatal = true) @shell.run(command, , !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end private # :nodoc: def fetch_commands_for_help(command) command.arguments.map { |c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
#shell ⇒ Bovem::Shell
Returns A shell helper.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 79 80 81 82 83 84 85 86 87 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/bovem/application.rb', line 24 class Application < Bovem::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # The location of the locales translation files. LOCALE_ROOT = ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/").freeze # 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. def self.create( = {}, &block) unless block_given? raise Bovem::Errors::Error.new( Bovem::Application, :missing_block, Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).missing_app_block ) end run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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. def self.setup_application_option() = { name: Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).default_application_name, parent: nil, application: nil } = .merge(.ensure_hash) run = .delete(:run) [(!run.nil? ? run : true).to_boolean, .delete(:__args__), ] 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. def self.create_application(run, args, , &block) application = new(, &block) application.execute(args) if application && run application end # Creates a new application. # # @param options [Hash] The settings to initialize the application with. def initialize( = {}, &block) super(, &block) @shell = Bovem::Shell.instance @console = @shell.console @skip_commands = false @show_commands = false @output_commands = false help_option end # Reads and optionally sets the version of this application. # # @param value [String|nil] The new version of this application. # @return [String|nil] The version of this application. def version(value = nil) @version = value.ensure_string unless value.nil? @version end # Executes this application. # # @param args [Array] The command line to pass to this application. Defaults to `ARGV`. def execute(args = nil) super(args || ARGV) end # Adds a help command and a help option to this application. 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.) { |application, _| application.show_help } end # The name of the current executable. # # @return [String] The name of the current executable. def executable_name $PROGRAM_NAME end # Shows a help about a command. # # @param command [Command] The command to show help for. 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 # 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. def run(command, = nil, show_exit = true, fatal = true) @shell.run(command, , !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end private # :nodoc: def fetch_commands_for_help(command) command.arguments.map { |c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
#show_commands ⇒ Boolean
Returns If to show command lines run via #run.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 79 80 81 82 83 84 85 86 87 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/bovem/application.rb', line 24 class Application < Bovem::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # The location of the locales translation files. LOCALE_ROOT = ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/").freeze # 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. def self.create( = {}, &block) unless block_given? raise Bovem::Errors::Error.new( Bovem::Application, :missing_block, Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).missing_app_block ) end run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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. def self.setup_application_option() = { name: Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).default_application_name, parent: nil, application: nil } = .merge(.ensure_hash) run = .delete(:run) [(!run.nil? ? run : true).to_boolean, .delete(:__args__), ] 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. def self.create_application(run, args, , &block) application = new(, &block) application.execute(args) if application && run application end # Creates a new application. # # @param options [Hash] The settings to initialize the application with. def initialize( = {}, &block) super(, &block) @shell = Bovem::Shell.instance @console = @shell.console @skip_commands = false @show_commands = false @output_commands = false help_option end # Reads and optionally sets the version of this application. # # @param value [String|nil] The new version of this application. # @return [String|nil] The version of this application. def version(value = nil) @version = value.ensure_string unless value.nil? @version end # Executes this application. # # @param args [Array] The command line to pass to this application. Defaults to `ARGV`. def execute(args = nil) super(args || ARGV) end # Adds a help command and a help option to this application. 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.) { |application, _| application.show_help } end # The name of the current executable. # # @return [String] The name of the current executable. def executable_name $PROGRAM_NAME end # Shows a help about a command. # # @param command [Command] The command to show help for. 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 # 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. def run(command, = nil, show_exit = true, fatal = true) @shell.run(command, , !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end private # :nodoc: def fetch_commands_for_help(command) command.arguments.map { |c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
#skip_commands ⇒ Boolean
Returns If to skip commands run via #run.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 79 80 81 82 83 84 85 86 87 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/bovem/application.rb', line 24 class Application < Bovem::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # The location of the locales translation files. LOCALE_ROOT = ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/").freeze # 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. def self.create( = {}, &block) unless block_given? raise Bovem::Errors::Error.new( Bovem::Application, :missing_block, Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).missing_app_block ) end run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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. def self.setup_application_option() = { name: Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).default_application_name, parent: nil, application: nil } = .merge(.ensure_hash) run = .delete(:run) [(!run.nil? ? run : true).to_boolean, .delete(:__args__), ] 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. def self.create_application(run, args, , &block) application = new(, &block) application.execute(args) if application && run application end # Creates a new application. # # @param options [Hash] The settings to initialize the application with. def initialize( = {}, &block) super(, &block) @shell = Bovem::Shell.instance @console = @shell.console @skip_commands = false @show_commands = false @output_commands = false help_option end # Reads and optionally sets the version of this application. # # @param value [String|nil] The new version of this application. # @return [String|nil] The version of this application. def version(value = nil) @version = value.ensure_string unless value.nil? @version end # Executes this application. # # @param args [Array] The command line to pass to this application. Defaults to `ARGV`. def execute(args = nil) super(args || ARGV) end # Adds a help command and a help option to this application. 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.) { |application, _| application.show_help } end # The name of the current executable. # # @return [String] The name of the current executable. def executable_name $PROGRAM_NAME end # Shows a help about a command. # # @param command [Command] The command to show help for. 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 # 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. def run(command, = nil, show_exit = true, fatal = true) @shell.run(command, , !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end private # :nodoc: def fetch_commands_for_help(command) command.arguments.map { |c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
#version(value = nil) ⇒ String|nil
Reads and optionally sets the version of this application.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 79 80 81 82 83 84 85 86 87 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/bovem/application.rb', line 24 class Application < Bovem::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # The location of the locales translation files. LOCALE_ROOT = ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/").freeze # 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. def self.create( = {}, &block) unless block_given? raise Bovem::Errors::Error.new( Bovem::Application, :missing_block, Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).missing_app_block ) end run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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. def self.setup_application_option() = { name: Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).default_application_name, parent: nil, application: nil } = .merge(.ensure_hash) run = .delete(:run) [(!run.nil? ? run : true).to_boolean, .delete(:__args__), ] 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. def self.create_application(run, args, , &block) application = new(, &block) application.execute(args) if application && run application end # Creates a new application. # # @param options [Hash] The settings to initialize the application with. def initialize( = {}, &block) super(, &block) @shell = Bovem::Shell.instance @console = @shell.console @skip_commands = false @show_commands = false @output_commands = false help_option end # Reads and optionally sets the version of this application. # # @param value [String|nil] The new version of this application. # @return [String|nil] The version of this application. def version(value = nil) @version = value.ensure_string unless value.nil? @version end # Executes this application. # # @param args [Array] The command line to pass to this application. Defaults to `ARGV`. def execute(args = nil) super(args || ARGV) end # Adds a help command and a help option to this application. 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.) { |application, _| application.show_help } end # The name of the current executable. # # @return [String] The name of the current executable. def executable_name $PROGRAM_NAME end # Shows a help about a command. # # @param command [Command] The command to show help for. 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 # 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. def run(command, = nil, show_exit = true, fatal = true) @shell.run(command, , !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end private # :nodoc: def fetch_commands_for_help(command) command.arguments.map { |c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
Class Method Details
.create(options = {}, &block) ⇒ Application
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
.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bovem/application.rb', line 43 def self.create( = {}, &block) unless block_given? raise Bovem::Errors::Error.new( Bovem::Application, :missing_block, Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).missing_app_block ) end run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end end |
.create_application(run, args, options, &block) ⇒ Application
Create the application.
81 82 83 84 85 |
# File 'lib/bovem/application.rb', line 81 def self.create_application(run, args, , &block) application = new(, &block) application.execute(args) if application && run application end |
.setup_application_option(options) ⇒ Array
Setup options for application creation.
65 66 67 68 69 70 71 72 73 |
# File 'lib/bovem/application.rb', line 65 def self.setup_application_option() = { name: Bovem::I18n.new([:locale], root: "bovem.application", path: LOCALE_ROOT).default_application_name, parent: nil, application: nil } = .merge(.ensure_hash) run = .delete(:run) [(!run.nil? ? run : true).to_boolean, .delete(:__args__), ] end |
Instance Method Details
#command_help(command) ⇒ Object
Shows a help about a command.
137 138 139 140 141 142 143 144 145 146 |
# 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 |
#executable_name ⇒ String
The name of the current executable.
130 131 132 |
# File 'lib/bovem/application.rb', line 130 def executable_name $PROGRAM_NAME end |
#execute(args = nil) ⇒ Object
Executes this application.
114 115 116 |
# File 'lib/bovem/application.rb', line 114 def execute(args = nil) super(args || ARGV) end |
#help_option ⇒ Object
Adds a help command and a help option to this application.
119 120 121 122 123 124 125 |
# 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.) { |application, _| application.show_help } end |
#run(command, message = nil, show_exit = true, fatal = true) ⇒ Hash
Runs a command into the shell.
155 156 157 |
# File 'lib/bovem/application.rb', line 155 def run(command, = nil, show_exit = true, fatal = true) @shell.run(command, , !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end |