Class: Mamertes::Application
- Defined in:
- lib/mamertes/application.rb
Overview
This is the main class for a Mamertes application.
Basically is the same of a command, but it adds support for application version.
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, #name, #options, #parent, #synopsis
Attributes included from CommandMethods::Children
Class Method Summary collapse
-
.create(options = {}, &block) ⇒ Application
Initializes a new Mamertes application.
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
#full_name, #has_banner?, #has_description?, #is_application?, #setup_with
Methods included from CommandMethods::Children
#argument, #arguments, #clear_commands, #clear_options, #command, #get_options, #has_commands?, #has_options?, #option
Methods included from CommandMethods::Help
Constructor Details
#initialize(options = {}, &block) ⇒ Application
Creates a new application.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/mamertes/application.rb', line 103 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.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/mamertes/application.rb', line 72 class Application < ::Mamertes::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # Initializes a new Mamertes 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) raise Mamertes::Error.new(Mamertes::Application, :missing_block, ::Mamertes::Localizer.localize_on_locale([:locale], :missing_app_block)) if !block_given? run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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 if !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 $0 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 = ::Mamertes::Parser.find_command(arg, command, []) if next_command then command = command.commands[next_command[:name]] else break end 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 # 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: ::Mamertes::Localizer.localize_on_locale([:locale], :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 # Fetch a command list for showing help. # # @param command [Command] The command to show help for. def fetch_commands_for_help(command) command.arguments.collect {|c| c.split(":") }.flatten.collect(&:strip).select(&:present?) end end |
#output_commands ⇒ Boolean
Returns If to show the output of the commands run via #run.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/mamertes/application.rb', line 72 class Application < ::Mamertes::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # Initializes a new Mamertes 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) raise Mamertes::Error.new(Mamertes::Application, :missing_block, ::Mamertes::Localizer.localize_on_locale([:locale], :missing_app_block)) if !block_given? run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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 if !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 $0 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 = ::Mamertes::Parser.find_command(arg, command, []) if next_command then command = command.commands[next_command[:name]] else break end 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 # 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: ::Mamertes::Localizer.localize_on_locale([:locale], :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 # Fetch a command list for showing help. # # @param command [Command] The command to show help for. def fetch_commands_for_help(command) command.arguments.collect {|c| c.split(":") }.flatten.collect(&:strip).select(&:present?) end end |
#shell ⇒ ::Bovem::Shell
Returns A shell helper.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/mamertes/application.rb', line 72 class Application < ::Mamertes::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # Initializes a new Mamertes 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) raise Mamertes::Error.new(Mamertes::Application, :missing_block, ::Mamertes::Localizer.localize_on_locale([:locale], :missing_app_block)) if !block_given? run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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 if !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 $0 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 = ::Mamertes::Parser.find_command(arg, command, []) if next_command then command = command.commands[next_command[:name]] else break end 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 # 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: ::Mamertes::Localizer.localize_on_locale([:locale], :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 # Fetch a command list for showing help. # # @param command [Command] The command to show help for. def fetch_commands_for_help(command) command.arguments.collect {|c| c.split(":") }.flatten.collect(&:strip).select(&:present?) end end |
#show_commands ⇒ Boolean
Returns If to show command lines run via #run.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/mamertes/application.rb', line 72 class Application < ::Mamertes::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # Initializes a new Mamertes 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) raise Mamertes::Error.new(Mamertes::Application, :missing_block, ::Mamertes::Localizer.localize_on_locale([:locale], :missing_app_block)) if !block_given? run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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 if !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 $0 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 = ::Mamertes::Parser.find_command(arg, command, []) if next_command then command = command.commands[next_command[:name]] else break end 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 # 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: ::Mamertes::Localizer.localize_on_locale([:locale], :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 # Fetch a command list for showing help. # # @param command [Command] The command to show help for. def fetch_commands_for_help(command) command.arguments.collect {|c| c.split(":") }.flatten.collect(&:strip).select(&:present?) end end |
#skip_commands ⇒ Boolean
Returns If to skip commands run via #run.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/mamertes/application.rb', line 72 class Application < ::Mamertes::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # Initializes a new Mamertes 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) raise Mamertes::Error.new(Mamertes::Application, :missing_block, ::Mamertes::Localizer.localize_on_locale([:locale], :missing_app_block)) if !block_given? run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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 if !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 $0 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 = ::Mamertes::Parser.find_command(arg, command, []) if next_command then command = command.commands[next_command[:name]] else break end 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 # 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: ::Mamertes::Localizer.localize_on_locale([:locale], :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 # Fetch a command list for showing help. # # @param command [Command] The command to show help for. def fetch_commands_for_help(command) command.arguments.collect {|c| c.split(":") }.flatten.collect(&:strip).select(&:present?) end end |
#version(value = nil) ⇒ String|nil
Reads and optionally sets the version of this application.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/mamertes/application.rb', line 72 class Application < ::Mamertes::Command attr_accessor :version attr_accessor :shell attr_accessor :console attr_accessor :skip_commands attr_accessor :show_commands attr_accessor :output_commands # Initializes a new Mamertes 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) raise Mamertes::Error.new(Mamertes::Application, :missing_block, ::Mamertes::Localizer.localize_on_locale([:locale], :missing_app_block)) if !block_given? run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end 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 if !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 $0 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 = ::Mamertes::Parser.find_command(arg, command, []) if next_command then command = command.commands[next_command[:name]] else break end 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 # 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: ::Mamertes::Localizer.localize_on_locale([:locale], :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 # Fetch a command list for showing help. # # @param command [Command] The command to show help for. def fetch_commands_for_help(command) command.arguments.collect {|c| c.split(":") }.flatten.collect(&:strip).select(&:present?) end end |
Class Method Details
.create(options = {}, &block) ⇒ Application
Initializes a new Mamertes application.
In options, you can override the command line arguments with :__args__
, and you can skip execution by specifying run: false
.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mamertes/application.rb', line 88 def self.create( = {}, &block) raise Mamertes::Error.new(Mamertes::Application, :missing_block, ::Mamertes::Localizer.localize_on_locale([:locale], :missing_app_block)) if !block_given? run, args, = setup_application_option() begin create_application(run, args, , &block) rescue => e Kernel.puts(e.to_s) Kernel.exit(1) end end |
Instance Method Details
#command_help(command) ⇒ Object
Shows a help about a command.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mamertes/application.rb', line 150 def command_help(command) fetch_commands_for_help(command).each do |arg| # Find the command across next_command = ::Mamertes::Parser.find_command(arg, command, []) if next_command then command = command.commands[next_command[:name]] else break end end command.show_help end |
#executable_name ⇒ String
The name of the current executable.
143 144 145 |
# File 'lib/mamertes/application.rb', line 143 def executable_name $0 end |
#execute(args = nil) ⇒ Object
Executes this application.
127 128 129 |
# File 'lib/mamertes/application.rb', line 127 def execute(args = nil) super(args || ARGV) end |
#help_option ⇒ Object
Adds a help command and a help option to this application.
132 133 134 135 136 137 138 |
# File 'lib/mamertes/application.rb', line 132 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.
172 173 174 |
# File 'lib/mamertes/application.rb', line 172 def run(command, = nil, show_exit = true, fatal = true) @shell.run(command, , !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end |