Skip to content

Commit fc63e42

Browse files
committed
- Add more custom validations
1 parent 96d7c00 commit fc63e42

File tree

15 files changed

+78
-34
lines changed

15 files changed

+78
-34
lines changed

examples/validations/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
calc
1+
validate

examples/validations/src/add_command.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/validations/src/bashly.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
name: calc
1+
name: validate
22
help: Sample application demonstrating validations
33
version: 0.1.0
44

55
commands:
6-
- name: add
7-
short: a
6+
- name: calc
87
help: Add two numbers
98

109
args:
@@ -31,3 +30,12 @@ commands:
3130

3231
# Validations also work on flags (when they have arguments)
3332
validate: integer
33+
34+
# Additional validation examples
35+
- name: cat
36+
help: Show a file
37+
args:
38+
- name: path
39+
help: Path to file
40+
required: true
41+
validate: file_exists
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/calc_command.sh'"
2+
echo "# code for 'validate calc' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/cat_command.sh'"
2+
echo "# code for 'validate cat' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate_file_exists() {
2+
[[ -f "$1" ]] || echo "must be an existing file"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate_not_empty() {
2+
[[ -z "$1" ]] && echo "must not be empty"
3+
}

examples/validations/test.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ bashly generate
1010

1111
### Try Me ###
1212

13-
./calc -h
14-
./calc add 1 2 --multiply 3
15-
./calc add A
16-
./calc add 1 B
17-
./calc add 1 2 --multiply C
13+
./validate -h
14+
./validate calc 1 2 --multiply 3
15+
./validate calc A
16+
./validate calc 1 B
17+
./validate calc 1 2 --multiply C
18+
./validate cat .gitignore
19+
./validate cat no-such-file
1820

1921

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate_file_exists() {
2+
[[ -f "$1" ]] || echo "must be an existing file"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate_not_empty() {
2+
[[ -z "$1" ]] && echo "must not be empty"
3+
}

0 commit comments

Comments
 (0)