Skip to content

Commit c200223

Browse files
committed
- Improve handling of unique repeatable args and flag args
1 parent 5f4ea75 commit c200223

File tree

5 files changed

+53
-63
lines changed

5 files changed

+53
-63
lines changed

lib/bashly/views/command/parse_requirements_case_repeatable.gtx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
condition = "if"
44
args.each do |arg|
5-
> escaped_value="${1//\"/\\\"}"
5+
> escaped="$(printf '%q' "$1")"
66
> {{ condition }} [[ -z ${args['{{ arg.name }}']+x} ]]; then
77
if arg.repeatable
8-
> args['{{ arg.name }}']="\"$escaped_value\""
8+
> args['{{ arg.name }}']="$escaped"
99
> shift
1010
if arg.unique
11-
> elif [[ ! "${args['{{ arg.name }}']}" =~ \"$1\" ]]; then
12-
> args['{{ arg.name }}']="${args[{{ arg.name }}]} \"$escaped_value\""
11+
> elif [[ -z "${unique_lookup["{{ arg.name }}:$escaped"]:-}" ]]; then
12+
> args['{{ arg.name }}']="${args[{{ arg.name }}]} $escaped"
13+
# TODO: This line should only be inserted if any uniques are used
14+
> unique_lookup["{{ arg.name }}:$escaped"]=1
1315
> shift
1416
> else
1517
> shift
1618
else
1719
> else
18-
> args['{{ arg.name }}']="${args[{{ arg.name }}]} \"$escaped_value\""
20+
> args['{{ arg.name }}']="${args[{{ arg.name }}]} $escaped_value"
1921
> shift
2022
end
2123

@@ -28,4 +30,4 @@ args.each do |arg|
2830
end
2931

3032
> fi
31-
>
33+
>

lib/bashly/views/command/run.gtx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
> declare -a other_args=()
77
> declare -a env_var_names=()
88
> declare -a input=()
9+
# TODO: This line should only be inserted if any uniques are used
10+
> declare -A unique_lookup=()
911
> normalize_input "$@"
1012
> parse_requirements "${input[@]}"
1113
if user_file_exist?('before')
@@ -29,5 +31,3 @@ if user_file_exist?('after')
2931
end
3032

3133
> }
32-
33-

lib/bashly/views/flag/case_arg.gtx

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

55
if repeatable
6-
> escaped_value="${2//\"/\\\"}"
6+
> escaped="$(printf '%q' "$2")"
77
> if [[ -z ${args['{{ name }}']+x} ]]; then
8-
> args['{{ name }}']="\"$escaped_value\""
8+
> args['{{ name }}']="$escaped"
99
if unique
10-
> elif [[ ! "${args['{{ name }}']}" =~ \"$escaped_value\" ]]; then
10+
> elif [[ -z "${unique_lookup["{{ name }}:${escaped}"]:-}" ]]; then
1111
else
1212
> else
1313
end
14-
> args['{{ name }}']="${args['{{ name }}']} \"$escaped_value\""
14+
> args['{{ name }}']="${args['{{ name }}']} $escaped"
1515
> fi
16+
# TODO: This line should only be inserted if any uniques are used
17+
> unique_lookup["{{ name }}:${escaped}"]=1
1618

1719
else
1820
> args['{{ name }}']="$2"
@@ -26,4 +28,4 @@ end
2628
> exit 1
2729
> fi
2830
> ;;
29-
>
31+
>

spec/approvals/fixtures/repeatable-escaping

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,45 @@ run ./cli --help to test your bash script
66
+ ./cli
77
args: none
88

9-
+ ./cli one 'two and a half' three
9+
+ ./cli before 'x x' 'x x' after
1010
args:
11-
- ${args[term]} = "one" "two and a half" "three"
12-
[TERM] --> one
13-
[TERM] --> two and a half
14-
[TERM] --> three
11+
- ${args[term]} = before x\ x after
12+
[TERM] --> before
13+
[TERM] --> x x
14+
[TERM] --> after
1515

16-
+ ./cli one 'the phrase "hello world"' three
16+
+ ./cli before 'x "x x"' 'x "x x"' after
1717
args:
18-
- ${args[term]} = "one" "the phrase \"hello world\"" "three"
19-
[TERM] --> one
20-
[TERM] --> the phrase "hello world"
21-
[TERM] --> three
18+
- ${args[term]} = before x\ \"x\ x\" after
19+
[TERM] --> before
20+
[TERM] --> x "x x"
21+
[TERM] --> after
2222

23-
+ ./cli one 'bad"quote' three
23+
+ ./cli before 'x"x' 'x"x' after
2424
args:
25-
- ${args[term]} = "one" "bad\"quote" "three"
26-
[TERM] --> one
27-
[TERM] --> bad"quote
28-
[TERM] --> three
25+
- ${args[term]} = before x\"x after
26+
[TERM] --> before
27+
[TERM] --> x"x
28+
[TERM] --> after
2929

30-
+ ./cli one 'only once' 'only once'
30+
+ ./cli -s before -s 'x x' -s 'x x' -s after
3131
args:
32-
- ${args[term]} = "one" "only once"
33-
[TERM] --> one
34-
[TERM] --> only once
32+
- ${args[--search]} = before x\ x after
33+
[--serach] --> before
34+
[--serach] --> x x
35+
[--serach] --> after
3536

36-
+ ./cli -s one -s 'two and a half' -s three
37+
+ ./cli -s before -s 'x "x x"' -s 'x "x x"' -s after
3738
args:
38-
- ${args[--search]} = "one" "two and a half" "three"
39-
[--serach] --> one
40-
[--serach] --> two and a half
41-
[--serach] --> three
39+
- ${args[--search]} = before x\ \"x\ x\" after
40+
[--serach] --> before
41+
[--serach] --> x "x x"
42+
[--serach] --> after
4243

43-
+ ./cli -s one -s 'the phrase "hello world"' -s three
44+
+ ./cli -s before -s 'x"x' -s 'x"x' -s after
4445
args:
45-
- ${args[--search]} = "one" "the phrase \"hello world\"" "three"
46-
[--serach] --> one
47-
[--serach] --> the phrase "hello world"
48-
[--serach] --> three
49-
50-
+ ./cli -s one -s 'bad"quote' -s three
51-
args:
52-
- ${args[--search]} = "one" "bad\"quote" "three"
53-
[--serach] --> one
54-
[--serach] --> bad"quote
55-
[--serach] --> three
56-
57-
+ ./cli -s one -s 'only once' -s 'only once'
58-
args:
59-
- ${args[--search]} = "one" "only once"
60-
[--serach] --> one
61-
[--serach] --> only once
46+
- ${args[--search]} = before x\"x after
47+
[--serach] --> before
48+
[--serach] --> x"x
49+
[--serach] --> after
6250

spec/fixtures/workspaces/repeatable-escaping/test.sh

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ set -x
77
bundle exec bashly generate
88

99
./cli
10-
./cli one "two and a half" three
11-
./cli one 'the phrase "hello world"' three
12-
./cli one 'bad"quote' three
13-
./cli one 'only once' 'only once'
14-
./cli -s one -s "two and a half" -s three
15-
./cli -s one -s 'the phrase "hello world"' -s three
16-
./cli -s one -s 'bad"quote' -s three
17-
./cli -s one -s 'only once' -s 'only once'
10+
./cli before "x x" "x x" after
11+
./cli before 'x "x x"' 'x "x x"' after
12+
./cli before 'x"x' 'x"x' after
13+
./cli -s before -s "x x" -s "x x" -s after
14+
./cli -s before -s 'x "x x"' -s 'x "x x"' -s after
15+
./cli -s before -s 'x"x' -s 'x"x' -s after

0 commit comments

Comments
 (0)