Skip to content

Commit 7417c24

Browse files
authored
Merge pull request #139 from DannyBen/fix/required-arg-filter
Allow required args to appear after flags
2 parents b42d85e + 2cf5fd4 commit 7417c24

File tree

8 files changed

+94
-11
lines changed

8 files changed

+94
-11
lines changed

lib/bashly/views/command/parse_requirements.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ parse_requirements() {
88
<%= render(:environment_variables_filter).indent 2 %>
99
<%= render(:dependencies_filter).indent 2 %>
1010
<%= render(:command_filter).indent 2 %>
11+
<%= render(:parse_requirements_while).indent 2 %>
1112
<%= render(:required_args_filter).indent 2 %>
1213
<%= render(:required_flags_filter).indent 2 %>
13-
<%= render(:parse_requirements_while).indent 2 %>
1414
<%= render(:catch_all_filter).indent 2 %>
1515
<%= render(:default_assignments).indent 2 %>
1616
<%= render(:whitelist_filter).indent 2 %>
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
# :command.required_args_filter
22
% required_args.each do |arg|
3-
if [[ -n ${1+x} && $1 != -* ]]; then
4-
<%= arg.render(:validations).indent 2 %>
5-
args[<%= arg.name %>]=$1
6-
shift
7-
else
3+
if [[ -z ${args[<%= arg.name %>]+x} ]]; then
84
printf "<%= strings[:missing_required_argument] % { arg: arg.name.upcase, usage: usage_string } %>\n"
95
exit 1
106
fi
11-
127
% end

lib/bashly/views/command/required_flags_filter.erb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# :command.required_flags_filter
2-
% if required_flags.any?
3-
argstring="$*"
4-
% end
52
% required_flags.each do |flag|
6-
if [[ <%= flag.aliases.map { |a| %Q["$argstring" != *#{a}*] }.join " && " %> ]]; then
3+
if [[ -z ${args[<%= flag.long %>]+x} ]]; then
74
printf "<%= strings[:missing_required_flag] % { usage: flag.usage_string } %>\n"
85
exit 1
96
fi
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
+ bundle exec bashly generate
2+
creating user files in src
3+
created src/initialize.sh
4+
created src/root_command.sh
5+
created ./download
6+
run ./download --help to test your bash script
7+
+ ./download
8+
missing required argument: PROTOCOL
9+
usage: download PROTOCOL PORT [BODY] [options]
10+
+ ./download http
11+
missing required argument: PORT
12+
usage: download PROTOCOL PORT [BODY] [options]
13+
+ ./download http 3000
14+
missing required flag: --method NAME
15+
+ ./download http 3000 --method GET
16+
# this file is located in 'src/root_command.sh'
17+
# you can edit it freely and regenerate (it will not be overwritten)
18+
args:
19+
- ${args[--method]} = GET
20+
- ${args[port]} = 3000
21+
- ${args[protocol]} = http
22+
+ ./download http 3000 --method GET --role admin
23+
# this file is located in 'src/root_command.sh'
24+
# you can edit it freely and regenerate (it will not be overwritten)
25+
args:
26+
- ${args[--method]} = GET
27+
- ${args[port]} = 3000
28+
- ${args[protocol]} = http
29+
- ${args[--role]} = admin
30+
+ ./download http --role admin --method GET 3000
31+
# this file is located in 'src/root_command.sh'
32+
# you can edit it freely and regenerate (it will not be overwritten)
33+
args:
34+
- ${args[--method]} = GET
35+
- ${args[port]} = 3000
36+
- ${args[protocol]} = http
37+
- ${args[--role]} = admin
38+
+ ./download --role admin --method GET http 3000
39+
# this file is located in 'src/root_command.sh'
40+
# you can edit it freely and regenerate (it will not be overwritten)
41+
args:
42+
- ${args[--method]} = GET
43+
- ${args[port]} = 3000
44+
- ${args[protocol]} = http
45+
- ${args[--role]} = admin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
download
2+
src/*.sh
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This fixture ensures that required args and flags are allowed to appear after
2+
the optional flags.
3+
Reference issue: https://github.com/DannyBen/bashly/issues/124
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: download
2+
3+
args:
4+
- name: protocol
5+
help: Protocol to use
6+
required: true
7+
- name: port
8+
help: Port to use
9+
required: true
10+
- name: body
11+
help: Message body
12+
13+
flags:
14+
- long: --method
15+
help: HTTP method
16+
arg: name
17+
required: true
18+
- long: --role
19+
help: role
20+
arg: name
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
# This fixture ensures that required args and flags are allowed to appear after
4+
# the optional flags.
5+
# Reference issue: https://github.com/DannyBen/bashly/issues/124
6+
7+
rm -f ./src/*.sh
8+
rm -f ./download
9+
10+
set -x
11+
12+
bundle exec bashly generate
13+
14+
./download
15+
./download http
16+
./download http 3000
17+
./download http 3000 --method GET
18+
./download http 3000 --method GET --role admin
19+
./download http --role admin --method GET 3000
20+
./download --role admin --method GET http 3000
21+

0 commit comments

Comments
 (0)