Skip to content

Commit e202478

Browse files
authored
Merge pull request #161 from DannyBen/refactor/catch-all
Refactor catch_all (internal)
2 parents 7beb22b + 9bda55c commit e202478

File tree

9 files changed

+184
-152
lines changed

9 files changed

+184
-152
lines changed

lib/bashly/script/catch_all.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Bashly
2+
module Script
3+
class CatchAll
4+
class << self
5+
def from_config(config)
6+
options = case config
7+
when nil
8+
{ enabled: false }
9+
when String
10+
{ label: config }
11+
when Hash
12+
{ label: config['label'], help: config['help'], required: config['required'] }
13+
else
14+
{}
15+
end
16+
17+
new **options
18+
end
19+
end
20+
21+
def initialize(label: nil, help: nil, required: false, enabled: true)
22+
@label, @help, @required, @enabled = label, help, required, enabled
23+
end
24+
25+
def enabled?
26+
@enabled
27+
end
28+
29+
def label
30+
enabled? ? "#{@label&.upcase}..." : nil
31+
end
32+
33+
def help
34+
enabled? ? @help : nil
35+
end
36+
37+
def required?
38+
@required
39+
end
40+
41+
def usage_string
42+
return nil unless enabled?
43+
required? ? label : "[#{label}]"
44+
end
45+
46+
end
47+
end
48+
end
49+

lib/bashly/script/command.rb

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,8 @@ def caption_string
3030
help ? "#{full_name} - #{summary}" : full_name
3131
end
3232

33-
# Returns a label for the catch_all directive
34-
def catch_all_label
35-
case catch_all
36-
when nil then nil
37-
when String then "#{catch_all.upcase}..."
38-
when Hash then "#{catch_all['label'].upcase}..."
39-
else "..."
40-
end
41-
end
42-
43-
# Returns a user defined help string for the catch_all directive
44-
def catch_all_help
45-
return nil unless catch_all
46-
47-
if catch_all.is_a?(Hash) and catch_all['help'].is_a?(String)
48-
catch_all['help']
49-
else
50-
nil
51-
end
52-
end
53-
54-
# Returns true if catch_all is required
55-
def catch_all_required?
56-
catch_all.is_a?(Hash) and catch_all['required']
57-
end
58-
59-
# Returns a string suitable for catch_all Usage pattern
60-
def catch_all_usage
61-
return nil unless catch_all
62-
catch_all_required? ? catch_all_label : "[#{catch_all_label}]"
33+
def catch_all
34+
@catch_all ||= CatchAll.from_config options['catch_all']
6335
end
6436

6537
# Returns only the names of the Commands
@@ -198,7 +170,7 @@ def usage_string
198170
result << arg.usage_string
199171
end
200172
result << "[options]" unless flags.empty?
201-
result << catch_all_usage if catch_all
173+
result << catch_all.usage_string if catch_all.enabled?
202174
result.join " "
203175
end
204176

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# :command.catch_all_filter
2-
% if catch_all_required?
2+
% if catch_all.required?
33
if [[ ${#other_args[@]} -eq 0 ]]; then
4-
printf "<%= strings[:missing_required_argument] % { arg: catch_all_label, usage: usage_string } %>\n"
4+
printf "<%= strings[:missing_required_argument] % { arg: catch_all.label, usage: usage_string } %>\n"
55
exit 1
66
fi
77
% end

lib/bashly/views/command/parse_requirements_case.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
% condition = "elif"
1010
% end
1111
else
12-
% if catch_all
12+
% if catch_all.enabled?
1313
other_args+=("$1")
1414
shift
1515
% else
1616
printf "<%= strings[:invalid_argument] %>\n" "$key"
1717
exit 1
1818
% end
1919
fi
20-
% elsif catch_all
20+
% elsif catch_all.enabled?
2121
other_args+=("$1")
2222
shift
2323
% else

lib/bashly/views/command/parse_requirements_while.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ while [[ $# -gt 0 ]]; do
88
% end
99

1010
-?* )
11-
% if catch_all
11+
% if catch_all.enabled?
1212
other_args+=("$1")
1313
shift
1414
;;

lib/bashly/views/command/usage.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
printf "<%= strings[:options] %>\n"
3838
<%= render(:usage_fixed_flags).indent 4 %>
3939
<%= render(:usage_flags).indent 4 if flags.any? %>
40-
<%= render(:usage_args).indent 4 if args.any? or catch_all_help %>
40+
<%= render(:usage_args).indent 4 if args.any? or catch_all.help %>
4141
<%= render(:usage_environment_variables).indent 4 if environment_variables.any? %>
4242
<%= render(:usage_examples).indent 4 if examples %>
4343
<%= render(:footer).indent 4 if footer %>

lib/bashly/views/command/usage_args.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ printf "<%= strings[:arguments] %>\n"
66
<%= arg.render(:usage) %>
77
% end
88
% end
9-
% if catch_all_help
9+
% if catch_all.help
1010

11-
echo " <%= catch_all_label %>"
12-
printf "<%= catch_all_help.wrap(76).indent(4).sanitize_for_print %>\n"
11+
echo " <%= catch_all.label %>"
12+
printf "<%= catch_all.help.wrap(76).indent(4).sanitize_for_print %>\n"
1313
echo
1414
% end

spec/bashly/script/catch_all_spec.rb

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
require 'spec_helper'
2+
3+
describe Script::CatchAll do
4+
let(:fixture) { :basic_command }
5+
6+
subject do
7+
options = load_fixture('script/commands')[fixture]
8+
Script::Command.new(options).catch_all
9+
end
10+
11+
describe '#label' do
12+
context "when catch_all is disabled" do
13+
it "returns nil" do
14+
expect(subject.label).to be_nil
15+
end
16+
end
17+
18+
context "when catch_all is a string" do
19+
let(:fixture) { :catch_all_string }
20+
21+
it "returns an uppercase version of it" do
22+
expect(subject.label).to eq "EXTRA_PARAMS..."
23+
end
24+
end
25+
26+
context "when catch_all is set" do
27+
let(:fixture) { :catch_all_hash }
28+
29+
it "returns an uppercase version of it" do
30+
expect(subject.label).to eq "ADDITIONAL_PARAMS..."
31+
end
32+
end
33+
34+
context "in other cases" do
35+
let(:fixture) { :catch_all }
36+
37+
it "returns '...'" do
38+
expect(subject.label).to eq "..."
39+
end
40+
end
41+
end
42+
43+
describe '#help' do
44+
context "when catch_all is disabled" do
45+
it "returns nil" do
46+
expect(subject.help).to be_nil
47+
end
48+
end
49+
50+
context "when catch_all is a hash" do
51+
let(:fixture) { :catch_all_hash }
52+
53+
it "returns an uppercase version of its label" do
54+
expect(subject.help).to eq "Any additional argument or flag"
55+
end
56+
end
57+
58+
context "in other cases" do
59+
let(:fixture) { :catch_all_string }
60+
61+
it "returns nil" do
62+
expect(subject.help).to be_nil
63+
end
64+
end
65+
end
66+
67+
describe '#required?' do
68+
context "when catch_all is disabled" do
69+
it "returns false" do
70+
expect(subject.required?).to be false
71+
end
72+
end
73+
74+
context "when catch_all['required'] is true" do
75+
let(:fixture) { :catch_all_hash }
76+
77+
it "returns true" do
78+
expect(subject.required?).to be true
79+
end
80+
end
81+
82+
context "in other cases" do
83+
let(:fixture) { :catch_all_string }
84+
85+
it "returns false" do
86+
expect(subject.required?).to be false
87+
end
88+
end
89+
end
90+
91+
describe '#usage_string' do
92+
context "when catch_all is disabled" do
93+
it "returns nil" do
94+
expect(subject.usage_string).to be_nil
95+
end
96+
end
97+
98+
context "when catch_all['required'] is true" do
99+
let(:fixture) { :catch_all_hash }
100+
101+
it "returns a usage help without []" do
102+
expect(subject.usage_string).to eq "ADDITIONAL_PARAMS..."
103+
end
104+
end
105+
106+
context "when catch_all['required'] is false" do
107+
let(:fixture) { :catch_all_string }
108+
109+
it "returns a usage help with []" do
110+
expect(subject.usage_string).to eq "[EXTRA_PARAMS...]"
111+
end
112+
end
113+
114+
context "when catch_all is true" do
115+
let(:fixture) { :catch_all }
116+
117+
it "returns [...]" do
118+
expect(subject.usage_string).to eq "[...]"
119+
end
120+
end
121+
end
122+
123+
end

spec/bashly/script/command_spec.rb

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -61,118 +61,6 @@
6161
end
6262
end
6363

64-
describe '#catch_all_label' do
65-
context "when catch_all is disabled" do
66-
it "returns nil" do
67-
expect(subject.catch_all_label).to be_nil
68-
end
69-
end
70-
71-
context "when catch_all is a string" do
72-
let(:fixture) { :catch_all_string }
73-
74-
it "returns an uppercase version of it" do
75-
expect(subject.catch_all_label).to eq "EXTRA_PARAMS..."
76-
end
77-
end
78-
79-
context "when catch_all['label'] is a string" do
80-
let(:fixture) { :catch_all_hash }
81-
82-
it "returns an uppercase version of it" do
83-
expect(subject.catch_all_label).to eq "ADDITIONAL_PARAMS..."
84-
end
85-
end
86-
87-
context "in other cases" do
88-
let(:fixture) { :catch_all }
89-
90-
it "returns '...'" do
91-
expect(subject.catch_all_label).to eq "..."
92-
end
93-
end
94-
end
95-
96-
describe '#catch_all_help' do
97-
context "when catch_all is disabled" do
98-
it "returns nil" do
99-
expect(subject.catch_all_help).to be_nil
100-
end
101-
end
102-
103-
context "when catch_all['help'] is a string" do
104-
let(:fixture) { :catch_all_hash }
105-
106-
it "returns it" do
107-
expect(subject.catch_all_help).to eq "Any additional argument or flag"
108-
end
109-
end
110-
111-
context "in other cases" do
112-
let(:fixture) { :catch_all_string }
113-
114-
it "returns nil" do
115-
expect(subject.catch_all_help).to be_nil
116-
end
117-
end
118-
end
119-
120-
describe '#catch_all_required?' do
121-
context "when catch_all is disabled" do
122-
it "returns false" do
123-
expect(subject.catch_all_required?).to be false
124-
end
125-
end
126-
127-
context "when catch_all['required'] is true" do
128-
let(:fixture) { :catch_all_hash }
129-
130-
it "returns true" do
131-
expect(subject.catch_all_required?).to be true
132-
end
133-
end
134-
135-
context "in other cases" do
136-
let(:fixture) { :catch_all_string }
137-
138-
it "returns false" do
139-
expect(subject.catch_all_required?).to be false
140-
end
141-
end
142-
end
143-
144-
describe '#catch_all_usage' do
145-
context "when catch_all is disabled" do
146-
it "returns nil" do
147-
expect(subject.catch_all_usage).to be_nil
148-
end
149-
end
150-
151-
context "when catch_all['required'] is true" do
152-
let(:fixture) { :catch_all_hash }
153-
154-
it "returns a usage help without []" do
155-
expect(subject.catch_all_usage).to eq "ADDITIONAL_PARAMS..."
156-
end
157-
end
158-
159-
context "when catch_all['required'] is false" do
160-
let(:fixture) { :catch_all_string }
161-
162-
it "returns a usage help with []" do
163-
expect(subject.catch_all_usage).to eq "[EXTRA_PARAMS...]"
164-
end
165-
end
166-
167-
context "when catch_all is true" do
168-
let(:fixture) { :catch_all }
169-
170-
it "returns [...]" do
171-
expect(subject.catch_all_usage).to eq "[...]"
172-
end
173-
end
174-
end
175-
17664
describe '#command_names' do
17765
let(:fixture) { :docker }
17866

0 commit comments

Comments
 (0)