bovem 3.0.5 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +4 -3
- data/.rubocop.yml +82 -0
- data/.travis-gemfile +4 -5
- data/.travis.yml +8 -6
- data/CHANGELOG.md +12 -0
- data/Gemfile +9 -8
- data/README.md +1 -1
- data/Rakefile +22 -6
- data/bovem.gemspec +5 -5
- data/doc/Bovem.html +10 -10
- data/doc/Bovem/Application.html +670 -318
- data/doc/Bovem/Command.html +1447 -1125
- data/doc/Bovem/CommandMethods.html +4 -4
- data/doc/Bovem/CommandMethods/Children.html +173 -179
- data/doc/Bovem/CommandMethods/Help.html +9 -9
- data/doc/Bovem/Configuration.html +239 -24
- data/doc/Bovem/Console.html +267 -128
- data/doc/Bovem/ConsoleMethods.html +4 -4
- data/doc/Bovem/ConsoleMethods/Interactions.html +57 -70
- data/doc/Bovem/ConsoleMethods/Interactions/ClassMethods.html +9 -9
- data/doc/Bovem/ConsoleMethods/Logging.html +258 -298
- data/doc/Bovem/ConsoleMethods/Logging/ClassMethods.html +8 -8
- data/doc/Bovem/ConsoleMethods/Output.html +96 -118
- data/doc/Bovem/ConsoleMethods/StyleHandling.html +8 -8
- data/doc/Bovem/ConsoleMethods/StyleHandling/ClassMethods.html +26 -39
- data/doc/Bovem/Errors.html +4 -4
- data/doc/Bovem/Errors/Error.html +4 -4
- data/doc/Bovem/Errors/InvalidConfiguration.html +4 -4
- data/doc/Bovem/Errors/InvalidLogger.html +4 -4
- data/doc/Bovem/I18n.html +175 -0
- data/doc/Bovem/Logger.html +95 -83
- data/doc/Bovem/Option.html +669 -862
- data/doc/Bovem/Parser.html +10 -10
- data/doc/Bovem/ParserMethods.html +4 -4
- data/doc/Bovem/ParserMethods/General.html +4 -4
- data/doc/Bovem/ParserMethods/General/ClassMethods.html +26 -38
- data/doc/Bovem/Shell.html +169 -48
- data/doc/Bovem/ShellMethods.html +4 -4
- data/doc/Bovem/ShellMethods/Directories.html +46 -62
- data/doc/Bovem/ShellMethods/Execute.html +51 -99
- data/doc/Bovem/ShellMethods/General.html +4 -445
- data/doc/Bovem/ShellMethods/Read.html +56 -61
- data/doc/Bovem/ShellMethods/Write.html +22 -242
- data/doc/Bovem/Version.html +6 -6
- data/doc/_index.html +18 -18
- data/doc/class_list.html +6 -2
- data/doc/css/style.css +1 -0
- data/doc/file.README.html +5 -5
- data/doc/file_list.html +5 -1
- data/doc/frames.html +1 -1
- data/doc/index.html +5 -5
- data/doc/js/full_list.js +4 -1
- data/doc/method_list.html +161 -157
- data/doc/top-level-namespace.html +4 -4
- data/lib/bovem.rb +3 -4
- data/lib/bovem/application.rb +47 -39
- data/lib/bovem/command.rb +175 -193
- data/lib/bovem/configuration.rb +28 -29
- data/lib/bovem/console.rb +244 -171
- data/lib/bovem/errors.rb +1 -1
- data/lib/bovem/i18n.rb +18 -0
- data/lib/bovem/logger.rb +26 -26
- data/lib/bovem/option.rb +49 -58
- data/lib/bovem/parser.rb +174 -222
- data/lib/bovem/shell.rb +272 -320
- data/lib/bovem/version.rb +2 -2
- data/locales/en.yml +39 -38
- data/locales/it.yml +39 -38
- data/spec/bovem/application_spec.rb +6 -5
- data/spec/bovem/command_spec.rb +23 -23
- data/spec/bovem/console_spec.rb +101 -102
- data/spec/bovem/i18n_spec.rb +21 -0
- data/spec/bovem/logger_spec.rb +4 -4
- data/spec/bovem/option_spec.rb +43 -43
- data/spec/bovem/parser_spec.rb +13 -13
- data/spec/bovem/shell_spec.rb +106 -115
- data/spec/spec_helper.rb +19 -6
- metadata +14 -13
- data/doc/Bovem/Localizer.html +0 -376
- data/lib/bovem/localizer.rb +0 -27
- data/spec/coverage_helper.rb +0 -20
@@ -6,7 +6,7 @@
|
|
6
6
|
<title>
|
7
7
|
Top Level Namespace
|
8
8
|
|
9
|
-
— Documentation by YARD 0.8.7.
|
9
|
+
— 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#!"
|
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
|
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.
|
108
|
+
0.8.7.6 (ruby-2.3.0).
|
109
109
|
</div>
|
110
110
|
|
111
111
|
</body>
|
data/lib/bovem.rb
CHANGED
@@ -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"
|
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"
|
data/lib/bovem/application.rb
CHANGED
@@ -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
|
-
|
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
|
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
|
-
$
|
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
|
-
|
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
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
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
|
data/lib/bovem/command.rb
CHANGED
@@ -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 =
|
15
|
-
|
16
|
-
show_help_banner(console) if
|
17
|
-
show_help_options(console) if
|
18
|
-
show_help_commands(console) if
|
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
|
-
|
34
|
-
|
35
|
-
#
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
67
|
-
|
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
|
-
|
70
|
-
|
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
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
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
|
-
|
106
|
-
|
107
|
-
|
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
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
-
|
122
|
-
|
123
|
-
#
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
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]
|
165
|
-
|
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
|
196
|
-
commands.
|
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
|
216
|
-
options.
|
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
|
239
|
+
def get_options(unprovided: false, application: "application_", prefix: "", whitelist: [])
|
245
240
|
rv = HashWithIndifferentAccess.new
|
246
|
-
|
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
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
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
|
-
|
278
|
-
|
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
|
-
|
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
|
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
|
360
|
-
|
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
|
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
|
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
|
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
|
-
|
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
|
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
|
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
|
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 = {}
|
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
|
453
|
+
if respond_to?(method) && self.method(method).arity != 0
|
469
454
|
send(method, value)
|
470
|
-
elsif respond_to?(method + "=")
|
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?
|
469
|
+
if subcommand.present? # We have a subcommand to call
|
485
470
|
commands[subcommand[:name]].execute(subcommand[:args])
|
486
|
-
elsif action
|
471
|
+
elsif action # Run our action
|
487
472
|
# Run the before hook
|
488
|
-
|
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
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
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
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
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
|