Skip to content

Commit ab34fc0

Browse files
committed
- Fix repeatable flag args when the input has quotes
1 parent 0d35217 commit ab34fc0

File tree

13 files changed

+114
-7
lines changed

13 files changed

+114
-7
lines changed

lib/bashly/views/flag/case_arg.gtx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
> if [[ -n ${2+x} ]]; then
44

55
if repeatable
6+
> escaped_value="${2//\"/\\\"}"
67
> if [[ -z ${args['{{ name }}']+x} ]]; then
7-
> args['{{ name }}']="\"$2\""
8+
> args['{{ name }}']="\"$escaped_value\""
89
if unique
9-
> elif [[ ! "${args['{{ name }}']}" =~ \"$2\" ]]; then
10+
> elif [[ ! "${args['{{ name }}']}" =~ \"$escaped_value\" ]]; then
1011
else
1112
> else
1213
end
13-
> args['{{ name }}']="${args['{{ name }}']} \"$2\""
14+
> args['{{ name }}']="${args['{{ name }}']} \"$escaped_value\""
1415
> fi
1516

1617
else

spec/approvals/fixtures/repeatable-arg-validations

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ must be an integer
3232
+ ./cli 1 a
3333
validation error in NUMBER:
3434
must be an integer
35+
+ ./cli 1 'not"a"number'
36+
validation error in NUMBER:
37+
must be an integer
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
+ bundle exec bashly generate
2+
creating user files in src
3+
skipped src/root_command.sh (exists)
4+
created ./cli
5+
run ./cli --help to test your bash script
6+
+ ./cli
7+
args: none
8+
9+
+ ./cli one 'two and a half' three
10+
args:
11+
- ${args[term]} = "one" "two and a half" "three"
12+
[TERM] --> one
13+
[TERM] --> two and a half
14+
[TERM] --> three
15+
16+
+ ./cli one 'the phrase "hello world"' three
17+
args:
18+
- ${args[term]} = "one" "the phrase \"hello world\"" "three"
19+
[TERM] --> one
20+
[TERM] --> the phrase "hello world"
21+
[TERM] --> three
22+
23+
+ ./cli one 'bad"quote' three
24+
args:
25+
- ${args[term]} = "one" "bad\"quote" "three"
26+
[TERM] --> one
27+
[TERM] --> bad"quote
28+
[TERM] --> three
29+
30+
+ ./cli -s one -s 'two and a half' -s three
31+
args:
32+
- ${args[--search]} = "one" "two and a half" "three"
33+
[--serach] --> one
34+
[--serach] --> two and a half
35+
[--serach] --> three
36+
37+
+ ./cli -s one -s 'the phrase "hello world"' -s three
38+
args:
39+
- ${args[--search]} = "one" "the phrase \"hello world\"" "three"
40+
[--serach] --> one
41+
[--serach] --> the phrase "hello world"
42+
[--serach] --> three
43+
44+
+ ./cli -s one -s 'bad"quote' -s three
45+
args:
46+
- ${args[--search]} = "one" "bad\"quote" "three"
47+
[--serach] --> one
48+
[--serach] --> bad"quote
49+
[--serach] --> three
50+

spec/approvals/fixtures/repeatable-flag-validations

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ must be an integer
3232
+ ./cli --number 1 --number a
3333
validation error in --number, -n NUMBER:
3434
must be an integer
35+
+ ./cli --number 1 --number 'not"a"number'
36+
validation error in --number, -n NUMBER:
37+
must be an integer

spec/approvals/fixtures/repeatable-whitelist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ args:
2424
--protocol must be one of: ssh, ftp, sftp
2525
+ ./download --protocol telnet
2626
--protocol must be one of: ssh, ftp, sftp
27+
+ ./download --protocol telnet --protocol 'not"a"protocol'
28+
--protocol must be one of: ssh, ftp, sftp

spec/fixtures/workspaces/repeatable-arg-validations/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ bundle exec bashly generate
1616
./cli 1.1 2
1717
./cli 1 2.2
1818
./cli 1 a
19+
./cli 1 'not"a"number'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This fixture tests that repeatable args and flag args with quotes do not break.
2+
Reference issue: https://github.com/DannyBen/bashly/issues/503
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: cli
2+
help: Test repeatable args with input that contains quotes
3+
version: 0.1.0
4+
5+
flags:
6+
- long: --search
7+
short: -s
8+
arg: query
9+
repeatable: true
10+
11+
args:
12+
- name: term
13+
repeatable: true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
inspect_args
2+
3+
if [[ -v args[term] ]]; then
4+
eval "terms=(${args[term]})"
5+
for t in "${terms[@]}"; do
6+
echo "[TERM] --> ${t}"
7+
done
8+
fi
9+
10+
if [[ -v args[--search] ]]; then
11+
eval "terms=(${args[--search]})"
12+
for t in "${terms[@]}"; do
13+
echo "[--serach] --> ${t}"
14+
done
15+
fi
16+
17+
echo

0 commit comments

Comments
 (0)