Skip to content

Instantly share code, notes, and snippets.

@nsainaney
Last active February 18, 2022 19:53
Show Gist options
  • Save nsainaney/c7f4e1947b5dd8bfd650650550c020fc to your computer and use it in GitHub Desktop.
Save nsainaney/c7f4e1947b5dd8bfd650650550c020fc to your computer and use it in GitHub Desktop.
bashly-repleatable-output
#!/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 "$@"
@nsainaney
Copy link
Author

name: download
help: Sample application to demonstrate the use of repeatable flags
version: 0.1.0

flags:
- long: --data
  short: -d
  arg: data
  help: Provide data values
  required: true

  # Setting this to true on a flag with an argument means the user can type it
  # multiple times, like --data a --data b.
  # The argument will be received as a quoted and space-delimited string which
  # needs to be converted to an array with `eval "data=(${args[--data]})"`
  repeatable: true

- long: --verbose
  short: -v
  help: Set verbosity level

  # Setting this to true on a regular flag means the user can type it multiple
  # times, in the form of -vvv or -v -v -v.
  # The argument's value will hold the number of times it was entered.
  repeatable: true

examples:
  - download -d one -d "two three" -vvv

@nsainaney
Copy link
Author

# 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