Last active
February 18, 2022 19:53
-
-
Save nsainaney/c7f4e1947b5dd8bfd650650550c020fc to your computer and use it in GitHub Desktop.
bashly-repleatable-output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.root_command | |
root_command() { | |
# :src/root_command.sh | |
# Convert the space delimited string to an array | |
eval "data=(${args[--data]})" | |
echo "Data elements:" | |
for i in "${data[@]}"; do | |
echo "$i" | |
done | |
# The --verbose arg will contain the number of times it was used by the user | |
verbose=${args[--verbose]} | |
echo "" | |
echo "Verbosity level: $verbose" | |
echo "" | |
inspect_args | |
} | |
# :command.version_command | |
version_command() { | |
echo "$version" | |
} | |
# :command.usage | |
download_usage() { | |
if [[ -n $long_usage ]]; then | |
printf "download - Sample application to demonstrate the use of repeatable flags\n" | |
echo | |
else | |
printf "download - Sample application to demonstrate the use of repeatable flags\n" | |
echo | |
fi | |
printf "Usage:\n" | |
printf " download [options]\n" | |
printf " download --help | -h\n" | |
printf " download --version\n" | |
echo | |
if [[ -n $long_usage ]]; then | |
printf "Options:\n" | |
# :command.usage_fixed_flags | |
echo " --help, -h" | |
printf " Show this help\n" | |
echo | |
echo " --version" | |
printf " Show version number\n" | |
echo | |
# :command.usage_flags | |
# :flag.usage | |
echo " --data, -d DATA (required)" | |
printf " Provide data values\n" | |
echo | |
# :flag.usage | |
echo " --verbose, -v" | |
printf " Set verbosity level\n" | |
echo | |
# :command.usage_examples | |
printf "Examples:\n" | |
printf " download -d one -d \"two three\" -vvv\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.command_functions | |
# :command.parse_requirements | |
parse_requirements() { | |
# :command.fixed_flag_filter | |
case "${1:-}" in | |
--version ) | |
version_command | |
exit | |
;; | |
--help | -h ) | |
long_usage=yes | |
download_usage | |
exit | |
;; | |
esac | |
# :command.environment_variables_filter | |
# :command.dependencies_filter | |
# :command.command_filter | |
action="root" | |
# :command.parse_requirements_while | |
while [[ $# -gt 0 ]]; do | |
key="$1" | |
case "$key" in | |
# :flag.case | |
--data | -d ) | |
if [[ -n ${2+x} ]]; then | |
# :flag.validations | |
args[--data]="$2" | |
shift | |
shift | |
else | |
printf "%s\n" "--data requires an argument: --data, -d DATA" | |
exit 1 | |
fi | |
;; | |
# :flag.case | |
--verbose | -v ) | |
args[--verbose]=1 | |
shift | |
;; | |
-?* ) | |
printf "invalid option: %s\n" "$key" | |
exit 1 | |
;; | |
* ) | |
# :command.parse_requirements_case | |
printf "invalid argument: %s\n" "$key" | |
exit 1 | |
;; | |
esac | |
done | |
# :command.required_args_filter | |
# :command.required_flags_filter | |
if [[ -z ${args[--data]+x} ]]; then | |
printf "missing required flag: --data, -d DATA\n" | |
exit 1 | |
fi | |
# :command.catch_all_filter | |
# :command.default_assignments | |
# :command.whitelist_filter | |
} | |
# :command.initialize | |
initialize() { | |
version="0.1.0" | |
long_usage='' | |
set -e | |
# :src/initialize.sh | |
TEST_CONFIG=FOO | |
googoo() { | |
echo "GOGOOOGOOG" | |
} | |
} | |
# :command.run | |
run() { | |
declare -A args=() | |
declare -a other_args=() | |
declare -a input=() | |
normalize_input "$@" | |
parse_requirements "${input[@]}" | |
if [[ $action == "root" ]]; then | |
root_command | |
fi | |
} | |
initialize | |
run "$@" |
Author
nsainaney
commented
Feb 18, 2022
# Convert the space delimited string to an array
eval "data=(${args[--data]})"
echo "Data elements:"
for i in "${data[@]}"; do
echo "$i"
done
# The --verbose arg will contain the number of times it was used by the user
verbose=${args[--verbose]}
echo ""
echo "Verbosity level: $verbose"
echo ""
inspect_args
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment