Skip to content

Commit 0458b30

Browse files
authored
Merge pull request #312 from DannyBen/fix/repeatable-arg-validation
Fix repeatable arg validation
2 parents fa12fbd + 905aa5b commit 0458b30

File tree

7 files changed

+61
-2
lines changed

7 files changed

+61
-2
lines changed

lib/bashly/views/command/parse_requirements_case_repeatable.gtx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
condition = "if"
44
args.each do |arg|
5+
= arg.render(:validations)
56
> {{ condition }} [[ -z ${args[{{ arg.name }}]+x} ]]; then
6-
= arg.render(:validations).indent 2
7-
87
if arg.repeatable
98
> args[{{ arg.name }}]="\"$1\""
109
> shift
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
+ bundle exec bashly generate
2+
creating user files in src
3+
created src/initialize.sh
4+
created src/root_command.sh
5+
created ./cli
6+
run ./cli --help to test your bash script
7+
+ ./cli
8+
# this file is located in 'src/root_command.sh'
9+
# you can edit it freely and regenerate (it will not be overwritten)
10+
args: none
11+
+ ./cli 1
12+
# this file is located in 'src/root_command.sh'
13+
# you can edit it freely and regenerate (it will not be overwritten)
14+
args:
15+
- ${args[number]} = "1"
16+
+ ./cli 1 2
17+
# this file is located in 'src/root_command.sh'
18+
# you can edit it freely and regenerate (it will not be overwritten)
19+
args:
20+
- ${args[number]} = "1" "2"
21+
+ ./cli 1.1
22+
validation error in NUMBER:
23+
must be an integer
24+
+ ./cli 1.1 2
25+
validation error in NUMBER:
26+
must be an integer
27+
+ ./cli 1 2.2
28+
validation error in NUMBER:
29+
must be an integer
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cli
2+
src/*.sh
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This fixture tests that validations on repeatable args validate all arguments
2+
**regardless of order**.
3+
Reference issue: https://github.com/DannyBen/bashly/issues/311
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: cli
2+
help: all arguments must be integers
3+
4+
args:
5+
- name: number
6+
repeatable: true
7+
validate: integer
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate_integer() {
2+
[[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
3+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f ./cli
4+
rm -f ./src/initialize.sh
5+
rm -f ./src/root_command.sh
6+
7+
set -x
8+
9+
bundle exec bashly generate
10+
11+
./cli
12+
./cli 1
13+
./cli 1 2
14+
./cli 1.1
15+
./cli 1.1 2
16+
./cli 1 2.2

0 commit comments

Comments
 (0)