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 try
() { "$@" || 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 # vbm() Displays messsages to stderr if opVerbose set to "true".
110 # showUsage() Displays usage information about parent script.
111 # showVersion() Displays version about parent script.
112 # arrayPosArgs Global array for storing non-option positional arguments (i.e. arguments following the `--` option).
113 # External dependencies: bash (5.1.16), echo
115 # [1]: Marco Aurelio (2014-05-08). "echo that outputs to stderr". https://stackoverflow.com/a/23550347
116 # [2]: "Handling positional parameters" (2018-05-12). https://wiki.bash-hackers.org/scripting/posparams
118 # Initialize function
119 vbm
"DEBUG:processArgs function called."
122 while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
123 #yell "DEBUG:Starting processArgs while loop." # Debug stderr message. See [1].
124 #yell "DEBUG:Provided arguments are:""$*" # Debug stderr message. See [1].
126 -h |
--help) showUsage
; exit 1;; # Display usage.
127 --version) showVersion
; exit 1;; # Show version
128 -v |
--verbose) opVerbose
="true"; vbm
"DEBUG:Verbose mode enabled.";; # Enable verbose mode. See [1].
129 -i |
--input-file) # Define input file path
130 if [ -f "$2" ]; then # If $2 is file that exists, set pathFileIn1 to $2, pop $2.
133 vbm
"DEBUG:Input file pathFileIn1 set to:""$2";
135 yell
"ERROR: 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.
143 vbm
"DEBUG:Input directory pathDirIn1 set to:""$2";
144 else # Display error if $2 is not a valid dir.
145 yell
"ERROR:Specified input directory does not exist:""$2";
149 -o |
--output-file) # Define output file path
150 if [ -f "$2" ]; then # If $2 is file that exists, prompt user to continue to overwrite, set pathFileOut1 to $2, pop $2.
151 yell
"Specified output file $2 already exists. Overwrite? (y/n):"
152 read -r m
; case $m in
153 y | Y |
yes) opFileOut1_overwrite
="true";;
154 n | N | no
) opFileOut1_overwrite
="false";;
155 *) yell
"Invalid selection. Exiting."; exit 1;;
157 if [ "$opFileOut1_overwrite" == "true" ]; then
160 vbm
"DEBUG:Output file pathFileOut1 set to:""$2";
162 yell
"ERORR:Exiting in order to not overwrite output file:""$pathFileOut1";
168 vbm
"DEBUG:Output file pathFileOut1 set to:""$2";
170 -O |
--output-dir) # Define output directory path
171 if [ -d "$2" ]; then # If $2 is dir that exists, set pathDirOut1 to $2, pop $2
174 vbm
"DEBUG:Output directory pathDirOut1 set to:""$2";
176 yell
"ERROR:Specified output directory is not valid:""$2";
180 --) # End of all options. See [2].
183 vbm
"DEBUG:adding to arrayPosArgs:$arg";
184 arrayPosArgs
+=("$arg");
187 -*) showUsage
; yell
"ERROR: Unrecognized option."; exit 1;; # Display usage
188 *) showUsage
; yell
"ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options. See [1].
194 vbm
"DEBUG:processArgs function ended."
195 return 0; # Function finished.
196 } # Evaluate script options from positional arguments (ex: $1, $2, $3, etc.).
198 #===END Declare local script functions===
199 #==END Define script parameters==
201 #==BEGIN sample code==
203 yell
"DEBUG:Provided arguments..:""$*"
204 yell
"DEBUG:opVerbose...........:${opVerbose:-<unset>}";
205 yell
"DEBUG:pathDirOut1.........:${pathDirOut1:-<unset>}";
206 yell
"DEBUG:pathFileOut1........:${pathFileOut1:-<unset>}";
207 yell
"DEBUG:pathDirIn1..........:${pathDirIn1:-<unset>}";
208 yell
"DEBUG:pathFileIn1.........:${pathFileIn1:-<unset>}";
209 yell
"DEBUG:opFileOut1_overwrite:${opFileOut1_overwrite:-<unset>}";
210 yell
"DEBUG:arrayPosArgs........:$(if [[ -v arrayPosArgs ]]; then declare -p arrayPosArgs; else printf "<unset>"; fi )";
211 #yell "DEBUG:arrayPosArgs........:${arrayPosArgs[*]}";
214 # Author: Steven Baltaktei Sandoval