2 # Desc: Processes script arguments
4 #==BEGIN Define script parameters==
5 #===BEGIN Define variables===
7 declare -ag arrayPosArgs
8 #===END Define variables===
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
13 must
() { "$@" || die
"cannot $*"; } #o
15 # Description: Prints verbose message ("vbm") to stderr if opVerbose is set to "true".
16 # Usage: vbm "DEBUG :verbose message here"
21 # Depends: bash 5.1.16, GNU-coreutils 8.30 (echo, date)
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.
29 return 0; # Function finished.
30 } # Displays message if opVerbose true
32 # Desc: Display script usage information
37 # Depends: GNU-coreutils 8.30 (cat)
40 bktemp-processArgs [ options ] [FILE...]
44 Display help information.
46 Display script version.
48 Display debugging info.
50 Define input file path.
52 Define input directory path.
54 Define output file path.
56 Define output directory path.
58 Indicate end of options.
61 bktemp-processArgs -o foo.txt
62 bktemp-processArgs -o foo.txt -- some_file.txt
64 } # Display information on how to use this script.
66 # Desc: Displays script version and license information.
69 # Input: scriptVersion var containing version string
71 # Depends: vbm(), yell, GNU-coreutils 8.30
74 vbm
"DEBUG:showVersion function called."
77 bktemp-processArgs 1.0.0
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.
84 Copyright (C) 2020 Free Software Foundation, Inc.
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.
91 vbm
"DEBUG:showVersion function ended."
92 return 0; # Function finished.
93 } # Display script version.
95 # Desc: Processes arguments provided to script.
96 # Usage: processArgs "$@"
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.
105 # opFileOut1_overwrite Indicates whether file pathFileOut1 should be overwritten (ex: "true", "false").
106 # arrayPosArgs Array of remaining positional argments
108 # yell() Displays messages to stderr.
109 # die() Displays message to stderr then exits with error code.
110 # vbm() Displays messsages to stderr if opVerbose set to "true".
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
116 # [1]: Marco Aurelio (2014-05-08). "echo that outputs to stderr". https://stackoverflow.com/a/23550347
117 # [2]: "Handling positional parameters" (2018-05-12). https://wiki.bash-hackers.org/scripting/posparams
119 # Initialize function
120 vbm
"DEBUG:processArgs function called.";
123 if [ $# -le 0 ]; then showUsage
; die
"FATAL:No arguments provided."; fi;
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].
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.
134 vbm
"DEBUG:Input file pathFileIn1 set to:${pathFileIn1}";
137 die
"FATAL: Specified input file does not exist:$2";
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.
142 vbm
"DEBUG:Input directory pathDirIn1 set to:${pathDirIn1}";
144 else # Display error if $2 is not a valid dir.
145 die
"FATAL:Specified input directory does not exist:$2";
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";;
153 *) die
"FATAL:Invalid selection.";;
155 if [ "$opFileOut1_overwrite" == "true" ]; then
157 vbm
"DEBUG:Output file pathFileOut1 set to:${pathFileOut1}";
160 die
"FATAL:Exiting in order to not overwrite output file:${pathFileOut1}";
164 vbm
"DEBUG:Output file pathFileOut1 set to:${pathFileOut1}";
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
170 vbm
"DEBUG:Output directory pathDirOut1 set to:${pathDirOut1}";
173 die
"FATAL:Specified output directory is not valid:$2";
175 --) # End of all options. See [2].
178 vbm
"DEBUG:adding to arrayPosArgs:$arg";
179 arrayPosArgs
+=("$arg");
182 -*) showUsage
; die
"FATAL: Unrecognized option.";; # Display usage
183 *) showUsage
; die
"FATAL: Unrecognized argument.";; # Handle unrecognized options. See [1].
189 vbm
"DEBUG:processArgs function ended.";
190 return 0; # Function finished.
191 } # Evaluate script options from positional arguments (ex: $1, $2, $3, etc.).
193 #===END Declare local script functions===
194 #==END Define script parameters==
196 #==BEGIN sample code==
198 yell
"DEBUG:Provided arguments..:""$*"
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[*]}";
209 # Author: Steven Baltaktei Sandoval