|
| 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 | +# :command.root_command |
| 6 | +root_command() { |
| 7 | + # :src/root_command.sh |
| 8 | + echo "# this file is located in 'src/root_command.sh'" |
| 9 | + echo "# you can edit it freely and regenerate (it will not be overwritten)" |
| 10 | + inspect_args |
| 11 | +} |
| 12 | + |
| 13 | +# :command.version_command |
| 14 | +version_command() { |
| 15 | + echo "$version" |
| 16 | +} |
| 17 | + |
| 18 | +# :command.usage |
| 19 | +login_usage() { |
| 20 | + if [[ -n $long_usage ]]; then |
| 21 | + printf "login - Sample showing the use of allowed values for a flag (whitelist)\n" |
| 22 | + echo |
| 23 | + else |
| 24 | + printf "login - Sample showing the use of allowed values for a flag (whitelist)\n" |
| 25 | + echo |
| 26 | + fi |
| 27 | + |
| 28 | + printf "Usage:\n" |
| 29 | + printf " login [options]\n" |
| 30 | + printf " login --help | -h\n" |
| 31 | + printf " login --version | -v\n" |
| 32 | + echo |
| 33 | + |
| 34 | + if [[ -n $long_usage ]]; then |
| 35 | + printf "Options:\n" |
| 36 | + # :command.usage_fixed_flags |
| 37 | + echo " --help, -h" |
| 38 | + printf " Show this help\n" |
| 39 | + echo |
| 40 | + echo " --version, -v" |
| 41 | + printf " Show version number\n" |
| 42 | + echo |
| 43 | + # :command.usage_flags |
| 44 | + # :flag.usage |
| 45 | + echo " --user, -u NAME" |
| 46 | + printf " User name\n" |
| 47 | + printf " Allowed: user, admin\n" |
| 48 | + printf " Default: user\n" |
| 49 | + echo |
| 50 | + |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | +# :command.inspect_args |
| 55 | +inspect_args() { |
| 56 | + readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort) |
| 57 | + if (( ${#args[@]} )); then |
| 58 | + echo args: |
| 59 | + for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done |
| 60 | + else |
| 61 | + echo args: none |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +# :command.command_functions |
| 66 | + |
| 67 | +# :command.parse_requirements |
| 68 | +parse_requirements() { |
| 69 | + # :command.fixed_flag_filter |
| 70 | + case "$1" in |
| 71 | + --version | -v ) |
| 72 | + version_command |
| 73 | + exit |
| 74 | + ;; |
| 75 | + |
| 76 | + --help | -h ) |
| 77 | + long_usage=yes |
| 78 | + login_usage |
| 79 | + exit 1 |
| 80 | + ;; |
| 81 | + |
| 82 | + esac |
| 83 | + # :command.environment_variables_filter |
| 84 | + # :command.dependencies_filter |
| 85 | + # :command.command_filter |
| 86 | + action="root" |
| 87 | + # :command.required_args_filter |
| 88 | + # :command.required_flags_filter |
| 89 | + # :command.parse_requirements_while |
| 90 | + while [[ $# -gt 0 ]]; do |
| 91 | + key="$1" |
| 92 | + case "$key" in |
| 93 | + # :flag.case |
| 94 | + --user | -u ) |
| 95 | + if [[ $2 ]]; then |
| 96 | + args[--user]="$2" |
| 97 | + shift |
| 98 | + shift |
| 99 | + if [[ ! ${args[--user]} =~ ^(user|admin)$ ]]; then |
| 100 | + printf "%s\n" "--user must be one of: user, admin" |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + else |
| 104 | + printf "%s\n" "--user requires an argument: --user, -u NAME" |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | + ;; |
| 108 | + |
| 109 | + |
| 110 | + -* ) |
| 111 | + printf "invalid option: %s\n" "$key" |
| 112 | + exit 1 |
| 113 | + ;; |
| 114 | + |
| 115 | + * ) |
| 116 | + # :command.parse_requirements_case |
| 117 | + printf "invalid argument: %s\n" "$key" |
| 118 | + exit 1 |
| 119 | + ;; |
| 120 | + |
| 121 | + esac |
| 122 | + done |
| 123 | + # :command.default_assignments |
| 124 | + [[ -n ${args[--user]} ]] || args[--user]="user" |
| 125 | +} |
| 126 | + |
| 127 | +# :command.initialize |
| 128 | +initialize() { |
| 129 | + version="0.1.0" |
| 130 | + long_usage='' |
| 131 | + set -e |
| 132 | + |
| 133 | + # :src/initialize.sh |
| 134 | + # Code here runs inside the initialize() function |
| 135 | + # Use it for anything that you need to run before any other function, like |
| 136 | + # setting environment vairables: |
| 137 | + # CONFIG_FILE=settings.ini |
| 138 | + # |
| 139 | + # Feel free to empty (but not delete) this file. |
| 140 | +} |
| 141 | + |
| 142 | +# :command.run |
| 143 | +run() { |
| 144 | + declare -A args |
| 145 | + parse_requirements "$@" |
| 146 | + |
| 147 | + if [[ $action == "root" ]]; then |
| 148 | + root_command |
| 149 | + fi |
| 150 | +} |
| 151 | + |
| 152 | +initialize |
| 153 | +run "$@" |
0 commit comments