-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathargparse.sh
109 lines (94 loc) · 3.42 KB
/
argparse.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# argparse.sh contains bash functions that streamlines the management of
# command-line arguments in Bash scripts
# Example:
# define_arg "username" "" "Username for login" "string" "true"
# parse_args "$@"
#
# echo "Welcome, $username!"
#
# # Usage:
# # ./example.sh --username Alice
# Author: Yaacov Zamir <[email protected]>
# License: MIT License.
# https://github.com/yaacov/argparse-sh/
# Declare an associative array for argument properties
declare -A ARG_PROPERTIES
# Variable for the script description
SCRIPT_DESCRIPTION=""
# Function to display an error message and exit
# Usage: display_error "Error message"
display_error() {
echo -e "Error: $1\n"
show_help
exit 1
}
# Function to set the script description
# Usage: set_description "Description text"
set_description() {
SCRIPT_DESCRIPTION="$1"
}
# Function to define a command-line argument
# Usage: define_arg "arg_name" ["default"] ["help text"] ["type"] ["required"]
_NULL_VALUE_="null"
define_arg() {
local arg_name=$1
ARG_PROPERTIES["$arg_name,default"]=${2:-""} # Default value
if [[ "$2" == "$_NULL_VALUE_" ]]; then
ARG_PROPERTIES["$arg_name,default"]="" # Default value (for compatibility with 'argparse3.sh')
fi
ARG_PROPERTIES["$arg_name,help"]=${3:-""} # Help text
ARG_PROPERTIES["$arg_name,type"]=${4:-"string"} # Type [ "string" | "bool" ], default is "string".
ARG_PROPERTIES["$arg_name,required"]=${5:-"optional"} # Required flag ["required" | "optional"], default is "optional"
}
# Function to parse command-line arguments
# Usage: parse_args "$@"
parse_args() {
while [[ $# -gt 0 ]]; do
key="$1"
key="${key#--}" # Remove the '--' prefix
if [[ -n "${ARG_PROPERTIES[$key,help]}" ]]; then
if [[ "${ARG_PROPERTIES[$key,type]}" == "bool" ]]; then
export "$key"="true"
shift # past the flag argument
else
[[ -z "$2" || "$2" == --* ]] && display_error "Missing value for argument --$key"
export "$key"="$2"
shift # past argument
shift # past value
fi
else
display_error "Unknown option: $key"
fi
done
# Check for required arguments
for arg in "${!ARG_PROPERTIES[@]}"; do
arg_name="${arg%%,*}" # Extract argument name
[[ "${ARG_PROPERTIES[$arg_name,required]}" == "required" && -z "${!arg_name}" ]] && display_error "Missing required argument --$arg_name"
done
# Set defaults for any unset arguments
for arg in "${!ARG_PROPERTIES[@]}"; do
arg_name="${arg%%,*}" # Extract argument name
[[ -z "${!arg_name}" ]] && export "$arg_name"="${ARG_PROPERTIES[$arg_name,default]}"
done
}
# Function to display help
# Usage: show_help
show_help() {
[[ -n "$SCRIPT_DESCRIPTION" ]] && echo -e "$SCRIPT_DESCRIPTION\n"
echo "usage: $0 [options...]"
echo "options:"
for arg in "${!ARG_PROPERTIES[@]}"; do
arg_name="${arg%%,*}" # Extract argument name
[[ "${arg##*,}" == "help" ]] && {
[[ "${ARG_PROPERTIES[$arg_name,type]}" != "bool" ]] && echo " --$arg_name [TXT]: ${ARG_PROPERTIES[$arg]}" || echo " --$arg_name: ${ARG_PROPERTIES[$arg]}"
}
done
}
# Function to check for help option
# Usage: check_for_help "$@"
check_for_help() {
for arg in "$@"; do
[[ $arg == "-h" || $arg == "--help" ]] && { show_help; exit 0; }
done
}