Skip to content

Commit 07abef6

Browse files
committed
resolve conflicts
1 parent 7ce824c commit 07abef6

File tree

31 files changed

+382
-210
lines changed

31 files changed

+382
-210
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Change Log
22
========================================
33

4+
Untagged - Latest
5+
----------------------------------------
6+
7+
- Add optional arg/flag validation functions
8+
9+
410
v0.6.8 - 2021-10-12
511
----------------------------------------
612

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ Bashly is responsible for:
6767
## Contributing / Support
6868

6969
If you experience any issue, have a question or a suggestion, or if you wish
70-
to contribute, feel free to [open an issue][issues].
70+
to contribute, feel free to [open an issue][issues] or
71+
[start a discussion][discussions].
7172

7273

7374

7475
[issues]: https://github.com/DannyBen/bashly/issues
76+
[discussions]: https://github.com/DannyBen/bashly/discussions
7577
[docs]: https://bashly.dannyb.co/
7678
[examples]: https://github.com/DannyBen/bashly/tree/master/examples#bashly-examples
7779

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ Each of these examples demonstrates one aspect or feature of bashly.
4343
- [colors](colors#readme) - using the color print feature
4444
- [yaml](yaml#readme) - using the YAML reading functions
4545
- [completions](completions#readme) - adding bash completion functionality
46+
- [validations](validations#readme) - adding argument validation functions

examples/validations/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
calc

examples/validations/README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Validations Example
2+
3+
Demonstrates how to add validation functions to arguments and flag arguments.
4+
5+
This example was generated with:
6+
7+
```bash
8+
$ bashly init
9+
# ... now edit src/bashly.yml to match the example ...
10+
$ bashly add validations
11+
$ bashly generate
12+
```
13+
14+
Running the `bashly add validations` command simply adds the
15+
[src/lib/validations](src/lib/validations) folder, which includes some built in
16+
validation functions. You can add custom function by adding a function that
17+
starts with `validate_`. Thee functions must return a string on validation
18+
failure, or an empty string on success.
19+
20+
-----
21+
22+
## `bashly.yml`
23+
24+
```yaml
25+
name: calc
26+
help: Sample application demonstrating validations
27+
version: 0.1.0
28+
29+
commands:
30+
- name: add
31+
short: a
32+
help: Add two numbers
33+
34+
args:
35+
- name: first
36+
help: First number
37+
required: true
38+
39+
# Specify one or more validation types (as string or array)
40+
# This validation will look for a function named `validate_integer` in your
41+
# script.
42+
validate: integer
43+
- name: second
44+
help: Second number
45+
46+
# Using the array syntax, you can specify more than one validations
47+
validate:
48+
- integer
49+
50+
flags:
51+
- long: --multiply
52+
short: -m
53+
arg: factor
54+
help: Multiply the result
55+
56+
# Validations also work on flags (when they have arguments)
57+
validate: integer
58+
```
59+
60+
61+
62+
## Generated script output
63+
64+
### `$ ./calc -h`
65+
66+
```shell
67+
calc - Sample application demonstrating validations
68+
69+
Usage:
70+
calc [command]
71+
calc [command] --help | -h
72+
calc --version | -v
73+
74+
Commands:
75+
add Add two numbers
76+
77+
Options:
78+
--help, -h
79+
Show this help
80+
81+
--version, -v
82+
Show version number
83+
84+
85+
86+
```
87+
88+
### `$ ./calc add 1 2 --multiply 3`
89+
90+
```shell
91+
# this file is located in 'src/add_command.sh'
92+
# code for 'calc add' goes here
93+
# you can edit it freely and regenerate (it will not be overwritten)
94+
args:
95+
- ${args[first]} = 1
96+
- ${args[--multiply]} = 3
97+
- ${args[second]} = 2
98+
99+
100+
```
101+
102+
### `$ ./calc add A`
103+
104+
```shell
105+
validation error: FIRST must be an integer
106+
107+
108+
```
109+
110+
### `$ ./calc add 1 B`
111+
112+
```shell
113+
validation error: SECOND must be an integer
114+
115+
116+
```
117+
118+
### `$ ./calc add 1 2 --multiply C`
119+
120+
```shell
121+
validation error: --multiply, -m FACTOR must be an integer
122+
123+
124+
```
125+
126+
127+
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/add_command.sh'"
2+
echo "# code for 'calc add' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: calc
2+
help: Sample application demonstrating validations
3+
version: 0.1.0
4+
5+
commands:
6+
- name: add
7+
short: a
8+
help: Add two numbers
9+
10+
args:
11+
- name: first
12+
help: First number
13+
required: true
14+
15+
# Specify one or more validation types (as string or array)
16+
# This validation will look for a function named `validate_integer` in your
17+
# script.
18+
validate: integer
19+
- name: second
20+
help: Second number
21+
22+
# Using the array syntax, you can specify more than one validations
23+
validate:
24+
- integer
25+
26+
flags:
27+
- long: --multiply
28+
short: -m
29+
arg: factor
30+
help: Multiply the result
31+
32+
# Validations also work on flags (when they have arguments)
33+
validate: integer
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Code here runs inside the initialize() function
2+
# Use it for anything that you need to run before any other function, like
3+
# setting environment vairables:
4+
# CONFIG_FILE=settings.ini
5+
#
6+
# Feel free to empty (but not delete) this file.
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+
}

examples/validations/test.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f ./src/*.sh
4+
rm -rf ./src/lib
5+
6+
set -x
7+
8+
bashly add validations
9+
bashly generate
10+
11+
### Try Me ###
12+
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
18+
19+

0 commit comments

Comments
 (0)