Line 0
Link Here
|
|
|
1 |
diff --git a/clamp-0.6.3.gemspec b/clamp-0.6.3.gemspec |
2 |
index 8f51cc3..acab485 100644 |
3 |
--- clamp-0.6.3.gemspec |
4 |
+++ clamp-0.6.3.gemspec |
5 |
@@ -43,6 +43,7 @@ |
6 |
- lib/clamp/command.rb |
7 |
- lib/clamp/errors.rb |
8 |
- lib/clamp/help.rb |
9 |
+- lib/clamp/messages.rb |
10 |
- lib/clamp/option/declaration.rb |
11 |
- lib/clamp/option/definition.rb |
12 |
- lib/clamp/option/parsing.rb |
13 |
|
14 |
diff --git a/lib/clamp/attribute/instance.rb b/lib/clamp/attribute/instance.rb |
15 |
index 8f51cc3..acab485 100644 |
16 |
--- lib/clamp/attribute/instance.rb |
17 |
+++ lib/clamp/attribute/instance.rb |
18 |
@@ -72,7 +72,7 @@ def default_from_environment |
19 |
begin |
20 |
take(value) |
21 |
rescue ArgumentError => e |
22 |
- command.send(:signal_usage_error, "$#{attribute.environment_variable}: #{e.message}") |
23 |
+ command.send(:signal_usage_error, Clamp.message(:env_argument_error, :env => attribute.environment_variable, :message => e.message)) |
24 |
end |
25 |
end |
26 |
|
27 |
diff --git a/lib/clamp/command.rb b/lib/clamp/command.rb |
28 |
index c6c50f4..52187ab 100644 |
29 |
--- lib/clamp/command.rb |
30 |
+++ lib/clamp/command.rb |
31 |
@@ -1,3 +1,4 @@ |
32 |
+require 'clamp/messages' |
33 |
require 'clamp/errors' |
34 |
require 'clamp/help' |
35 |
require 'clamp/option/declaration' |
36 |
@@ -91,7 +92,7 @@ def help |
37 |
|
38 |
def handle_remaining_arguments |
39 |
unless remaining_arguments.empty? |
40 |
- signal_usage_error "too many arguments" |
41 |
+ signal_usage_error Clamp.message(:too_many_arguments) |
42 |
end |
43 |
end |
44 |
|
45 |
diff --git a/lib/clamp/help.rb b/lib/clamp/help.rb |
46 |
index e09731a..1e57530 100644 |
47 |
--- lib/clamp/help.rb |
48 |
+++ lib/clamp/help.rb |
49 |
@@ -61,7 +61,7 @@ def string |
50 |
end |
51 |
|
52 |
def add_usage(invocation_path, usage_descriptions) |
53 |
- puts "Usage:" |
54 |
+ puts usage_heading |
55 |
usage_descriptions.each do |usage| |
56 |
puts " #{invocation_path} #{usage}".rstrip |
57 |
end |
58 |
@@ -87,6 +87,12 @@ def add_list(heading, items) |
59 |
end |
60 |
end |
61 |
|
62 |
+ protected |
63 |
+ |
64 |
+ def usage_heading |
65 |
+ "Usage:" |
66 |
+ end |
67 |
+ |
68 |
private |
69 |
|
70 |
def puts(*args) |
71 |
diff --git a/lib/clamp/messages.rb b/lib/clamp/messages.rb |
72 |
new file mode 100644 |
73 |
index 0000000..61cce94 |
74 |
--- /dev/null |
75 |
+++ lib/clamp/messages.rb |
76 |
@@ -0,0 +1,39 @@ |
77 |
+module Clamp |
78 |
+ |
79 |
+ def self.messages=(messages) |
80 |
+ @user_defined_messages = messages |
81 |
+ end |
82 |
+ |
83 |
+ def self.message(key, options={}) |
84 |
+ @user_defined_messages ||= {} |
85 |
+ msg = @user_defined_messages[key] || messages[key] |
86 |
+ format_string(msg, options) |
87 |
+ end |
88 |
+ |
89 |
+ def self.messages |
90 |
+ { |
91 |
+ :too_many_arguments => "too many arguments", |
92 |
+ :option_required => "option '%<option>s' is required", |
93 |
+ :option_or_env_required => "option '%<option>s' (or env %<env>s) is required", |
94 |
+ :option_argument_error => "option '%<switch>s': %<message>s", |
95 |
+ :parameter_argument_error => "parameter '%<param>s': %<message>s", |
96 |
+ :env_argument_error => "$%<env>s: %<message>s", |
97 |
+ :unrecognised_option => "Unrecognised option '%<switch>s'", |
98 |
+ :no_such_subcommand => "No such sub-command '%<name>s'", |
99 |
+ :no_value_provided => "no value provided" |
100 |
+ } |
101 |
+ end |
102 |
+ |
103 |
+ private |
104 |
+ |
105 |
+ # string formatting for ruby 1.8 |
106 |
+ def self.format_string(string, params) |
107 |
+ array_params = string.scan(/%[<{]([^>}]*)[>}]/).collect do |name| |
108 |
+ name = name[0] |
109 |
+ params[name.to_s] || params[name.to_sym] |
110 |
+ end |
111 |
+ |
112 |
+ string.gsub(/%[<]([^>]*)[>]/, '%').gsub(/%[{]([^}]*)[}]/, '%s') % array_params |
113 |
+ end |
114 |
+ |
115 |
+end |
116 |
diff --git a/lib/clamp/option/parsing.rb b/lib/clamp/option/parsing.rb |
117 |
index 9f057f1..bb7b41c 100644 |
118 |
--- lib/clamp/option/parsing.rb |
119 |
+++ lib/clamp/option/parsing.rb |
120 |
@@ -31,7 +31,7 @@ def parse_options |
121 |
begin |
122 |
option.of(self).take(value) |
123 |
rescue ArgumentError => e |
124 |
- signal_usage_error "option '#{switch}': #{e.message}" |
125 |
+ signal_usage_error Clamp.message(:option_argument_error, :switch => switch, :message => e.message) |
126 |
end |
127 |
|
128 |
end |
129 |
@@ -45,11 +45,11 @@ def parse_options |
130 |
self.class.recognised_options.each do |option| |
131 |
# If this option is required and the value is nil, there's an error. |
132 |
if option.required? and send(option.attribute_name).nil? |
133 |
- message = "option '#{option.switches.first}'" |
134 |
if option.environment_variable |
135 |
- message += " (or env #{option.environment_variable})" |
136 |
+ message = Clamp.message(:option_or_env_required, :option => option.switches.first, :env => option.environment_variable) |
137 |
+ else |
138 |
+ message = Clamp.message(:option_required, :option => option.switches.first) |
139 |
end |
140 |
- message += " is required" |
141 |
signal_usage_error message |
142 |
end |
143 |
end |
144 |
@@ -59,7 +59,7 @@ def parse_options |
145 |
|
146 |
def find_option(switch) |
147 |
self.class.find_option(switch) || |
148 |
- signal_usage_error("Unrecognised option '#{switch}'") |
149 |
+ signal_usage_error(Clamp.message(:unrecognised_option, :switch => switch)) |
150 |
end |
151 |
|
152 |
end |
153 |
diff --git a/lib/clamp/parameter/definition.rb b/lib/clamp/parameter/definition.rb |
154 |
index 6412546..a276dea 100644 |
155 |
--- lib/clamp/parameter/definition.rb |
156 |
+++ lib/clamp/parameter/definition.rb |
157 |
@@ -22,7 +22,7 @@ def help_lhs |
158 |
end |
159 |
|
160 |
def consume(arguments) |
161 |
- raise ArgumentError, "no value provided" if required? && arguments.empty? |
162 |
+ raise ArgumentError, Clamp.message(:no_value_provided) if required? && arguments.empty? |
163 |
arguments.shift(multivalued? ? arguments.length : 1) |
164 |
end |
165 |
|
166 |
diff --git a/lib/clamp/parameter/parsing.rb b/lib/clamp/parameter/parsing.rb |
167 |
index 8a70719..95aa422 100644 |
168 |
--- lib/clamp/parameter/parsing.rb |
169 |
+++ lib/clamp/parameter/parsing.rb |
170 |
@@ -13,7 +13,7 @@ def parse_parameters |
171 |
parameter.of(self).take(value) |
172 |
end |
173 |
rescue ArgumentError => e |
174 |
- signal_usage_error "parameter '#{parameter.name}': #{e.message}" |
175 |
+ signal_usage_error Clamp.message(:parameter_argument_error, :param => parameter.name, :message => e.message) |
176 |
end |
177 |
end |
178 |
|
179 |
diff --git a/lib/clamp/subcommand/execution.rb b/lib/clamp/subcommand/execution.rb |
180 |
index d15c56b..c341df6 100644 |
181 |
--- lib/clamp/subcommand/execution.rb |
182 |
+++ lib/clamp/subcommand/execution.rb |
183 |
@@ -25,7 +25,7 @@ def instatiate_subcommand(name) |
184 |
end |
185 |
|
186 |
def find_subcommand_class(name) |
187 |
- subcommand_def = self.class.find_subcommand(name) || signal_usage_error("No such sub-command '#{name}'") |
188 |
+ subcommand_def = self.class.find_subcommand(name) || signal_usage_error(Clamp.message(:no_such_subcommand, :name => name)) |
189 |
subcommand_def.subcommand_class |
190 |
end |
191 |
|