bovem 3.0.5 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -3
  3. data/.rubocop.yml +82 -0
  4. data/.travis-gemfile +4 -5
  5. data/.travis.yml +8 -6
  6. data/CHANGELOG.md +12 -0
  7. data/Gemfile +9 -8
  8. data/README.md +1 -1
  9. data/Rakefile +22 -6
  10. data/bovem.gemspec +5 -5
  11. data/doc/Bovem.html +10 -10
  12. data/doc/Bovem/Application.html +670 -318
  13. data/doc/Bovem/Command.html +1447 -1125
  14. data/doc/Bovem/CommandMethods.html +4 -4
  15. data/doc/Bovem/CommandMethods/Children.html +173 -179
  16. data/doc/Bovem/CommandMethods/Help.html +9 -9
  17. data/doc/Bovem/Configuration.html +239 -24
  18. data/doc/Bovem/Console.html +267 -128
  19. data/doc/Bovem/ConsoleMethods.html +4 -4
  20. data/doc/Bovem/ConsoleMethods/Interactions.html +57 -70
  21. data/doc/Bovem/ConsoleMethods/Interactions/ClassMethods.html +9 -9
  22. data/doc/Bovem/ConsoleMethods/Logging.html +258 -298
  23. data/doc/Bovem/ConsoleMethods/Logging/ClassMethods.html +8 -8
  24. data/doc/Bovem/ConsoleMethods/Output.html +96 -118
  25. data/doc/Bovem/ConsoleMethods/StyleHandling.html +8 -8
  26. data/doc/Bovem/ConsoleMethods/StyleHandling/ClassMethods.html +26 -39
  27. data/doc/Bovem/Errors.html +4 -4
  28. data/doc/Bovem/Errors/Error.html +4 -4
  29. data/doc/Bovem/Errors/InvalidConfiguration.html +4 -4
  30. data/doc/Bovem/Errors/InvalidLogger.html +4 -4
  31. data/doc/Bovem/I18n.html +175 -0
  32. data/doc/Bovem/Logger.html +95 -83
  33. data/doc/Bovem/Option.html +669 -862
  34. data/doc/Bovem/Parser.html +10 -10
  35. data/doc/Bovem/ParserMethods.html +4 -4
  36. data/doc/Bovem/ParserMethods/General.html +4 -4
  37. data/doc/Bovem/ParserMethods/General/ClassMethods.html +26 -38
  38. data/doc/Bovem/Shell.html +169 -48
  39. data/doc/Bovem/ShellMethods.html +4 -4
  40. data/doc/Bovem/ShellMethods/Directories.html +46 -62
  41. data/doc/Bovem/ShellMethods/Execute.html +51 -99
  42. data/doc/Bovem/ShellMethods/General.html +4 -445
  43. data/doc/Bovem/ShellMethods/Read.html +56 -61
  44. data/doc/Bovem/ShellMethods/Write.html +22 -242
  45. data/doc/Bovem/Version.html +6 -6
  46. data/doc/_index.html +18 -18
  47. data/doc/class_list.html +6 -2
  48. data/doc/css/style.css +1 -0
  49. data/doc/file.README.html +5 -5
  50. data/doc/file_list.html +5 -1
  51. data/doc/frames.html +1 -1
  52. data/doc/index.html +5 -5
  53. data/doc/js/full_list.js +4 -1
  54. data/doc/method_list.html +161 -157
  55. data/doc/top-level-namespace.html +4 -4
  56. data/lib/bovem.rb +3 -4
  57. data/lib/bovem/application.rb +47 -39
  58. data/lib/bovem/command.rb +175 -193
  59. data/lib/bovem/configuration.rb +28 -29
  60. data/lib/bovem/console.rb +244 -171
  61. data/lib/bovem/errors.rb +1 -1
  62. data/lib/bovem/i18n.rb +18 -0
  63. data/lib/bovem/logger.rb +26 -26
  64. data/lib/bovem/option.rb +49 -58
  65. data/lib/bovem/parser.rb +174 -222
  66. data/lib/bovem/shell.rb +272 -320
  67. data/lib/bovem/version.rb +2 -2
  68. data/locales/en.yml +39 -38
  69. data/locales/it.yml +39 -38
  70. data/spec/bovem/application_spec.rb +6 -5
  71. data/spec/bovem/command_spec.rb +23 -23
  72. data/spec/bovem/console_spec.rb +101 -102
  73. data/spec/bovem/i18n_spec.rb +21 -0
  74. data/spec/bovem/logger_spec.rb +4 -4
  75. data/spec/bovem/option_spec.rb +43 -43
  76. data/spec/bovem/parser_spec.rb +13 -13
  77. data/spec/bovem/shell_spec.rb +106 -115
  78. data/spec/spec_helper.rb +19 -6
  79. metadata +14 -13
  80. data/doc/Bovem/Localizer.html +0 -376
  81. data/lib/bovem/localizer.rb +0 -27
  82. data/spec/coverage_helper.rb +0 -20
@@ -6,7 +6,7 @@
6
6
  <title>
7
7
  Top Level Namespace
8
8
 
9
- &mdash; Documentation by YARD 0.8.7.3
9
+ &mdash; Documentation by YARD 0.8.7.6
10
10
 
11
11
  </title>
12
12
 
@@ -17,7 +17,7 @@
17
17
  <script type="text/javascript" charset="utf-8">
18
18
  hasFrames = window.top.frames.main ? true : false;
19
19
  relpath = '';
20
- framesUrl = "frames.html#!" + escape(window.location.href);
20
+ framesUrl = "frames.html#!top-level-namespace.html";
21
21
  </script>
22
22
 
23
23
 
@@ -103,9 +103,9 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Sat Mar 8 10:27:42 2014 by
106
+ Generated on Tue Mar 29 20:43:16 2016 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
- 0.8.7.3 (ruby-2.1.0).
108
+ 0.8.7.6 (ruby-2.3.0).
109
109
  </div>
110
110
 
111
111
  </body>
@@ -9,14 +9,13 @@ require "prettyprint"
9
9
  require "logger"
10
10
  require "open4"
11
11
  require "find"
12
- require "fileutils"
13
12
  require "lazier"
14
13
 
15
14
  Lazier.load!(:object, :boolean, :math)
16
15
 
17
- require "bovem/version" if !defined?(Bovem::Version)
16
+ require "bovem/version" unless defined?(Bovem::Version)
17
+ require "bovem/i18n"
18
18
  require "bovem/errors"
19
- require "bovem/localizer"
20
19
  require "bovem/configuration"
21
20
  require "bovem/logger"
22
21
  require "bovem/console"
@@ -24,4 +23,4 @@ require "bovem/shell"
24
23
  require "bovem/command"
25
24
  require "bovem/option"
26
25
  require "bovem/application"
27
- require "bovem/parser"
26
+ require "bovem/parser"
@@ -29,6 +29,9 @@ module Bovem
29
29
  attr_accessor :show_commands
30
30
  attr_accessor :output_commands
31
31
 
32
+ # The location of the locales translation files.
33
+ LOCALE_ROOT = ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/").freeze
34
+
32
35
  # Initializes a new Bovem application.
33
36
  #
34
37
  # In options, you can override the command line arguments with `:__args__`, and you can skip execution by specifying `run: false`.
@@ -38,7 +41,13 @@ module Bovem
38
41
  # @param options [Hash] The settings to initialize the application with.
39
42
  # @return [Application] The created application.
40
43
  def self.create(options = {}, &block)
41
- raise Bovem::Errors::Error.new(Bovem::Application, :missing_block, Bovem::Localizer.localize_on_locale(options[:locale], :missing_app_block)) if !block_given?
44
+ unless block_given?
45
+ raise Bovem::Errors::Error.new(
46
+ Bovem::Application, :missing_block,
47
+ Bovem::I18n.new(options[:locale], root: "bovem.application", path: LOCALE_ROOT).missing_app_block
48
+ )
49
+ end
50
+
42
51
  run, args, options = setup_application_option(options)
43
52
 
44
53
  begin
@@ -49,6 +58,32 @@ module Bovem
49
58
  end
50
59
  end
51
60
 
61
+ # Setup options for application creation.
62
+ #
63
+ # @param options [Hash] The options to setups.
64
+ # @return [Array] If to run the application, the arguments and the specified options.
65
+ def self.setup_application_option(options)
66
+ base_options = {
67
+ name: Bovem::I18n.new(options[:locale], root: "bovem.application", path: LOCALE_ROOT).default_application_name,
68
+ parent: nil, application: nil
69
+ }
70
+ options = base_options.merge(options.ensure_hash)
71
+ run = options.delete(:run)
72
+ [(!run.nil? ? run : true).to_boolean, options.delete(:__args__), options]
73
+ end
74
+
75
+ # Create the application.
76
+ #
77
+ # @param run [Boolean] If to run the application.
78
+ # @param args [Hash] The arguments to use for running.
79
+ # @param options [Hash] The options of the application.
80
+ # @return [Application] The new application.
81
+ def self.create_application(run, args, options, &block)
82
+ application = new(options, &block)
83
+ application.execute(args) if application && run
84
+ application
85
+ end
86
+
52
87
  # Creates a new application.
53
88
  #
54
89
  # @param options [Hash] The settings to initialize the application with.
@@ -69,7 +104,7 @@ module Bovem
69
104
  # @param value [String|nil] The new version of this application.
70
105
  # @return [String|nil] The version of this application.
71
106
  def version(value = nil)
72
- @version = value.ensure_string if !value.nil?
107
+ @version = value.ensure_string unless value.nil?
73
108
  @version
74
109
  end
75
110
 
@@ -86,14 +121,14 @@ module Bovem
86
121
  action { |command| application.command_help(command) }
87
122
  end
88
123
 
89
- option(:help, [i18n.help_option_short_form, i18n.help_option_long_form], help: i18n.help_message){ |application, _| application.show_help }
124
+ option(:help, [i18n.help_option_short_form, i18n.help_option_long_form], help: i18n.help_message) { |application, _| application.show_help }
90
125
  end
91
126
 
92
127
  # The name of the current executable.
93
128
  #
94
129
  # @return [String] The name of the current executable.
95
130
  def executable_name
96
- $0
131
+ $PROGRAM_NAME
97
132
  end
98
133
 
99
134
  # Shows a help about a command.
@@ -102,13 +137,9 @@ module Bovem
102
137
  def command_help(command)
103
138
  fetch_commands_for_help(command).each do |arg|
104
139
  # Find the command across
105
- next_command = Bovem::Parser.find_command(arg, command, [])
106
-
107
- if next_command then
108
- command = command.commands[next_command[:name]]
109
- else
110
- break
111
- end
140
+ next_command = Bovem::Parser.find_command(arg, command, args: [])
141
+ break unless next_command
142
+ command = command.commands[next_command[:name]]
112
143
  end
113
144
 
114
145
  command.show_help
@@ -126,33 +157,10 @@ module Bovem
126
157
  end
127
158
 
128
159
  private
129
- # Setup options for application creation.
130
- #
131
- # @param options [Hash] The options to setups.
132
- # @return [Array] If to run the application, the arguments and the specified options.
133
- def self.setup_application_option(options)
134
- options = {name: Bovem::Localizer.localize_on_locale(options[:locale], :default_application_name), parent: nil, application: nil}.merge(options.ensure_hash)
135
- run = options.delete(:run)
136
- [(!run.nil? ? run : true).to_boolean, options.delete(:__args__), options]
137
- end
138
160
 
139
- # Create the application.
140
- #
141
- # @param run [Boolean ]If to run the application.
142
- # @param args [Hash] The arguments to use for running.
143
- # @param options [Hash] The options of the application.
144
- # @return [Application] The new application.
145
- def self.create_application(run, args, options, &block)
146
- application = new(options, &block)
147
- application.execute(args) if application && run
148
- application
149
- end
150
-
151
- # Fetch a command list for showing help.
152
- #
153
- # @param command [Command] The command to show help for.
154
- def fetch_commands_for_help(command)
155
- command.arguments.map {|c| c.split(":") }.flatten.map(&:strip).select(&:present?)
156
- end
161
+ # :nodoc:
162
+ def fetch_commands_for_help(command)
163
+ command.arguments.map { |c| c.split(":") }.flatten.map(&:strip).select(&:present?)
164
+ end
157
165
  end
158
- end
166
+ end
@@ -11,122 +11,121 @@ module Bovem
11
11
  module Help
12
12
  # Shows a help about this command.
13
13
  def show_help
14
- console = is_application? ? self.console : application.console
15
- is_application? ? show_help_application_summary(console) : show_help_command_summary(console)
16
- show_help_banner(console) if has_banner?
17
- show_help_options(console) if has_options?
18
- show_help_commands(console) if has_commands?
14
+ console = application? ? self.console : application.console
15
+ application? ? show_help_application_summary(console) : show_help_command_summary(console)
16
+ show_help_banner(console) if banner?
17
+ show_help_options(console) if options?
18
+ show_help_commands(console) if commands?
19
19
  Kernel.exit(0)
20
20
  end
21
21
 
22
22
  private
23
- # Prints a help summary about the application.
24
- #
25
- # @param console [Bovem::Console] The console object to use to print.
26
- def show_help_application_summary(console)
27
- # Application
28
- console.write(i18n.help_name)
29
- console.write("%s %s%s" % [name, version, has_description? ? " - " + description : ""], "\n", 4, true)
30
- show_synopsis(console)
31
- end
32
23
 
33
- # Prints a synopsis about the application.
34
- #
35
- # @param console [Bovem::Console] The console object to use to print.
36
- def show_synopsis(console)
37
- console.write("")
38
- console.write(i18n.help_synopsis)
39
- console.write(synopsis.present? ? synopsis : i18n.help_application_synopsis % [executable_name, has_commands? ? i18n.help_subcommand_invocation : ""], "\n", 4, true)
40
- end
24
+ # :nodoc:
25
+ def show_help_application_summary(console)
26
+ # Application
27
+ console.write(i18n.help_name)
28
+ console.write(sprintf("%s %s%s", name, version, description? ? " - " + description : ""), suffix: "\n", indented: 4, wrap: true)
29
+ show_synopsis(console)
30
+ end
41
31
 
42
- # Prints a help summary about the command.
43
- #
44
- # @param console [Bovem::Console] The console object to use to print.
45
- def show_help_command_summary(console)
46
- console.write(i18n.help_synopsis)
47
- console.write(synopsis.present? ? synopsis : i18n.help_command_synopsis % [application.executable_name, full_name(nil, " "), has_commands? ? i18n.help_subsubcommand_invocation : ""], "\n", 4, true)
48
- end
32
+ # :nodoc:
33
+ def show_synopsis(console)
34
+ console.write("")
35
+ console.write(i18n.help_synopsis)
36
+ console.write(format_synopsis, suffix: "\n", indented: 4, wrap: true)
37
+ end
49
38
 
50
- # Prints the description of the command.
51
- #
52
- # @param console [Bovem::Console] The console object to use to print.
53
- def show_help_banner(console)
54
- console.write("")
55
- console.write(i18n.help_description)
56
- console.write(banner, "\n", 4, true)
57
- end
39
+ # :nodoc:
40
+ def show_help_command_summary(console)
41
+ console.write(i18n.help_synopsis)
42
+ console.write(format_summary, suffix: "\n", indented: 4, wrap: true)
43
+ end
58
44
 
59
- # Prints information about the command's options.
60
- #
61
- # @param console [Bovem::Console] The console object to use to print.
62
- def show_help_options(console)
63
- console.write("")
64
- console.write(is_application? ? i18n.help_global_options : i18n.help_options)
45
+ # :nodoc:
46
+ def show_help_banner(console)
47
+ console.write("")
48
+ console.write(i18n.help_description)
49
+ console.write(banner, suffix: "\n", indented: 4, wrap: true)
50
+ end
65
51
 
66
- # First of all, grab all options and construct labels
67
- lefts = show_help_options_build_labels
52
+ # :nodoc:
53
+ def show_help_options(console)
54
+ console.write("")
55
+ console.write(application? ? i18n.help_global_options : i18n.help_options)
68
56
 
69
- console.with_indentation(4) do
70
- lefts.keys.sort.each do |head|
71
- show_help_option(console, lefts, head)
72
- end
73
- end
74
- end
57
+ # First of all, grab all options and construct labels
58
+ lefts = show_help_options_build_labels
75
59
 
76
- # Adjusts options names for printing.
77
- #
78
- # @return [Hash] The adjusted options for printing.
79
- def show_help_options_build_labels
80
- options.values.reduce({}) do |lefts, option|
81
- left = [option.complete_short, option.complete_long]
82
- left.map!{|l| l + " " + option.meta } if option.requires_argument?
83
- lefts[left.join(", ")] = option.has_help? ? option.help : i18n.help_no_description
84
- lefts
60
+ console.with_indentation(4) do
61
+ lefts.keys.sort.each do |head|
62
+ show_help_option(console, lefts, head)
85
63
  end
86
64
  end
65
+ end
87
66
 
88
- # Prints information about an option.
89
- #
90
- # @param console [Bovem::Console] The console object to use to print.
91
- # @param lefts [Hash] The list of adjusted options.
92
- # @param head [String] The option to print.
93
- def show_help_option(console, lefts, head)
94
- alignment = lefts.keys.map(&:length).max
95
- help = lefts[head]
96
- console.write("%s - %s" % [head.ljust(alignment, " "), help], "\n", true, true)
67
+ # :nodoc:
68
+ def show_help_options_build_labels
69
+ options.values.reduce({}) do |lefts, option|
70
+ left = [option.complete_short, option.complete_long]
71
+ format_label(option, left, lefts)
97
72
  end
73
+ end
98
74
 
99
- # Prints information about the command's subcommands.
100
- #
101
- # @param console [Bovem::Console] The console object to use to print.
102
- def show_help_commands(console)
103
- alignment = prepare_show_help_commands(console)
75
+ # :nodoc:
76
+ def show_help_option(console, lefts, head)
77
+ alignment = lefts.keys.map(&:length).max
78
+ help = lefts[head]
79
+ console.write(sprintf("%s - %s", head.ljust(alignment, " "), help), suffix: "\n", indented: true, wrap: true)
80
+ end
81
+
82
+ # :nodoc:
83
+ def show_help_commands(console)
84
+ alignment = prepare_show_help_commands(console)
104
85
 
105
- console.with_indentation(4) do
106
- commands.keys.sort.each do |name|
107
- show_help_command(console, name, alignment)
108
- end
86
+ console.with_indentation(4) do
87
+ commands.keys.sort.each do |name|
88
+ show_help_command(console, name, alignment)
109
89
  end
110
90
  end
91
+ end
111
92
 
112
- # Starts printing information about the command's subcommands.
113
- #
114
- # @param console [Bovem::Console] The console object to use to print.
115
- def prepare_show_help_commands(console)
116
- console.write("")
117
- console.write(is_application? ? i18n.help_commands : i18n.help_subcommands)
118
- commands.keys.map(&:length).max
119
- end
93
+ # :nodoc:
94
+ def prepare_show_help_commands(console)
95
+ console.write("")
96
+ console.write(application? ? i18n.help_commands : i18n.help_subcommands)
97
+ commands.keys.map(&:length).max
98
+ end
120
99
 
121
- # Prints information about a command's subcommand.
122
- #
123
- # @param name [String] The name of command to print.
124
- # @param console [Bovem::Console] The console object to use to print.
125
- def show_help_command(console, name, alignment)
126
- # Find the maximum length of the commands
127
- command = commands[name]
128
- console.write("%s - %s" % [name.ljust(alignment, " "), command.description.present? ? command.description : i18n.help_no_description], "\n", true, true)
129
- end
100
+ # :nodoc:
101
+ def show_help_command(console, name, alignment)
102
+ # Find the maximum length of the commands
103
+ command = commands[name]
104
+ console.write(
105
+ sprintf("%s - %s", name.ljust(alignment, " "), command.description.present? ? command.description : i18n.help_no_description),
106
+ suffix: "\n", indented: true, wrap: true
107
+ )
108
+ end
109
+
110
+ # :nodoc:
111
+ def format_synopsis
112
+ sprintf(synopsis.present? ? synopsis : i18n.help_application_synopsis, executable_name, commands? ? i18n.help_subcommand_invocation : "")
113
+ end
114
+
115
+ # :nodoc:
116
+ def format_summary
117
+ sprintf(
118
+ synopsis.present? ? synopsis : i18n.help_command_synopsis,
119
+ application.executable_name, full_name(nil, " "), commands? ? i18n.help_subsubcommand_invocation : ""
120
+ )
121
+ end
122
+
123
+ # :nodoc:
124
+ def format_label(option, left, lefts)
125
+ left.map! { |l| l + " " + option.meta } if option.requires_argument?
126
+ lefts[left.join(", ")] = option.help? ? option.help : i18n.help_no_description
127
+ lefts
128
+ end
130
129
  end
131
130
 
132
131
  # Methods to manage options and subcommands.
@@ -161,12 +160,8 @@ module Bovem
161
160
  name = name.ensure_string
162
161
  @options ||= HashWithIndifferentAccess.new
163
162
 
164
- if @options[name] then
165
- if is_application? then
166
- raise Bovem::Errors::Error.new(self, :duplicate_option, i18n.existing_option_global(name))
167
- else
168
- raise Bovem::Errors::Error.new(self, :duplicate_option, i18n.existing_option(name, full_name))
169
- end
163
+ if @options[name]
164
+ raise Bovem::Errors::Error.new(self, :duplicate_option, application? ? i18n.existing_option_global(name) : i18n.existing_option(name, full_name))
170
165
  end
171
166
 
172
167
  option = Bovem::Option.new(name, forms, options, &action)
@@ -192,8 +187,8 @@ module Bovem
192
187
  # Check if this command has subcommands.
193
188
  #
194
189
  # @return [Boolean] `true` if this command has subcommands, `false` otherwise.
195
- def has_commands?
196
- commands.length > 0
190
+ def commands?
191
+ !commands.empty?
197
192
  end
198
193
 
199
194
  # Returns the list of options of this command.
@@ -212,8 +207,8 @@ module Bovem
212
207
  # Check if this command has options.
213
208
  #
214
209
  # @return [Boolean] `true` if this command has options, `false` otherwise.
215
- def has_options?
216
- options.length > 0
210
+ def options?
211
+ !options.empty?
217
212
  end
218
213
 
219
214
  # Adds a new argument to this command.
@@ -241,52 +236,43 @@ module Bovem
241
236
  # @param prefix [String] The prefix to add to the option of this command.
242
237
  # @param whitelist [Array] The list of options to include. By default all options are included.
243
238
  # @return [HashWithIndifferentAccess] The requested options.
244
- def get_options(unprovided = false, application = "application_", prefix = "", *whitelist)
239
+ def get_options(unprovided: false, application: "application_", prefix: "", whitelist: [])
245
240
  rv = HashWithIndifferentAccess.new
246
- rv.merge!(self.application.get_options(unprovided, nil, application, *whitelist)) if application && !is_application?
241
+
242
+ if application && !application?
243
+ rv.merge!(self.application.get_options(unprovided: unprovided, application: nil, prefix: application, whitelist: whitelist))
244
+ end
245
+
247
246
  rv.merge!(get_current_options(unprovided, prefix, whitelist))
248
247
  rv
249
248
  end
250
249
 
251
250
  private
252
- # Creates a new command.
253
- #
254
- # @param name [String] The name of this command.
255
- # @param options [Hash] The settings for this command.
256
- # @return [Command] The new command.
257
- def create_command(name, options, &block)
258
- command = Bovem::Command.new(options, &block)
259
- command.option(:help, [i18n.help_option_short_form, i18n.help_option_long_form], help: i18n.help_message){|c, _| c.show_help }
260
- @commands[name.to_s] = command
261
- command
262
- end
263
251
 
264
- # Gets the list of the options of this command.
265
- # @param unprovided [Boolean] If to include also options that were not provided by the user and that don't have any default value.
266
- # @param prefix [String] The prefix to add to the option of this command.
267
- # @param whitelist [Array] The list of options to include. By default all options are included.
268
- # @return [HashWithIndifferentAccess] The requested options.
269
- def get_current_options(unprovided, prefix, whitelist)
270
- rv = HashWithIndifferentAccess.new
271
- whitelist = (whitelist.present? ? whitelist : options.keys).map(&:to_s)
272
-
273
- options.each do |key, option|
274
- rv["#{prefix}#{key}"] = option.value if include_option?(whitelist, unprovided, key, option)
275
- end
252
+ # :nodoc:
253
+ def create_command(name, options, &block)
254
+ command = Bovem::Command.new(options, &block)
255
+ command.option(:help, [i18n.help_option_short_form, i18n.help_option_long_form], help: i18n.help_message) { |c, _| c.show_help }
256
+ @commands[name.to_s] = command
257
+ command
258
+ end
276
259
 
277
- rv
278
- end
260
+ # :nodoc:
261
+ def get_current_options(unprovided, prefix, whitelist)
262
+ rv = HashWithIndifferentAccess.new
263
+ whitelist = (whitelist.present? ? whitelist : options.keys).map(&:to_s)
279
264
 
280
- # Checks if a option must be included in a hash.
281
- #
282
- # @param whitelist [Array] The list of options to include.
283
- # @param unprovided [Boolean] If to include also options that were not provided by the user and that don't have any default value.
284
- # @param key [String] The option name.
285
- # @param option [Option] The option to include.
286
- # @return [Boolean] Whether to include the option.
287
- def include_option?(whitelist, unprovided, key, option)
288
- whitelist.include?(key.to_s) && (option.provided? || option.has_default? || (unprovided && option.action.nil?))
265
+ options.each do |key, option|
266
+ rv["#{prefix}#{key}"] = option.value if include_option?(whitelist, unprovided, key, option)
289
267
  end
268
+
269
+ rv
270
+ end
271
+
272
+ # :nodoc:
273
+ def include_option?(whitelist, unprovided, key, option)
274
+ whitelist.include?(key.to_s) && (option.provided? || option.default? || (unprovided && option.action.nil?))
275
+ end
290
276
  end
291
277
  end
292
278
 
@@ -318,6 +304,8 @@ module Bovem
318
304
  # @return [Array] The options available for this command.
319
305
  # @attribute [r] arguments
320
306
  # @return [Array] The arguments provided to this command.
307
+ # @attribute [r] i18n
308
+ # @return [I18n] A i18n helper.
321
309
  class Command
322
310
  attr_accessor :name
323
311
  attr_accessor :description
@@ -328,8 +316,8 @@ module Bovem
328
316
  attr_accessor :after
329
317
  attr_accessor :application
330
318
  attr_accessor :parent
319
+ attr_reader :i18n
331
320
 
332
- include Lazier::I18n
333
321
  include Bovem::CommandMethods::Help
334
322
  include Bovem::CommandMethods::Children
335
323
 
@@ -346,7 +334,7 @@ module Bovem
346
334
  # @param value [NilClass|Object] The new name of this command.
347
335
  # @return [String] The name of this command.
348
336
  def name(value = nil)
349
- @name = value if !value.nil?
337
+ @name = value unless value.nil?
350
338
  @name
351
339
  end
352
340
 
@@ -356,11 +344,8 @@ module Bovem
356
344
  # @param separator [String] The separator to use for components.
357
345
  # @return [String] The full name.
358
346
  def full_name(suffix = nil, separator = ":")
359
- if is_application? then
360
- nil
361
- else
362
- [@parent ? @parent.full_name(nil, separator) : nil, !is_application? ? name : nil, suffix].compact.join(separator)
363
- end
347
+ return nil if application?
348
+ [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator)
364
349
  end
365
350
 
366
351
  # Reads and optionally sets the short description of this command.
@@ -368,7 +353,7 @@ module Bovem
368
353
  # @param value [NilClass|Object] The new short description of this command.
369
354
  # @return [String] The short description of this command.
370
355
  def description(value = nil)
371
- @description = value if !value.nil?
356
+ @description = value unless value.nil?
372
357
  @description
373
358
  end
374
359
 
@@ -377,7 +362,7 @@ module Bovem
377
362
  # @param value [NilClass|Object] The new description of this command.
378
363
  # @return [String] The description of this command.
379
364
  def banner(value = nil)
380
- @banner = value if !value.nil?
365
+ @banner = value unless value.nil?
381
366
  @banner
382
367
  end
383
368
 
@@ -386,7 +371,7 @@ module Bovem
386
371
  # @param value [NilClass|Object] The new synopsis of this command.
387
372
  # @return [String] The synopsis of this command.
388
373
  def synopsis(value = nil)
389
- @synopsis = value if !value.nil?
374
+ @synopsis = value unless value.nil?
390
375
  @synopsis
391
376
  end
392
377
 
@@ -430,27 +415,27 @@ module Bovem
430
415
  #
431
416
  # @return [Application] The application this command belongs to or `self`, if the command is an Application.
432
417
  def application
433
- is_application? ? self : @application
418
+ application? ? self : @application
434
419
  end
435
420
 
436
421
  # Checks if the command is an application.
437
422
  #
438
423
  # @return [Boolean] `true` if command is an application, `false` otherwise.
439
- def is_application?
424
+ def application?
440
425
  is_a?(Bovem::Application)
441
426
  end
442
427
 
443
428
  # Check if this command has a description.
444
429
  #
445
430
  # @return [Boolean] `true` if this command has a description, `false` otherwise.
446
- def has_description?
431
+ def description?
447
432
  description.present?
448
433
  end
449
434
 
450
435
  # Check if this command has a banner.
451
436
  #
452
437
  # @return [Boolean] `true` if this command has a banner, `false` otherwise.
453
- def has_banner?
438
+ def banner?
454
439
  banner.present?
455
440
  end
456
441
 
@@ -459,15 +444,15 @@ module Bovem
459
444
  # @param options [Hash] The settings for this command.
460
445
  # @return [Command] The command.
461
446
  def setup_with(options = {})
462
- options = {} if !options.is_a?(::Hash)
447
+ options = {} unless options.is_a?(::Hash)
463
448
  setup_i18n(options)
464
449
 
465
450
  options.each_pair do |option, value|
466
451
  method = option.to_s
467
452
 
468
- if respond_to?(method) && self.method(method).arity != 0 then
453
+ if respond_to?(method) && self.method(method).arity != 0
469
454
  send(method, value)
470
- elsif respond_to?(method + "=") then
455
+ elsif respond_to?(method + "=")
471
456
  send(method + "=", value)
472
457
  end
473
458
  end
@@ -481,49 +466,46 @@ module Bovem
481
466
  def execute(args)
482
467
  subcommand = Bovem::Parser.parse(self, args)
483
468
 
484
- if subcommand.present? then # We have a subcommand to call
469
+ if subcommand.present? # We have a subcommand to call
485
470
  commands[subcommand[:name]].execute(subcommand[:args])
486
- elsif action then # Run our action
471
+ elsif action # Run our action
487
472
  # Run the before hook
488
- execute_hook(before)
489
-
490
- # Run the action
491
- execute_hook(action)
492
-
493
- # Run the after hook
494
- execute_hook(after)
473
+ perform_action
495
474
  else # Show the help
496
475
  show_help
497
476
  end
498
477
  end
499
478
 
500
479
  private
501
- # Setups the application localization.
502
- #
503
- # @param options [Hash] The settings for this command.
504
- def setup_i18n(options)
505
- i18n_setup("bovem.application", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
506
- self.i18n = (options[:locale]).ensure_string
507
- end
508
480
 
509
- # Assigns a hook to a command.
510
- #
511
- # @param method [String|Symbol|NilClass] The method of the application to hookup.
512
- # @param hook [Proc] The block to hookup if method is not provided.
513
- def assign_hook(method, &hook)
514
- assigned = nil
515
- assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
516
- assigned = hook if !assigned && hook && hook.arity == 1
517
- assigned
518
- end
481
+ # :nodoc:
482
+ def setup_i18n(options)
483
+ @i18n = Bovem::I18n.new(options[:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT)
484
+ end
519
485
 
520
- # Executes a hook.
521
- #
522
- # @param hook [String|Symbol|Proc|NilClass] The hook to execute.
523
- def execute_hook(hook)
524
- if hook then
525
- hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
526
- end
527
- end
486
+ # :nodoc:
487
+ def assign_hook(method, &hook)
488
+ assigned = nil
489
+ assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
490
+ assigned = hook if !assigned && hook && hook.arity == 1
491
+ assigned
492
+ end
493
+
494
+ # :nodoc:
495
+ def execute_hook(hook)
496
+ return unless hook
497
+ hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
498
+ end
499
+
500
+ # :nodoc:
501
+ def perform_action
502
+ execute_hook(before)
503
+
504
+ # Run the action
505
+ execute_hook(action)
506
+
507
+ # Run the after hook
508
+ execute_hook(after)
509
+ end
528
510
  end
529
- end
511
+ end