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.
Instance Attribute Summary (collapse)
-
- (Bovem::Console) console
A console helper.
-
- (Boolean) output_commands
If to show the output of the commands run via #run.
-
- (Bovem::Shell) shell
A shell helper.
-
- (Boolean) show_commands
If to show command lines run via #run.
-
- (Boolean) skip_commands
If to skip commands run via #run.
-
- (String|nil) version(value = 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)
-
+ (Application) create(options = {}, &block)
Initializes a new Bovem application.
Instance Method Summary (collapse)
-
- (Object) command_help(command)
Shows a help about a command.
-
- (String) executable_name
The name of the current executable.
-
- (Object) execute(args = nil)
Executes this application.
-
- (Object) help_option
Adds a help command and a help option to this application.
-
- (Application) initialize(options = {}, &block)
constructor
Creates a new application.
-
- (Hash) run(command, message = nil, show_exit = true, fatal = true)
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
- (Application) initialize(options = {}, &block)
Creates a new application.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bovem/application.rb', line 55 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
- (Bovem::Console) 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 |
# 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 # 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) raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::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 = Bovem::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: Bovem::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.map {|c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
- (Boolean) output_commands
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 |
# 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 # 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) raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::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 = Bovem::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: Bovem::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.map {|c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
- (Bovem::Shell) 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 |
# 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 # 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) raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::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 = Bovem::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: Bovem::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.map {|c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
- (Boolean) show_commands
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 |
# 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 # 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) raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::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 = Bovem::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: Bovem::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.map {|c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
- (Boolean) skip_commands
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 |
# 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 # 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) raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::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 = Bovem::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: Bovem::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.map {|c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
- (String|nil) version(value = 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 |
# 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 # 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) raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::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 = Bovem::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: Bovem::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.map {|c| c.split(":") }.flatten.map(&:strip).select(&:present?) end end |
Class Method Details
+ (Application) create(options = {}, &block)
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
.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bovem/application.rb', line 40 def self.create( = {}, &block) raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::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
- (Object) command_help(command)
Shows a help about a command.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/bovem/application.rb', line 102 def command_help(command) fetch_commands_for_help(command).each do |arg| # Find the command across next_command = Bovem::Parser.find_command(arg, command, []) if next_command then command = command.commands[next_command[:name]] else break end end command.show_help end |
- (String) executable_name
The name of the current executable.
95 96 97 |
# File 'lib/bovem/application.rb', line 95 def executable_name $0 end |
- (Object) execute(args = nil)
Executes this application.
79 80 81 |
# File 'lib/bovem/application.rb', line 79 def execute(args = nil) super(args || ARGV) end |
- (Object) help_option
Adds a help command and a help option to this application.
84 85 86 87 88 89 90 |
# File 'lib/bovem/application.rb', line 84 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 |
- (Hash) run(command, message = nil, show_exit = true, fatal = true)
Runs a command into the shell.
124 125 126 |
# File 'lib/bovem/application.rb', line 124 def run(command, = nil, show_exit = true, fatal = true) @shell.run(command, , !@skip_commands, show_exit, @output_commands, @show_commands, fatal) end |