Skip to content

Commit 12be30f

Browse files
committed
specs for new catch_all updates
1 parent 6f34e27 commit 12be30f

File tree

7 files changed

+120
-82
lines changed

7 files changed

+120
-82
lines changed

examples/catch-all-advanced/cli

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ cli_upload_usage() {
112112
echo
113113

114114
printf "Usage:\n"
115-
printf " cli upload SOURCE [options]\n"
115+
printf " cli upload FILES...\n"
116116
printf " cli upload --help | -h\n"
117117
echo
118118

@@ -122,22 +122,12 @@ cli_upload_usage() {
122122
echo " --help, -h"
123123
printf " Show this help\n"
124124
echo
125-
# :command.usage_flags
126-
# :flag.usage
127-
echo " --user, -u USER (required)"
128-
printf " Username to use for logging in\n"
129-
echo
130-
131-
# :flag.usage
132-
echo " --password, -p PASSWORD"
133-
printf " Password to use for logging in\n"
134-
echo
125+
135126
# :command.usage_args
136127
printf "Arguments:\n"
137128

138-
# :argument.usage
139-
echo " SOURCE"
140-
printf " File to upload\n"
129+
echo " FILES..."
130+
printf " Files to upload\n"
141131
echo
142132

143133
fi
@@ -340,67 +330,30 @@ cli_upload_parse_requirements() {
340330
# :command.command_filter
341331
action="upload"
342332
# :command.required_args_filter
343-
if [[ $1 && $1 != -* ]]; then
344-
args[source]=$1
345-
shift
346-
else
347-
printf "missing required argument: SOURCE\nusage: cli upload SOURCE [options]\n"
348-
exit 1
349-
fi
350333
# :command.required_flags_filter
351-
argstring="$*"
352-
if [[ "$argstring" != *--user* && "$argstring" != *-u* ]]; then
353-
printf "missing required flag: --user, -u USER\n"
354-
exit 1
355-
fi
356334
# :command.parse_requirements_while
357335
while [[ $# -gt 0 ]]; do
358336
key="$1"
359337
case "$key" in
360-
# :flag.case
361-
--user | -u )
362-
if [[ $2 ]]; then
363-
args[--user]="$2"
364-
shift
365-
shift
366-
else
367-
printf "%s\n" "--user requires an argument: --user, -u USER"
368-
exit 1
369-
fi
370-
;;
371-
372-
# :flag.case
373-
--password | -p )
374-
if [[ $2 ]]; then
375-
args[--password]="$2"
376-
shift
377-
shift
378-
else
379-
printf "%s\n" "--password requires an argument: --password, -p PASSWORD"
380-
exit 1
381-
fi
382-
;;
383-
384338

385339
-* )
386-
printf "invalid option: %s\n" "$key"
387-
exit 1
340+
other_args+=("$1")
341+
shift
388342
;;
389343

390344
* )
391345
# :command.parse_requirements_case
392-
if [[ ! ${args[source]} ]]; then
393-
args[source]=$1
346+
other_args+=("$1")
394347
shift
395-
else
396-
printf "invalid argument: %s\n" "$key"
397-
exit 1
398-
fi
399348
;;
400349

401350
esac
402351
done
403352
# :command.catch_all_filter
353+
if [[ ${#other_args[@]} -eq 0 ]]; then
354+
printf "missing required argument: FILES...\nusage: cli upload FILES...\n"
355+
exit 1
356+
fi
404357
# :command.default_assignments
405358
# :command.whitelist_filter
406359
}

examples/catch-all-advanced/src/bashly.yml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,11 @@ commands:
3232
- name: upload
3333
short: u
3434
help: Upload a file
35-
args:
36-
- name: source
37-
required: true
38-
help: File to upload
3935

40-
flags:
41-
- long: --user
42-
short: -u
43-
arg: user
44-
help: Username to use for logging in
36+
# Configure catch_all for the `upload` sub-command using the extended
37+
# syntax, and specifying that `catch_all` is required (which means that at
38+
# least one extra argument must be provided)
39+
catch_all:
40+
label: Files
41+
help: Files to upload
4542
required: true
46-
- long: --password
47-
short: -p
48-
arg: password
49-
help: Password to use for logging in

examples/catch-all-advanced/test.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ bashly generate
1212
./cli download -h
1313
./cli download source
1414
./cli download source target
15-
./cli download source target and --additional stuff
15+
./cli download source target and --additional stuff
16+
17+
./cli upload -h
18+
./cli upload
19+
./cli upload file1 "file 2" file3

lib/bashly/models/command.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ def catch_all_label
4343
end
4444
end
4545

46-
# Returns a string suitable for catch all usage text
47-
def catch_all_usage
48-
catch_all_required? ? catch_all_label : "[#{catch_all_label}]"
49-
end
50-
5146
# Returns a used defined help string for the catch_all directive
5247
def catch_all_help
5348
return nil unless catch_all
@@ -64,6 +59,12 @@ def catch_all_required?
6459
catch_all.is_a?(Hash) and catch_all['required']
6560
end
6661

62+
# Returns a string suitable for catch_all Usage pattern
63+
def catch_all_usage
64+
return nil unless catch_all
65+
catch_all_required? ? catch_all_label : "[#{catch_all_label}]"
66+
end
67+
6768
# Returns only the names of the Commands
6869
def command_names
6970
commands.map &:name
@@ -200,7 +201,7 @@ def usage_string
200201
result << arg.usage_string
201202
end
202203
result << "[options]" unless flags.empty?
203-
result << "#{catch_all_usage}" if catch_all
204+
result << catch_all_usage if catch_all
204205
result.join " "
205206
end
206207

spec/approvals/examples/catch-all-advanced

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,34 @@ other_args:
7373
- ${other_args[0]} = and
7474
- ${other_args[1]} = --additional
7575
- ${other_args[2]} = stuff
76+
+ ./cli upload -h
77+
cli upload - Upload a file
78+
79+
Shortcut: u
80+
81+
Usage:
82+
cli upload FILES...
83+
cli upload --help | -h
84+
85+
Options:
86+
--help, -h
87+
Show this help
88+
89+
Arguments:
90+
FILES...
91+
Files to upload
92+
93+
+ ./cli upload
94+
missing required argument: FILES...
95+
usage: cli upload FILES...
96+
+ ./cli upload file1 'file 2' file3
97+
# this file is located in 'src/upload_command.sh'
98+
# code for 'cli upload' goes here
99+
# you can edit it freely and regenerate (it will not be overwritten)
100+
args: none
101+
102+
other_args:
103+
- ${other_args[*]} = file1 file 2 file3
104+
- ${other_args[0]} = file1
105+
- ${other_args[1]} = file 2
106+
- ${other_args[2]} = file3

spec/bashly/models/command_spec.rb

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@
7272
let(:fixture) { :catch_all_string }
7373

7474
it "returns an uppercase version of it" do
75-
expect(subject.catch_all_label).to eq "EXTRA PARAMS..."
75+
expect(subject.catch_all_label).to eq "EXTRA_PARAMS..."
7676
end
7777
end
7878

7979
context "when catch_all['label'] is a string" do
8080
let(:fixture) { :catch_all_hash }
8181

8282
it "returns an uppercase version of it" do
83-
expect(subject.catch_all_label).to eq "ADDITIONAL PARAMS..."
83+
expect(subject.catch_all_label).to eq "ADDITIONAL_PARAMS..."
8484
end
8585
end
8686

@@ -115,7 +115,62 @@
115115
expect(subject.catch_all_help).to be_nil
116116
end
117117
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
118143

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
119174
end
120175

121176
describe '#command_names' do

spec/fixtures/models/commands.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,15 @@
119119
:catch_all_string:
120120
name: get
121121
help: get something from somewhere
122-
catch_all: extra params
122+
catch_all: extra_params
123123

124124
:catch_all_hash:
125125
name: get
126126
help: get something from somewhere
127127
catch_all:
128-
label: additional params
128+
label: additional_params
129129
help: Any additional argument or flag
130+
required: true
130131

131132
:completions_simple:
132133
name: get

0 commit comments

Comments
 (0)