Skip to content

Commit 7c54b71

Browse files
authored
Merge pull request #130 from DannyBen/add/validations
Add optional arg/flag validation functions
2 parents 534f1bf + 6d689b9 commit 7c54b71

File tree

24 files changed

+325
-6
lines changed

24 files changed

+325
-6
lines changed

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

examples/validations/src/bashly.yml

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+

lib/bashly/commands/add.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Add < Base
88
usage "bashly add config [--force]"
99
usage "bashly add colors [--force]"
1010
usage "bashly add yaml [--force]"
11+
usage "bashly add validations [--force]"
1112
usage "bashly add comp FORMAT [OUTPUT]"
1213
usage "bashly add (-h|--help)"
1314

@@ -21,6 +22,7 @@ class Add < Base
2122
command "config", "Add standard functions for handling INI files to the lib directory."
2223
command "colors", "Add standard functions for printing colorful and formatted text to the lib directory."
2324
command "yaml", "Add standard functions for reading YAML files to the lib directory."
25+
command "validations", "Add argument validation functions to the lib directory."
2426
command "comp", "Generate a bash completions script or function."
2527

2628
example "bashly add strings --force"
@@ -34,19 +36,23 @@ def strings_command
3436
end
3537

3638
def lib_command
37-
safe_copy_lib "sample_function.sh"
39+
safe_copy_file "sample_function.sh"
3840
end
3941

4042
def config_command
41-
safe_copy_lib "config.sh"
43+
safe_copy_file "config.sh"
4244
end
4345

4446
def colors_command
45-
safe_copy_lib "colors.sh"
47+
safe_copy_file "colors.sh"
4648
end
4749

4850
def yaml_command
49-
safe_copy_lib "yaml.sh"
51+
safe_copy_file "yaml.sh"
52+
end
53+
54+
def validations_command
55+
safe_copy_dir "validations"
5056
end
5157

5258
def comp_command
@@ -68,8 +74,14 @@ def comp_command
6874

6975
private
7076

71-
def safe_copy_lib(libfile)
72-
safe_copy asset("templates/lib/#{libfile}"), "#{Settings.source_dir}/lib/#{libfile}"
77+
def safe_copy_dir(dir)
78+
Dir[asset("templates/lib/#{dir}/*.sh")].each do |file|
79+
safe_copy_file "#{dir}/#{File.basename file}"
80+
end
81+
end
82+
83+
def safe_copy_file(file)
84+
safe_copy asset("templates/lib/#{file}"), "#{Settings.source_dir}/lib/#{file}"
7385
end
7486

7587
def safe_copy(source, target)

lib/bashly/models/base.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Base
2525
parent_name
2626
required
2727
short
28+
validate
2829
version
2930
]
3031

@@ -46,6 +47,15 @@ def help
4647
options['help'] ||= ''
4748
end
4849

50+
def validations
51+
return [] unless options['validate']
52+
if options['validate'].is_a? String
53+
[options['validate']]
54+
else
55+
options['validate']
56+
end
57+
end
58+
4959
def method_missing(method_name, *arguments, &block)
5060
key = method_name.to_s
5161
respond_to?(method_name) ? options[key] : super

0 commit comments

Comments
 (0)