Skip to content

Commit a254e6e

Browse files
authored
Merge pull request #97 from DannyBen/add/required-catch-all
Add support for required `catch_all` arguments
2 parents 7ba21b6 + 2771da6 commit a254e6e

File tree

34 files changed

+251
-90
lines changed

34 files changed

+251
-90
lines changed

examples/catch-all-advanced/README.md

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,14 @@ commands:
4141
- name: upload
4242
short: u
4343
help: Upload a file
44-
args:
45-
- name: source
46-
required: true
47-
help: File to upload
4844

49-
flags:
50-
- long: --user
51-
short: -u
52-
arg: user
53-
help: Username to use for logging in
45+
# Configure catch_all for the `upload` sub-command using the extended
46+
# syntax, and specifying that `catch_all` is required (which means that at
47+
# least one extra argument must be provided)
48+
catch_all:
49+
label: Files
50+
help: Files to upload
5451
required: true
55-
- long: --password
56-
short: -p
57-
arg: password
58-
help: Password to use for logging in
5952
```
6053
6154
## Generated script output
@@ -158,5 +151,54 @@ other_args:
158151
159152
```
160153

154+
### `$ ./cli upload -h`
155+
156+
```shell
157+
cli upload - Upload a file
158+
159+
Shortcut: u
160+
161+
Usage:
162+
cli upload FILES...
163+
cli upload --help | -h
164+
165+
Options:
166+
--help, -h
167+
Show this help
168+
169+
Arguments:
170+
FILES...
171+
Files to upload
172+
173+
174+
175+
```
176+
177+
### `$ ./cli upload`
178+
179+
```shell
180+
missing required argument: FILES...
181+
usage: cli upload FILES...
182+
183+
184+
```
185+
186+
### `$ ./cli upload file1 "file 2" file3`
187+
188+
```shell
189+
# this file is located in 'src/upload_command.sh'
190+
# code for 'cli upload' goes here
191+
# you can edit it freely and regenerate (it will not be overwritten)
192+
args: none
193+
194+
other_args:
195+
- ${other_args[*]} = file1 file 2 file3
196+
- ${other_args[0]} = file1
197+
- ${other_args[1]} = file 2
198+
- ${other_args[2]} = file3
199+
200+
201+
```
202+
161203

162204

examples/catch-all-advanced/cli

Lines changed: 14 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
@@ -248,6 +238,7 @@ parse_requirements() {
248238

249239
esac
250240
done
241+
# :command.catch_all_filter
251242
# :command.default_assignments
252243
# :command.whitelist_filter
253244
}
@@ -313,6 +304,7 @@ cli_download_parse_requirements() {
313304

314305
esac
315306
done
307+
# :command.catch_all_filter
316308
# :command.default_assignments
317309
# :command.whitelist_filter
318310
}
@@ -338,66 +330,30 @@ cli_upload_parse_requirements() {
338330
# :command.command_filter
339331
action="upload"
340332
# :command.required_args_filter
341-
if [[ $1 && $1 != -* ]]; then
342-
args[source]=$1
343-
shift
344-
else
345-
printf "missing required argument: SOURCE\nusage: cli upload SOURCE [options]\n"
346-
exit 1
347-
fi
348333
# :command.required_flags_filter
349-
argstring="$*"
350-
if [[ "$argstring" != *--user* && "$argstring" != *-u* ]]; then
351-
printf "missing required flag: --user, -u USER\n"
352-
exit 1
353-
fi
354334
# :command.parse_requirements_while
355335
while [[ $# -gt 0 ]]; do
356336
key="$1"
357337
case "$key" in
358-
# :flag.case
359-
--user | -u )
360-
if [[ $2 ]]; then
361-
args[--user]="$2"
362-
shift
363-
shift
364-
else
365-
printf "%s\n" "--user requires an argument: --user, -u USER"
366-
exit 1
367-
fi
368-
;;
369-
370-
# :flag.case
371-
--password | -p )
372-
if [[ $2 ]]; then
373-
args[--password]="$2"
374-
shift
375-
shift
376-
else
377-
printf "%s\n" "--password requires an argument: --password, -p PASSWORD"
378-
exit 1
379-
fi
380-
;;
381-
382338

383339
-* )
384-
printf "invalid option: %s\n" "$key"
385-
exit 1
340+
other_args+=("$1")
341+
shift
386342
;;
387343

388344
* )
389345
# :command.parse_requirements_case
390-
if [[ ! ${args[source]} ]]; then
391-
args[source]=$1
346+
other_args+=("$1")
392347
shift
393-
else
394-
printf "invalid argument: %s\n" "$key"
395-
exit 1
396-
fi
397348
;;
398349

399350
esac
400351
done
352+
# :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
401357
# :command.default_assignments
402358
# :command.whitelist_filter
403359
}

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

examples/catch-all/download

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ parse_requirements() {
126126

127127
esac
128128
done
129+
# :command.catch_all_filter
129130
# :command.default_assignments
130131
# :command.whitelist_filter
131132
}

examples/colors/colorly

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ parse_requirements() {
159159

160160
esac
161161
done
162+
# :command.catch_all_filter
162163
# :command.default_assignments
163164
# :command.whitelist_filter
164165
}

examples/command-default/ftp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ parse_requirements() {
224224

225225
esac
226226
done
227+
# :command.catch_all_filter
227228
# :command.default_assignments
228229
# :command.whitelist_filter
229230
}
@@ -280,6 +281,7 @@ ftp_upload_parse_requirements() {
280281

281282
esac
282283
done
284+
# :command.catch_all_filter
283285
# :command.default_assignments
284286
# :command.whitelist_filter
285287
}
@@ -336,6 +338,7 @@ ftp_download_parse_requirements() {
336338

337339
esac
338340
done
341+
# :command.catch_all_filter
339342
# :command.default_assignments
340343
# :command.whitelist_filter
341344
}

examples/command-groups/ftp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ parse_requirements() {
297297

298298
esac
299299
done
300+
# :command.catch_all_filter
300301
# :command.default_assignments
301302
# :command.whitelist_filter
302303
}
@@ -353,6 +354,7 @@ ftp_download_parse_requirements() {
353354

354355
esac
355356
done
357+
# :command.catch_all_filter
356358
# :command.default_assignments
357359
# :command.whitelist_filter
358360
}
@@ -409,6 +411,7 @@ ftp_upload_parse_requirements() {
409411

410412
esac
411413
done
414+
# :command.catch_all_filter
412415
# :command.default_assignments
413416
# :command.whitelist_filter
414417
}
@@ -453,6 +456,7 @@ ftp_login_parse_requirements() {
453456

454457
esac
455458
done
459+
# :command.catch_all_filter
456460
# :command.default_assignments
457461
# :command.whitelist_filter
458462
}
@@ -497,6 +501,7 @@ ftp_logout_parse_requirements() {
497501

498502
esac
499503
done
504+
# :command.catch_all_filter
500505
# :command.default_assignments
501506
# :command.whitelist_filter
502507
}

examples/commands-nested/cli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ parse_requirements() {
367367

368368
esac
369369
done
370+
# :command.catch_all_filter
370371
# :command.default_assignments
371372
# :command.whitelist_filter
372373
}
@@ -437,6 +438,7 @@ cli_dir_parse_requirements() {
437438

438439
esac
439440
done
441+
# :command.catch_all_filter
440442
# :command.default_assignments
441443
# :command.whitelist_filter
442444
}
@@ -493,6 +495,7 @@ cli_dir_list_parse_requirements() {
493495

494496
esac
495497
done
498+
# :command.catch_all_filter
496499
# :command.default_assignments
497500
# :command.whitelist_filter
498501
}
@@ -555,6 +558,7 @@ cli_dir_remove_parse_requirements() {
555558

556559
esac
557560
done
561+
# :command.catch_all_filter
558562
# :command.default_assignments
559563
# :command.whitelist_filter
560564
}
@@ -625,6 +629,7 @@ cli_file_parse_requirements() {
625629

626630
esac
627631
done
632+
# :command.catch_all_filter
628633
# :command.default_assignments
629634
# :command.whitelist_filter
630635
}
@@ -681,6 +686,7 @@ cli_file_show_parse_requirements() {
681686

682687
esac
683688
done
689+
# :command.catch_all_filter
684690
# :command.default_assignments
685691
# :command.whitelist_filter
686692
}
@@ -737,6 +743,7 @@ cli_file_edit_parse_requirements() {
737743

738744
esac
739745
done
746+
# :command.catch_all_filter
740747
# :command.default_assignments
741748
# :command.whitelist_filter
742749
}

0 commit comments

Comments
 (0)