chore(user/bkmml):Cleanup comment
[BK-2020-03.git] / unitproc / bkt-processArgs
1 #!/bin/bash
2 # Desc: Processes script arguments
3
4 #==BEGIN Define script parameters==
5 #===BEGIN Define variables===
6 :
7 declare -ag arrayPosArgs
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
13 try() { "$@" || die "cannot $*"; } #o
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
21 # Depends: bash 5.1.16, GNU-coreutils 8.30 (echo, date)
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
34 # Version 0.0.2
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
54 Define output file path.
55 -O, --output-dir
56 Define output directory path.
57 --
58 Indicate end of options.
59
60 EXAMPLE:
61 bktemp-processArgs -o foo.txt
62 bktemp-processArgs -o foo.txt -- some_file.txt
63 EOF
64 } # Display information on how to use this script.
65 showVersion() {
66 # Desc: Displays script version and license information.
67 # Usage: showVersion
68 # Version: 0.0.2
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'
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.
82
83 GNU Coreutils 8.32
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.
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 "$@"
97 # Version: 1.0.0
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
107 # Depends:
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
114 # Ref./Attrib.:
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
117
118 # Initialize function
119 vbm "DEBUG:processArgs function called."
120
121 # Perform work
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].
125 case "$1" in
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.
131 pathFileIn1="$2";
132 shift;
133 vbm "DEBUG:Input file pathFileIn1 set to:""$2";
134 else
135 yell "ERROR: Specified input file does not exist:""$2";
136 yell "Exiting.";
137 exit 1;
138 fi ;;
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";
142 shift;
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";
146 yell "Exiting.";
147 exit 1;
148 fi ;;
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;;
156 esac
157 if [ "$opFileOut1_overwrite" == "true" ]; then
158 pathFileOut1="$2";
159 shift;
160 vbm "DEBUG:Output file pathFileOut1 set to:""$2";
161 else
162 yell "ERORR:Exiting in order to not overwrite output file:""$pathFileOut1";
163 exit 1;
164 fi
165 else
166 pathFileOut1="$2";
167 shift;
168 vbm "DEBUG:Output file pathFileOut1 set to:""$2";
169 fi ;;
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
172 pathDirOut1="$2";
173 shift;
174 vbm "DEBUG:Output directory pathDirOut1 set to:""$2";
175 else
176 yell "ERROR:Specified output directory is not valid:""$2";
177 yell "Exiting.";
178 exit 1;
179 fi ;;
180 --) # End of all options. See [2].
181 shift;
182 for arg in "$@"; do
183 vbm "DEBUG:adding to arrayPosArgs:$arg";
184 arrayPosArgs+=("$arg");
185 done;
186 break;;
187 -*) showUsage; yell "ERROR: Unrecognized option."; exit 1;; # Display usage
188 *) showUsage; yell "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options. See [1].
189 esac
190 shift
191 done
192
193 # End function
194 vbm "DEBUG:processArgs function ended."
195 return 0; # Function finished.
196 } # Evaluate script options from positional arguments (ex: $1, $2, $3, etc.).
197
198 #===END Declare local script functions===
199 #==END Define script parameters==
200
201 #==BEGIN sample code==
202 processArgs "$@";
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[*]}";
212 #==END sample code==
213
214 # Author: Steven Baltaktei Sandoval
215 # License: GPLv3+