Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/validations/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
calc
validate
346 changes: 346 additions & 0 deletions examples/validations/calc
Original file line number Diff line number Diff line change
@@ -0,0 +1,346 @@
#!/usr/bin/env bash
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended

# :script.bash3_bouncer
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n"
exit 1
fi

# :command.version_command
version_command() {
echo "$version"
}

# :command.usage
calc_usage() {
if [[ -n $long_usage ]]; then
printf "calc - Sample application demonstrating validations\n"
echo

else
printf "calc - Sample application demonstrating validations\n"
echo

fi

printf "Usage:\n"
printf " calc [command]\n"
printf " calc [command] --help | -h\n"
printf " calc --version | -v\n"
echo
# :command.usage_commands
printf "Commands:\n"
echo " add Add two numbers"
echo

if [[ -n $long_usage ]]; then
printf "Options:\n"
# :command.usage_fixed_flags
echo " --help, -h"
printf " Show this help\n"
echo
echo " --version, -v"
printf " Show version number\n"
echo

fi
}

# :command.usage
calc_add_usage() {
if [[ -n $long_usage ]]; then
printf "calc add - Add two numbers\n"
echo

else
printf "calc add - Add two numbers\n"
echo

fi

printf "Shortcut: a\n"
echo

printf "Usage:\n"
printf " calc add FIRST [SECOND] [options]\n"
printf " calc add --help | -h\n"
echo

if [[ -n $long_usage ]]; then
printf "Options:\n"
# :command.usage_fixed_flags
echo " --help, -h"
printf " Show this help\n"
echo
# :command.usage_flags
# :flag.usage
echo " --multiply, -m FACTOR"
printf " Multiply the result\n"
echo
# :command.usage_args
printf "Arguments:\n"

# :argument.usage
echo " FIRST"
printf " First number\n"
echo

# :argument.usage
echo " SECOND"
printf " Second number\n"
echo

fi
}

# :command.normalize_input
normalize_input() {
local arg flags

while [[ $# -gt 0 ]]; do
arg="$1"
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
flags="${BASH_REMATCH[1]}"
for (( i=0 ; i < ${#flags} ; i++ )); do
input+=("-${flags:i:1}")
done
else
input+=("$arg")
fi

shift
done
}
# :command.inspect_args
inspect_args() {
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
if (( ${#args[@]} )); then
echo args:
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
else
echo args: none
fi

if (( ${#other_args[@]} )); then
echo
echo other_args:
echo "- \${other_args[*]} = ${other_args[*]}"
for i in "${!other_args[@]}"; do
echo "- \${other_args[$i]} = ${other_args[$i]}"
done
fi
}

# :command.user_lib
# :src/lib/validations/validate_integer.sh
validate_integer() {
[[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
}

# :command.command_functions
# :command.function
calc_add_command() {
# :src/add_command.sh
echo "# this file is located in 'src/add_command.sh'"
echo "# code for 'calc add' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
}

# :command.parse_requirements
parse_requirements() {
# :command.fixed_flag_filter
case "$1" in
--version | -v )
version_command
exit
;;

--help | -h )
long_usage=yes
calc_usage
exit
;;

esac
# :command.environment_variables_filter
# :command.dependencies_filter
# :command.command_filter
action=$1

case $action in
-* )
;;

add | a )
action="add"
shift
calc_add_parse_requirements "$@"
shift $#
;;

# :command.command_fallback
* )
calc_usage
exit 1
;;

esac
# :command.required_args_filter
# :command.required_flags_filter
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in

-* )
printf "invalid option: %s\n" "$key"
exit 1
;;

* )
# :command.parse_requirements_case
printf "invalid argument: %s\n" "$key"
exit 1
;;

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}

# :command.parse_requirements
calc_add_parse_requirements() {
# :command.fixed_flag_filter
case "$1" in
--version | -v )
version_command
exit
;;

--help | -h )
long_usage=yes
calc_add_usage
exit
;;

esac
# :command.environment_variables_filter
# :command.dependencies_filter
# :command.command_filter
action="add"
# :command.required_args_filter
if [[ $1 && $1 != -* ]]; then
if [[ -n $(validate_integer "$1") ]]; then
printf "validation error: %s\n" "FIRST $(validate_integer "$1")"
exit 1
fi
args[first]=$1
shift
else
printf "missing required argument: FIRST\nusage: calc add FIRST [SECOND] [options]\n"
exit 1
fi
# :command.required_flags_filter
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--multiply | -m )
if [[ $2 ]]; then
if [[ -n $(validate_integer "$2") ]]; then
printf "validation error: %s\n" "--multiply, -m FACTOR $(validate_integer "$2")"
exit 1
fi
args[--multiply]="$2"
shift
shift
else
printf "%s\n" "--multiply requires an argument: --multiply, -m FACTOR"
exit 1
fi
;;

-* )
printf "invalid option: %s\n" "$key"
exit 1
;;

* )
# :command.parse_requirements_case
if [[ ! ${args[first]} ]]; then
if [[ -n $(validate_integer "$1") ]]; then
printf "validation error: %s\n" "FIRST $(validate_integer "$1")"
exit 1
fi
args[first]=$1
shift
elif [[ ! ${args[second]} ]]; then
if [[ -n $(validate_integer "$1") ]]; then
printf "validation error: %s\n" "SECOND $(validate_integer "$1")"
exit 1
fi
args[second]=$1
shift
else
printf "invalid argument: %s\n" "$key"
exit 1
fi
;;

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}

# :command.initialize
initialize() {
version="0.1.0"
long_usage=''
set -e

# :src/initialize.sh
# Code here runs inside the initialize() function
# Use it for anything that you need to run before any other function, like
# setting environment vairables:
# CONFIG_FILE=settings.ini
#
# Feel free to empty (but not delete) this file.
}

# :command.run
run() {
declare -A args
declare -a other_args
declare -a input
normalize_input "$@"
parse_requirements "${input[@]}"

if [[ $action == "add" ]]; then
if [[ ${args[--help]} ]]; then
long_usage=yes
calc_add_usage
else
calc_add_command
fi

elif [[ $action == "root" ]]; then
root_command
fi
}

initialize
run "$@"
4 changes: 0 additions & 4 deletions examples/validations/src/add_command.sh

This file was deleted.

Loading