Commit | Line | Data |
---|---|---|
2928a8e4 SBS |
1 | #!/bin/bash |
2 | # Desc: Processes script arguments | |
3 | ||
4 | #==BEGIN Define script parameters== | |
5 | #===BEGIN Define variables=== | |
6 | : | |
b64bbf81 | 7 | declare -ag arrayPosArgs |
2928a8e4 SBS |
8 | #===END Define variables=== |
9 | ||
10 | #===BEGIN Declare local script functions=== | |
11 | yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique | |
12 | die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370 | |
4a48bb01 | 13 | must() { "$@" || die "cannot $*"; } #o |
2928a8e4 SBS |
14 | vbm() { |
15 | # Description: Prints verbose message ("vbm") to stderr if opVerbose is set to "true". | |
16 | # Usage: vbm "DEBUG :verbose message here" | |
17 | # Version 0.2.0 | |
18 | # Input: arg1: string | |
19 | # vars: opVerbose | |
20 | # Output: stderr | |
b64bbf81 | 21 | # Depends: bash 5.1.16, GNU-coreutils 8.30 (echo, date) |
2928a8e4 SBS |
22 | |
23 | if [ "$opVerbose" = "true" ]; then | |
24 | functionTime="$(date --iso-8601=ns)"; # Save current time in nano seconds. | |
25 | echo "[$functionTime]:$0:""$*" 1>&2; # Display argument text. | |
26 | fi | |
27 | ||
28 | # End function | |
29 | return 0; # Function finished. | |
30 | } # Displays message if opVerbose true | |
31 | showUsage() { | |
32 | # Desc: Display script usage information | |
33 | # Usage: showUsage | |
b64bbf81 | 34 | # Version 0.0.2 |
2928a8e4 SBS |
35 | # Input: none |
36 | # Output: stdout | |
37 | # Depends: GNU-coreutils 8.30 (cat) | |
38 | cat <<'EOF' | |
39 | USAGE: | |
40 | bktemp-processArgs [ options ] [FILE...] | |
41 | ||
42 | OPTIONS: | |
43 | -h, --help | |
44 | Display help information. | |
45 | --version | |
46 | Display script version. | |
47 | -v, --verbose | |
48 | Display debugging info. | |
49 | -i, --input-file | |
50 | Define input file path. | |
51 | -I, --input-dir | |
52 | Define input directory path. | |
53 | -o, --output-file | |
b64bbf81 | 54 | Define output file path. |
2928a8e4 | 55 | -O, --output-dir |
b64bbf81 SBS |
56 | Define output directory path. |
57 | -- | |
58 | Indicate end of options. | |
2928a8e4 SBS |
59 | |
60 | EXAMPLE: | |
61 | bktemp-processArgs -o foo.txt | |
b64bbf81 | 62 | bktemp-processArgs -o foo.txt -- some_file.txt |
2928a8e4 SBS |
63 | EOF |
64 | } # Display information on how to use this script. | |
65 | showVersion() { | |
66 | # Desc: Displays script version and license information. | |
67 | # Usage: showVersion | |
b64bbf81 | 68 | # Version: 0.0.2 |
2928a8e4 SBS |
69 | # Input: scriptVersion var containing version string |
70 | # Output: stdout | |
71 | # Depends: vbm(), yell, GNU-coreutils 8.30 | |
72 | ||
73 | # Initialize function | |
74 | vbm "DEBUG:showVersion function called." | |
75 | ||
76 | cat <<'EOF' | |
b64bbf81 | 77 | bktemp-processArgs 1.0.0 |
2928a8e4 SBS |
78 | Copyright (C) 2021 Steven Baltakatei Sandoval |
79 | License GPLv3: GNU GPL version 3 | |
80 | This is free software; you are free to change and redistribute it. | |
81 | There is NO WARRANTY, to the extent permitted by law. | |
82 | ||
b64bbf81 SBS |
83 | GNU Coreutils 8.32 |
84 | Copyright (C) 2020 Free Software Foundation, Inc. | |
2928a8e4 SBS |
85 | License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. |
86 | This is free software: you are free to change and redistribute it. | |
87 | There is NO WARRANTY, to the extent permitted by law. | |
88 | EOF | |
89 | ||
90 | # End function | |
91 | vbm "DEBUG:showVersion function ended." | |
92 | return 0; # Function finished. | |
93 | } # Display script version. | |
94 | processArgs() { | |
95 | # Desc: Processes arguments provided to script. | |
96 | # Usage: processArgs "$@" | |
4a48bb01 | 97 | # Version: 1.0.1 |
2928a8e4 SBS |
98 | # Input: "$@" (list of arguments provided to the function) |
99 | # Output: Sets following variables used by other functions: | |
100 | # opVerbose Indicates verbose mode enable status. (ex: "true", "false") | |
101 | # pathDirOut1 Path to output directory. | |
102 | # pathFileOut1 Path to output file. | |
103 | # pathDirIn1 Path to input directory. | |
104 | # pathFileIn1 Path to input file. | |
b64bbf81 SBS |
105 | # opFileOut1_overwrite Indicates whether file pathFileOut1 should be overwritten (ex: "true", "false"). |
106 | # arrayPosArgs Array of remaining positional argments | |
2928a8e4 SBS |
107 | # Depends: |
108 | # yell() Displays messages to stderr. | |
4a48bb01 | 109 | # die() Displays message to stderr then exits with error code. |
2928a8e4 | 110 | # vbm() Displays messsages to stderr if opVerbose set to "true". |
b64bbf81 SBS |
111 | # showUsage() Displays usage information about parent script. |
112 | # showVersion() Displays version about parent script. | |
113 | # arrayPosArgs Global array for storing non-option positional arguments (i.e. arguments following the `--` option). | |
114 | # External dependencies: bash (5.1.16), echo | |
2928a8e4 SBS |
115 | # Ref./Attrib.: |
116 | # [1]: Marco Aurelio (2014-05-08). "echo that outputs to stderr". https://stackoverflow.com/a/23550347 | |
b64bbf81 | 117 | # [2]: "Handling positional parameters" (2018-05-12). https://wiki.bash-hackers.org/scripting/posparams |
2928a8e4 SBS |
118 | |
119 | # Initialize function | |
4a48bb01 | 120 | vbm "DEBUG:processArgs function called."; |
2928a8e4 SBS |
121 | |
122 | # Perform work | |
4a48bb01 | 123 | if [ $# -le 0 ]; then showUsage; die "FATAL:No arguments provided."; fi; |
2928a8e4 SBS |
124 | while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0). |
125 | #yell "DEBUG:Starting processArgs while loop." # Debug stderr message. See [1]. | |
126 | #yell "DEBUG:Provided arguments are:""$*" # Debug stderr message. See [1]. | |
127 | case "$1" in | |
128 | -h | --help) showUsage; exit 1;; # Display usage. | |
129 | --version) showVersion; exit 1;; # Show version | |
130 | -v | --verbose) opVerbose="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode. See [1]. | |
131 | -i | --input-file) # Define input file path | |
132 | if [ -f "$2" ]; then # If $2 is file that exists, set pathFileIn1 to $2, pop $2. | |
133 | pathFileIn1="$2"; | |
4a48bb01 | 134 | vbm "DEBUG:Input file pathFileIn1 set to:${pathFileIn1}"; |
2928a8e4 | 135 | shift; |
2928a8e4 | 136 | else |
4a48bb01 SBS |
137 | die "FATAL: Specified input file does not exist:$2"; |
138 | fi;; | |
2928a8e4 SBS |
139 | -I | --input-dir) # Define input directory path |
140 | if [ -d "$2" ]; then # If $2 is dir that exists, set pathDirIn1 to $2, pop $2. | |
141 | pathDirIn1="$2"; | |
4a48bb01 SBS |
142 | vbm "DEBUG:Input directory pathDirIn1 set to:${pathDirIn1}"; |
143 | shift; | |
2928a8e4 | 144 | else # Display error if $2 is not a valid dir. |
4a48bb01 SBS |
145 | die "FATAL:Specified input directory does not exist:$2"; |
146 | fi;; | |
2928a8e4 SBS |
147 | -o | --output-file) # Define output file path |
148 | if [ -f "$2" ]; then # If $2 is file that exists, prompt user to continue to overwrite, set pathFileOut1 to $2, pop $2. | |
149 | yell "Specified output file $2 already exists. Overwrite? (y/n):" | |
150 | read -r m; case $m in | |
151 | y | Y | yes) opFileOut1_overwrite="true";; | |
152 | n | N | no) opFileOut1_overwrite="false";; | |
4a48bb01 | 153 | *) die "FATAL:Invalid selection.";; |
2928a8e4 SBS |
154 | esac |
155 | if [ "$opFileOut1_overwrite" == "true" ]; then | |
156 | pathFileOut1="$2"; | |
4a48bb01 SBS |
157 | vbm "DEBUG:Output file pathFileOut1 set to:${pathFileOut1}"; |
158 | shift; | |
2928a8e4 | 159 | else |
4a48bb01 SBS |
160 | die "FATAL:Exiting in order to not overwrite output file:${pathFileOut1}"; |
161 | fi; | |
2928a8e4 SBS |
162 | else |
163 | pathFileOut1="$2"; | |
4a48bb01 SBS |
164 | vbm "DEBUG:Output file pathFileOut1 set to:${pathFileOut1}"; |
165 | shift; | |
166 | fi;; | |
2928a8e4 SBS |
167 | -O | --output-dir) # Define output directory path |
168 | if [ -d "$2" ]; then # If $2 is dir that exists, set pathDirOut1 to $2, pop $2 | |
169 | pathDirOut1="$2"; | |
4a48bb01 SBS |
170 | vbm "DEBUG:Output directory pathDirOut1 set to:${pathDirOut1}"; |
171 | shift; | |
2928a8e4 | 172 | else |
4a48bb01 SBS |
173 | die "FATAL:Specified output directory is not valid:$2"; |
174 | fi;; | |
b64bbf81 SBS |
175 | --) # End of all options. See [2]. |
176 | shift; | |
177 | for arg in "$@"; do | |
178 | vbm "DEBUG:adding to arrayPosArgs:$arg"; | |
179 | arrayPosArgs+=("$arg"); | |
180 | done; | |
181 | break;; | |
4a48bb01 SBS |
182 | -*) showUsage; die "FATAL: Unrecognized option.";; # Display usage |
183 | *) showUsage; die "FATAL: Unrecognized argument.";; # Handle unrecognized options. See [1]. | |
184 | esac; | |
185 | shift; | |
186 | done; | |
2928a8e4 SBS |
187 | |
188 | # End function | |
4a48bb01 | 189 | vbm "DEBUG:processArgs function ended."; |
2928a8e4 SBS |
190 | return 0; # Function finished. |
191 | } # Evaluate script options from positional arguments (ex: $1, $2, $3, etc.). | |
192 | ||
193 | #===END Declare local script functions=== | |
194 | #==END Define script parameters== | |
195 | ||
196 | #==BEGIN sample code== | |
197 | processArgs "$@"; | |
198 | yell "DEBUG:Provided arguments..:""$*" | |
b64bbf81 SBS |
199 | yell "DEBUG:opVerbose...........:${opVerbose:-<unset>}"; |
200 | yell "DEBUG:pathDirOut1.........:${pathDirOut1:-<unset>}"; | |
201 | yell "DEBUG:pathFileOut1........:${pathFileOut1:-<unset>}"; | |
202 | yell "DEBUG:pathDirIn1..........:${pathDirIn1:-<unset>}"; | |
203 | yell "DEBUG:pathFileIn1.........:${pathFileIn1:-<unset>}"; | |
204 | yell "DEBUG:opFileOut1_overwrite:${opFileOut1_overwrite:-<unset>}"; | |
205 | yell "DEBUG:arrayPosArgs........:$(if [[ -v arrayPosArgs ]]; then declare -p arrayPosArgs; else printf "<unset>"; fi )"; | |
206 | #yell "DEBUG:arrayPosArgs........:${arrayPosArgs[*]}"; | |
2928a8e4 SBS |
207 | #==END sample code== |
208 | ||
209 | # Author: Steven Baltaktei Sandoval | |
210 | # License: GPLv3+ |