Skip to content

Commit c577155

Browse files
committed
add an initial validation function and specs
1 parent bafa806 commit c577155

File tree

19 files changed

+253
-24
lines changed

19 files changed

+253
-24
lines changed

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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
sh: 1: ./calc: not found
68+
69+
70+
```
71+
72+
73+
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+

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/argument.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ class Argument < Base
44
def usage_string
55
required ? name.upcase : "[#{name.upcase}]"
66
end
7-
8-
def validations
9-
return [] unless options['validate']
10-
if options['validate'].is_a? String
11-
[options['validate']]
12-
else
13-
options['validate']
14-
end
15-
end
167
end
178
end
189
end

lib/bashly/models/base.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ def help
4747
options['help'] ||= ''
4848
end
4949

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+
5059
def method_missing(method_name, *arguments, &block)
5160
key = method_name.to_s
5261
respond_to?(method_name) ? options[key] : super

0 commit comments

Comments
 (0)