Skip to content

Commit e945414

Browse files
committed
add validations and improve tests
1 parent fc63e42 commit e945414

File tree

17 files changed

+475
-66
lines changed

17 files changed

+475
-66
lines changed

examples/validations/calc

Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
#!/usr/bin/env bash
2+
# This script was generated by bashly (https://github.com/DannyBen/bashly)
3+
# Modifying it manually is not recommended
4+
5+
# :script.bash3_bouncer
6+
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
7+
printf "bash version 4 or higher is required\n"
8+
exit 1
9+
fi
10+
11+
# :command.version_command
12+
version_command() {
13+
echo "$version"
14+
}
15+
16+
# :command.usage
17+
calc_usage() {
18+
if [[ -n $long_usage ]]; then
19+
printf "calc - Sample application demonstrating validations\n"
20+
echo
21+
22+
else
23+
printf "calc - Sample application demonstrating validations\n"
24+
echo
25+
26+
fi
27+
28+
printf "Usage:\n"
29+
printf " calc [command]\n"
30+
printf " calc [command] --help | -h\n"
31+
printf " calc --version | -v\n"
32+
echo
33+
# :command.usage_commands
34+
printf "Commands:\n"
35+
echo " add Add two numbers"
36+
echo
37+
38+
if [[ -n $long_usage ]]; then
39+
printf "Options:\n"
40+
# :command.usage_fixed_flags
41+
echo " --help, -h"
42+
printf " Show this help\n"
43+
echo
44+
echo " --version, -v"
45+
printf " Show version number\n"
46+
echo
47+
48+
fi
49+
}
50+
51+
# :command.usage
52+
calc_add_usage() {
53+
if [[ -n $long_usage ]]; then
54+
printf "calc add - Add two numbers\n"
55+
echo
56+
57+
else
58+
printf "calc add - Add two numbers\n"
59+
echo
60+
61+
fi
62+
63+
printf "Shortcut: a\n"
64+
echo
65+
66+
printf "Usage:\n"
67+
printf " calc add FIRST [SECOND] [options]\n"
68+
printf " calc add --help | -h\n"
69+
echo
70+
71+
if [[ -n $long_usage ]]; then
72+
printf "Options:\n"
73+
# :command.usage_fixed_flags
74+
echo " --help, -h"
75+
printf " Show this help\n"
76+
echo
77+
# :command.usage_flags
78+
# :flag.usage
79+
echo " --multiply, -m FACTOR"
80+
printf " Multiply the result\n"
81+
echo
82+
# :command.usage_args
83+
printf "Arguments:\n"
84+
85+
# :argument.usage
86+
echo " FIRST"
87+
printf " First number\n"
88+
echo
89+
90+
# :argument.usage
91+
echo " SECOND"
92+
printf " Second number\n"
93+
echo
94+
95+
fi
96+
}
97+
98+
# :command.normalize_input
99+
normalize_input() {
100+
local arg flags
101+
102+
while [[ $# -gt 0 ]]; do
103+
arg="$1"
104+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
105+
input+=("${BASH_REMATCH[1]}")
106+
input+=("${BASH_REMATCH[2]}")
107+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
108+
input+=("${BASH_REMATCH[1]}")
109+
input+=("${BASH_REMATCH[2]}")
110+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
111+
flags="${BASH_REMATCH[1]}"
112+
for (( i=0 ; i < ${#flags} ; i++ )); do
113+
input+=("-${flags:i:1}")
114+
done
115+
else
116+
input+=("$arg")
117+
fi
118+
119+
shift
120+
done
121+
}
122+
# :command.inspect_args
123+
inspect_args() {
124+
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
125+
if (( ${#args[@]} )); then
126+
echo args:
127+
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
128+
else
129+
echo args: none
130+
fi
131+
132+
if (( ${#other_args[@]} )); then
133+
echo
134+
echo other_args:
135+
echo "- \${other_args[*]} = ${other_args[*]}"
136+
for i in "${!other_args[@]}"; do
137+
echo "- \${other_args[$i]} = ${other_args[$i]}"
138+
done
139+
fi
140+
}
141+
142+
# :command.user_lib
143+
# :src/lib/validations/validate_integer.sh
144+
validate_integer() {
145+
[[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
146+
}
147+
148+
# :command.command_functions
149+
# :command.function
150+
calc_add_command() {
151+
# :src/add_command.sh
152+
echo "# this file is located in 'src/add_command.sh'"
153+
echo "# code for 'calc add' goes here"
154+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
155+
inspect_args
156+
}
157+
158+
# :command.parse_requirements
159+
parse_requirements() {
160+
# :command.fixed_flag_filter
161+
case "$1" in
162+
--version | -v )
163+
version_command
164+
exit
165+
;;
166+
167+
--help | -h )
168+
long_usage=yes
169+
calc_usage
170+
exit
171+
;;
172+
173+
esac
174+
# :command.environment_variables_filter
175+
# :command.dependencies_filter
176+
# :command.command_filter
177+
action=$1
178+
179+
case $action in
180+
-* )
181+
;;
182+
183+
add | a )
184+
action="add"
185+
shift
186+
calc_add_parse_requirements "$@"
187+
shift $#
188+
;;
189+
190+
# :command.command_fallback
191+
* )
192+
calc_usage
193+
exit 1
194+
;;
195+
196+
esac
197+
# :command.required_args_filter
198+
# :command.required_flags_filter
199+
# :command.parse_requirements_while
200+
while [[ $# -gt 0 ]]; do
201+
key="$1"
202+
case "$key" in
203+
204+
-* )
205+
printf "invalid option: %s\n" "$key"
206+
exit 1
207+
;;
208+
209+
* )
210+
# :command.parse_requirements_case
211+
printf "invalid argument: %s\n" "$key"
212+
exit 1
213+
;;
214+
215+
esac
216+
done
217+
# :command.catch_all_filter
218+
# :command.default_assignments
219+
# :command.whitelist_filter
220+
}
221+
222+
# :command.parse_requirements
223+
calc_add_parse_requirements() {
224+
# :command.fixed_flag_filter
225+
case "$1" in
226+
--version | -v )
227+
version_command
228+
exit
229+
;;
230+
231+
--help | -h )
232+
long_usage=yes
233+
calc_add_usage
234+
exit
235+
;;
236+
237+
esac
238+
# :command.environment_variables_filter
239+
# :command.dependencies_filter
240+
# :command.command_filter
241+
action="add"
242+
# :command.required_args_filter
243+
if [[ $1 && $1 != -* ]]; then
244+
if [[ -n $(validate_integer "$1") ]]; then
245+
printf "validation error: %s\n" "FIRST $(validate_integer "$1")"
246+
exit 1
247+
fi
248+
args[first]=$1
249+
shift
250+
else
251+
printf "missing required argument: FIRST\nusage: calc add FIRST [SECOND] [options]\n"
252+
exit 1
253+
fi
254+
# :command.required_flags_filter
255+
# :command.parse_requirements_while
256+
while [[ $# -gt 0 ]]; do
257+
key="$1"
258+
case "$key" in
259+
# :flag.case
260+
--multiply | -m )
261+
if [[ $2 ]]; then
262+
if [[ -n $(validate_integer "$2") ]]; then
263+
printf "validation error: %s\n" "--multiply, -m FACTOR $(validate_integer "$2")"
264+
exit 1
265+
fi
266+
args[--multiply]="$2"
267+
shift
268+
shift
269+
else
270+
printf "%s\n" "--multiply requires an argument: --multiply, -m FACTOR"
271+
exit 1
272+
fi
273+
;;
274+
275+
-* )
276+
printf "invalid option: %s\n" "$key"
277+
exit 1
278+
;;
279+
280+
* )
281+
# :command.parse_requirements_case
282+
if [[ ! ${args[first]} ]]; then
283+
if [[ -n $(validate_integer "$1") ]]; then
284+
printf "validation error: %s\n" "FIRST $(validate_integer "$1")"
285+
exit 1
286+
fi
287+
args[first]=$1
288+
shift
289+
elif [[ ! ${args[second]} ]]; then
290+
if [[ -n $(validate_integer "$1") ]]; then
291+
printf "validation error: %s\n" "SECOND $(validate_integer "$1")"
292+
exit 1
293+
fi
294+
args[second]=$1
295+
shift
296+
else
297+
printf "invalid argument: %s\n" "$key"
298+
exit 1
299+
fi
300+
;;
301+
302+
esac
303+
done
304+
# :command.catch_all_filter
305+
# :command.default_assignments
306+
# :command.whitelist_filter
307+
}
308+
309+
# :command.initialize
310+
initialize() {
311+
version="0.1.0"
312+
long_usage=''
313+
set -e
314+
315+
# :src/initialize.sh
316+
# Code here runs inside the initialize() function
317+
# Use it for anything that you need to run before any other function, like
318+
# setting environment vairables:
319+
# CONFIG_FILE=settings.ini
320+
#
321+
# Feel free to empty (but not delete) this file.
322+
}
323+
324+
# :command.run
325+
run() {
326+
declare -A args
327+
declare -a other_args
328+
declare -a input
329+
normalize_input "$@"
330+
parse_requirements "${input[@]}"
331+
332+
if [[ $action == "add" ]]; then
333+
if [[ ${args[--help]} ]]; then
334+
long_usage=yes
335+
calc_add_usage
336+
else
337+
calc_add_command
338+
fi
339+
340+
elif [[ $action == "root" ]]; then
341+
root_command
342+
fi
343+
}
344+
345+
initialize
346+
run "$@"

examples/validations/src/bashly.yml

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,19 @@ commands:
1111
help: First number
1212
required: true
1313

14-
# Specify one or more validation types (as string or array)
15-
# This validation will look for a function named `validate_integer` in your
16-
# script.
14+
# Specify a validation function.
15+
# Bashly will look for a function named `validate_integer` in your
16+
# script, you can use any name as long as it has a matching function.
1717
validate: integer
1818
- name: second
1919
help: Second number
20-
21-
# Using the array syntax, you can specify more than one validations
22-
validate:
23-
- integer
20+
validate: integer
2421

2522
flags:
26-
- long: --multiply
27-
short: -m
28-
arg: factor
29-
help: Multiply the result
23+
- long: --save
24+
arg: path
25+
help: Save the result to a file
3026

3127
# Validations also work on flags (when they have arguments)
32-
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
4128
validate: file_exists
29+

examples/validations/src/cat_command.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate_dir_exists() {
2+
[[ -d "$1" ]] || echo "must be an existing directory"
3+
}

0 commit comments

Comments
 (0)