Skip to content

Commit d557687

Browse files
committed
declare unique_lookup conditionally
1 parent c200223 commit d557687

File tree

9 files changed

+35
-20
lines changed

9 files changed

+35
-20
lines changed

lib/bashly/script/command.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ def commands
101101
end
102102

103103
# Returns a flat array containing all the commands in this tree.
104-
# This includes self + children + grandchildres + ...
105-
def deep_commands
104+
# This can children + grandchildres (recursive), and may include self
105+
def deep_commands(include_self: false)
106106
result = []
107+
result << self if include_self
107108
commands.each do |command|
108109
result << command
109110
if command.commands.any?
@@ -217,6 +218,16 @@ def grouped_commands
217218
result
218219
end
219220

221+
# Returns true if this command, or any subcommand (deep) as any arg or
222+
# flag with arg that is defined as unique
223+
def has_unique_args_or_flags?
224+
deep_commands(include_self: true).each do |command|
225+
return true if command.args.count(&:unique).positive? ||
226+
command.flags.count(&:unique).positive?
227+
end
228+
false
229+
end
230+
220231
# Returns a mode identifier
221232
def mode
222233
@mode ||= if global_flags? then :global_flags

lib/bashly/views/command/parse_requirements_case_repeatable.gtx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ args.each do |arg|
1010
if arg.unique
1111
> elif [[ -z "${unique_lookup["{{ arg.name }}:$escaped"]:-}" ]]; then
1212
> args['{{ arg.name }}']="${args[{{ arg.name }}]} $escaped"
13-
# TODO: This line should only be inserted if any uniques are used
1413
> unique_lookup["{{ arg.name }}:$escaped"]=1
1514
> shift
1615
> else
1716
> shift
1817
else
1918
> else
20-
> args['{{ arg.name }}']="${args[{{ arg.name }}]} $escaped_value"
19+
> args['{{ arg.name }}']="${args[{{ arg.name }}]} $escaped"
2120
> shift
2221
end
2322

lib/bashly/views/command/run.gtx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
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=()
9+
if has_unique_args_or_flags?
10+
> declare -A unique_lookup=()
11+
end
1112
> normalize_input "$@"
1213
> parse_requirements "${input[@]}"
1314
if user_file_exist?('before')

lib/bashly/views/flag/case_arg.gtx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ if repeatable
1313
end
1414
> 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
16+
if unique
17+
> unique_lookup["{{ name }}:${escaped}"]=1
18+
end
1819

1920
else
2021
> args['{{ name }}']="$2"

spec/approvals/examples/repeatable-arg

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ files:
4747
upcase: CONTENT OF FILE1
4848

4949
args:
50-
- ${args[file]} = "file1"
50+
- ${args[file]} = file1
5151
+ ./upcase file1 file2
5252

5353
files:
@@ -59,7 +59,7 @@ files:
5959
upcase: CONTENT OF FILE2
6060

6161
args:
62-
- ${args[file]} = "file1" "file2"
62+
- ${args[file]} = file1 file2
6363
+ ./upcase file1 file2 file1
6464

6565
files:
@@ -69,6 +69,9 @@ files:
6969
path: file2:
7070
content: content of file2
7171
upcase: CONTENT OF FILE2
72+
path: file1:
73+
content: content of file1
74+
upcase: CONTENT OF FILE1
7275

7376
args:
74-
- ${args[file]} = "file1" "file2"
77+
- ${args[file]} = file1 file2 file1

spec/approvals/examples/repeatable-flag

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ two three
4747
Verbosity level: 3
4848

4949
args:
50-
- ${args[--data]} = "one" "two three"
50+
- ${args[--data]} = one two\ three
5151
- ${args[--path]} = file\ one file-two
5252
- ${args[--verbose]} = 3
5353
+ ./download -d one --path /bin --path /usr/lib --path /bin
@@ -57,5 +57,5 @@ one
5757
Verbosity level: 1
5858

5959
args:
60-
- ${args[--data]} = "one"
61-
- ${args[--path]} = "/bin" "/usr/lib"
60+
- ${args[--data]} = one
61+
- ${args[--path]} = /bin /usr/lib

spec/approvals/fixtures/repeatable-arg-validations

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ args: none
1111
# this file is located in 'src/root_command.sh'
1212
# you can edit it freely and regenerate (it will not be overwritten)
1313
args:
14-
- ${args[number]} = "1"
14+
- ${args[number]} = 1
1515
+ ./cli 1.1
1616
validation error in NUMBER:
1717
must be an integer
@@ -22,7 +22,7 @@ must be an integer
2222
# this file is located in 'src/root_command.sh'
2323
# you can edit it freely and regenerate (it will not be overwritten)
2424
args:
25-
- ${args[number]} = "1" "2"
25+
- ${args[number]} = 1 2
2626
+ ./cli 1.1 2
2727
validation error in NUMBER:
2828
must be an integer

spec/approvals/fixtures/repeatable-flag-validations

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ args: none
1111
# this file is located in 'src/root_command.sh'
1212
# you can edit it freely and regenerate (it will not be overwritten)
1313
args:
14-
- ${args[--number]} = "1"
14+
- ${args[--number]} = 1
1515
+ ./cli --number 1.1
1616
validation error in --number, -n NUMBER:
1717
must be an integer
@@ -22,7 +22,7 @@ must be an integer
2222
# this file is located in 'src/root_command.sh'
2323
# you can edit it freely and regenerate (it will not be overwritten)
2424
args:
25-
- ${args[--number]} = "1" "2"
25+
- ${args[--number]} = 1 2
2626
+ ./cli --number 1.1 --number 2
2727
validation error in --number, -n NUMBER:
2828
must be an integer

spec/approvals/fixtures/repeatable-whitelist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ args:
1212
# this file is located in 'src/root_command.sh'
1313
# you can edit it freely and regenerate (it will not be overwritten)
1414
args:
15-
- ${args[--protocol]} = "ssh"
15+
- ${args[--protocol]} = ssh
1616
+ ./download -p ssh -p ftp
1717
# this file is located in 'src/root_command.sh'
1818
# you can edit it freely and regenerate (it will not be overwritten)
1919
args:
20-
- ${args[--protocol]} = "ssh" "ftp"
20+
- ${args[--protocol]} = ssh ftp
2121
+ ./download -p sftp -p https
2222
--protocol must be one of: ssh, ftp, sftp
2323
+ ./download -p http -p ftp

0 commit comments

Comments
 (0)