Class: Bovem::Application

Inherits:
Command
  • Object
show all
Defined in:
lib/bovem/application.rb

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

Attributes inherited from Command

#action, #after, #application, #arguments, #banner, #before, #commands, #description, #name, #options, #parent, #synopsis

Attributes included from CommandMethods::Children

#commands, #options

Class Method Summary collapse

Instance Method Summary collapse

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

#show_help

Constructor Details

#initialize(options = {}, &block) ⇒ Application

Creates a new application.

Parameters:

  • options (Hash) (defaults to: {})

    The settings to initialize the application with.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bovem/application.rb', line 55

def initialize(options = {}, &block)
  super(options, &block)

  @shell = Bovem::Shell.instance
  @console = @shell.console
  @skip_commands = false
  @show_commands = false
  @output_commands = false

  help_option
end

Instance Attribute Details

#consoleBovem::Console

Returns A console helper.

Returns:



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(options = {}, &block)
    raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::Localizer.localize_on_locale(options[:locale], :missing_app_block)) if !block_given?
    run, args, options = setup_application_option(options)

    begin
      create_application(run, args, options, &block)
    rescue => e
      Kernel.puts(e.to_s)
      Kernel.exit(1)
    end
  end

  # Creates a new application.
  #
  # @param options [Hash] The settings to initialize the application with.
  def initialize(options = {}, &block)
    super(options, &block)

    @shell = Bovem::Shell.instance
    @console = @shell.console
    @skip_commands = false
    @show_commands = false
    @output_commands = false

    help_option
  end

  # 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.help_message){ |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, message = nil, show_exit = true, fatal = true)
    @shell.run(command, message, !@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(options)
      options = {name: Bovem::Localizer.localize_on_locale(options[:locale], :default_application_name), parent: nil, application: nil}.merge(options.ensure_hash)
      run = options.delete(:run)
      [(!run.nil? ? run : true).to_boolean, options.delete(:__args__), options]
    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, options, &block)
      application = new(options, &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

#output_commandsBoolean

Returns If to show the output of the commands run via #run.

Returns:

  • (Boolean)

    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(options = {}, &block)
    raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::Localizer.localize_on_locale(options[:locale], :missing_app_block)) if !block_given?
    run, args, options = setup_application_option(options)

    begin
      create_application(run, args, options, &block)
    rescue => e
      Kernel.puts(e.to_s)
      Kernel.exit(1)
    end
  end

  # Creates a new application.
  #
  # @param options [Hash] The settings to initialize the application with.
  def initialize(options = {}, &block)
    super(options, &block)

    @shell = Bovem::Shell.instance
    @console = @shell.console
    @skip_commands = false
    @show_commands = false
    @output_commands = false

    help_option
  end

  # 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.help_message){ |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, message = nil, show_exit = true, fatal = true)
    @shell.run(command, message, !@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(options)
      options = {name: Bovem::Localizer.localize_on_locale(options[:locale], :default_application_name), parent: nil, application: nil}.merge(options.ensure_hash)
      run = options.delete(:run)
      [(!run.nil? ? run : true).to_boolean, options.delete(:__args__), options]
    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, options, &block)
      application = new(options, &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

#shellBovem::Shell

Returns A shell helper.

Returns:



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(options = {}, &block)
    raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::Localizer.localize_on_locale(options[:locale], :missing_app_block)) if !block_given?
    run, args, options = setup_application_option(options)

    begin
      create_application(run, args, options, &block)
    rescue => e
      Kernel.puts(e.to_s)
      Kernel.exit(1)
    end
  end

  # Creates a new application.
  #
  # @param options [Hash] The settings to initialize the application with.
  def initialize(options = {}, &block)
    super(options, &block)

    @shell = Bovem::Shell.instance
    @console = @shell.console
    @skip_commands = false
    @show_commands = false
    @output_commands = false

    help_option
  end

  # 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.help_message){ |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, message = nil, show_exit = true, fatal = true)
    @shell.run(command, message, !@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(options)
      options = {name: Bovem::Localizer.localize_on_locale(options[:locale], :default_application_name), parent: nil, application: nil}.merge(options.ensure_hash)
      run = options.delete(:run)
      [(!run.nil? ? run : true).to_boolean, options.delete(:__args__), options]
    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, options, &block)
      application = new(options, &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

#show_commandsBoolean

Returns If to show command lines run via #run.

Returns:

  • (Boolean)

    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(options = {}, &block)
    raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::Localizer.localize_on_locale(options[:locale], :missing_app_block)) if !block_given?
    run, args, options = setup_application_option(options)

    begin
      create_application(run, args, options, &block)
    rescue => e
      Kernel.puts(e.to_s)
      Kernel.exit(1)
    end
  end

  # Creates a new application.
  #
  # @param options [Hash] The settings to initialize the application with.
  def initialize(options = {}, &block)
    super(options, &block)

    @shell = Bovem::Shell.instance
    @console = @shell.console
    @skip_commands = false
    @show_commands = false
    @output_commands = false

    help_option
  end

  # 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.help_message){ |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, message = nil, show_exit = true, fatal = true)
    @shell.run(command, message, !@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(options)
      options = {name: Bovem::Localizer.localize_on_locale(options[:locale], :default_application_name), parent: nil, application: nil}.merge(options.ensure_hash)
      run = options.delete(:run)
      [(!run.nil? ? run : true).to_boolean, options.delete(:__args__), options]
    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, options, &block)
      application = new(options, &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

#skip_commandsBoolean

Returns If to skip commands run via #run.

Returns:

  • (Boolean)

    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(options = {}, &block)
    raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::Localizer.localize_on_locale(options[:locale], :missing_app_block)) if !block_given?
    run, args, options = setup_application_option(options)

    begin
      create_application(run, args, options, &block)
    rescue => e
      Kernel.puts(e.to_s)
      Kernel.exit(1)
    end
  end

  # Creates a new application.
  #
  # @param options [Hash] The settings to initialize the application with.
  def initialize(options = {}, &block)
    super(options, &block)

    @shell = Bovem::Shell.instance
    @console = @shell.console
    @skip_commands = false
    @show_commands = false
    @output_commands = false

    help_option
  end

  # 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.help_message){ |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, message = nil, show_exit = true, fatal = true)
    @shell.run(command, message, !@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(options)
      options = {name: Bovem::Localizer.localize_on_locale(options[:locale], :default_application_name), parent: nil, application: nil}.merge(options.ensure_hash)
      run = options.delete(:run)
      [(!run.nil? ? run : true).to_boolean, options.delete(:__args__), options]
    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, options, &block)
      application = new(options, &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

#version(value = nil) ⇒ String|nil

Reads and optionally sets the version of this application.

Parameters:

  • value (String|nil) (defaults to: nil)

    The new version of this application.

Returns:

  • (String|nil)

    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(options = {}, &block)
    raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::Localizer.localize_on_locale(options[:locale], :missing_app_block)) if !block_given?
    run, args, options = setup_application_option(options)

    begin
      create_application(run, args, options, &block)
    rescue => e
      Kernel.puts(e.to_s)
      Kernel.exit(1)
    end
  end

  # Creates a new application.
  #
  # @param options [Hash] The settings to initialize the application with.
  def initialize(options = {}, &block)
    super(options, &block)

    @shell = Bovem::Shell.instance
    @console = @shell.console
    @skip_commands = false
    @show_commands = false
    @output_commands = false

    help_option
  end

  # 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.help_message){ |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, message = nil, show_exit = true, fatal = true)
    @shell.run(command, message, !@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(options)
      options = {name: Bovem::Localizer.localize_on_locale(options[:locale], :default_application_name), parent: nil, application: nil}.merge(options.ensure_hash)
      run = options.delete(:run)
      [(!run.nil? ? run : true).to_boolean, options.delete(:__args__), options]
    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, options, &block)
      application = new(options, &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

.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.

Parameters:

  • options (Hash) (defaults to: {})

    The settings to initialize the application with.

Returns:

Raises:

See Also:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bovem/application.rb', line 40

def self.create(options = {}, &block)
  raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::Localizer.localize_on_locale(options[:locale], :missing_app_block)) if !block_given?
  run, args, options = setup_application_option(options)

  begin
    create_application(run, args, options, &block)
  rescue => e
    Kernel.puts(e.to_s)
    Kernel.exit(1)
  end
end

Instance Method Details

#command_help(command) ⇒ Object

Shows a help about a command.

Parameters:

  • command (Command)

    The command to show help for.



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

#executable_nameString

The name of the current executable.

Returns:

  • (String)

    The name of the current executable.



95
96
97
# File 'lib/bovem/application.rb', line 95

def executable_name
  $0
end

#execute(args = nil) ⇒ Object

Executes this application.

Parameters:

  • args (Array) (defaults to: nil)

    The command line to pass to this application. Defaults to ARGV.



79
80
81
# File 'lib/bovem/application.rb', line 79

def execute(args = nil)
  super(args || ARGV)
end

#help_optionObject

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.help_message){ |application, _| application.show_help }
end

#run(command, message = nil, show_exit = true, fatal = true) ⇒ Hash

Runs a command into the shell.

Parameters:

  • command (String)

    The string to run.

  • message (String) (defaults to: nil)

    A message to show before running.

  • show_exit (Boolean) (defaults to: true)

    If show the exit status.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Hash)

    An hash with status and output keys.



124
125
126
# File 'lib/bovem/application.rb', line 124

def run(command, message = nil, show_exit = true, fatal = true)
  @shell.run(command, message, !@skip_commands, show_exit, @output_commands, @show_commands, fatal)
end