2 # Desc: Processes script arguments
4 #==BEGIN Define script parameters==
5 #===BEGIN Define variables===
7 #===END Define variables===
9 #===BEGIN Declare local script functions===
10 yell
() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
11 die
() { yell
"$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
12 try
() { "$@" || die
"cannot $*"; } #o
14 # Description: Prints verbose message ("vbm") to stderr if opVerbose is set to "true".
15 # Usage: vbm "DEBUG :verbose message here"
20 # Depends: bash 5.0.3, GNU-coreutils 8.30 (echo, date)
22 if [ "$opVerbose" = "true" ]; then
23 functionTime
="$(date --iso-8601=ns)"; # Save current time in nano seconds.
24 echo "[$functionTime]:$0:""$*" 1>&2; # Display argument text.
28 return 0; # Function finished.
29 } # Displays message if opVerbose true
31 # Desc: Display script usage information
36 # Depends: GNU-coreutils 8.30 (cat)
39 bktemp-processArgs [ options ] [FILE...]
43 Display help information.
45 Display script version.
47 Display debugging info.
49 Define input file path.
51 Define input directory path.
53 Define output file path
55 Define output directory path
58 bktemp-processArgs -o foo.txt
60 } # Display information on how to use this script.
62 # Desc: Displays script version and license information.
65 # Input: scriptVersion var containing version string
67 # Depends: vbm(), yell, GNU-coreutils 8.30
70 vbm
"DEBUG:showVersion function called."
73 bktemp-processArgs 0.0.1
74 Copyright (C) 2021 Steven Baltakatei Sandoval
75 License GPLv3: GNU GPL version 3
76 This is free software; you are free to change and redistribute it.
77 There is NO WARRANTY, to the extent permitted by law.
80 Copyright (C) 2018 Free Software Foundation, Inc.
81 License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
82 This is free software: you are free to change and redistribute it.
83 There is NO WARRANTY, to the extent permitted by law.
87 vbm
"DEBUG:showVersion function ended."
88 return 0; # Function finished.
89 } # Display script version.
91 # Desc: Processes arguments provided to script.
92 # Usage: processArgs "$@"
94 # Input: "$@" (list of arguments provided to the function)
95 # Output: Sets following variables used by other functions:
96 # opVerbose Indicates verbose mode enable status. (ex: "true", "false")
97 # pathDirOut1 Path to output directory.
98 # pathFileOut1 Path to output file.
99 # pathDirIn1 Path to input directory.
100 # pathFileIn1 Path to input file.
101 # opFileOut1_overwrite Indicates whether file pathFileOut1 should be overwritten (ex: "true", "false')
103 # yell() Displays messages to stderr.
104 # vbm() Displays messsages to stderr if opVerbose set to "true".
105 # showUsage() Displays usage information about parent script
106 # showVersion() Displays version about parent script
107 # External dependencies: bash (5.0.3), echo
109 # [1]: Marco Aurelio (2014-05-08). "echo that outputs to stderr". https://stackoverflow.com/a/23550347
111 # Initialize function
112 vbm
"DEBUG:processArgs function called."
115 while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
116 #yell "DEBUG:Starting processArgs while loop." # Debug stderr message. See [1].
117 #yell "DEBUG:Provided arguments are:""$*" # Debug stderr message. See [1].
119 -h |
--help) showUsage
; exit 1;; # Display usage.
120 --version) showVersion
; exit 1;; # Show version
121 -v |
--verbose) opVerbose
="true"; vbm
"DEBUG:Verbose mode enabled.";; # Enable verbose mode. See [1].
122 -i |
--input-file) # Define input file path
123 if [ -f "$2" ]; then # If $2 is file that exists, set pathFileIn1 to $2, pop $2.
126 vbm
"DEBUG:Input file pathFileIn1 set to:""$2";
128 yell
"ERROR: Specified input file does not exist:""$2";
132 -I |
--input-dir) # Define input directory path
133 if [ -d "$2" ]; then # If $2 is dir that exists, set pathDirIn1 to $2, pop $2.
136 vbm
"DEBUG:Input directory pathDirIn1 set to:""$2";
137 else # Display error if $2 is not a valid dir.
138 yell
"ERROR:Specified input directory does not exist:""$2";
142 -o |
--output-file) # Define output file path
143 if [ -f "$2" ]; then # If $2 is file that exists, prompt user to continue to overwrite, set pathFileOut1 to $2, pop $2.
144 yell
"Specified output file $2 already exists. Overwrite? (y/n):"
145 read -r m
; case $m in
146 y | Y |
yes) opFileOut1_overwrite
="true";;
147 n | N | no
) opFileOut1_overwrite
="false";;
148 *) yell
"Invalid selection. Exiting."; exit 1;;
150 if [ "$opFileOut1_overwrite" == "true" ]; then
153 vbm
"DEBUG:Output file pathFileOut1 set to:""$2";
155 yell
"ERORR:Exiting in order to not overwrite output file:""$pathFileOut1";
161 vbm
"DEBUG:Output file pathFileOut1 set to:""$2";
163 -O |
--output-dir) # Define output directory path
164 if [ -d "$2" ]; then # If $2 is dir that exists, set pathDirOut1 to $2, pop $2
167 vbm
"DEBUG:Output directory pathDirOut1 set to:""$2";
169 yell
"ERROR:Specified output directory is not valid:""$2";
173 *) yell
"ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options. See [1].
179 vbm
"DEBUG:processArgs function ended."
180 return 0; # Function finished.
181 } # Evaluate script options from positional arguments (ex: $1, $2, $3, etc.).
183 #===END Declare local script functions===
184 #==END Define script parameters==
186 #==BEGIN sample code==
188 yell
"DEBUG:Provided arguments..:""$*"
189 yell
"DEBUG:opVerbose...........:$opVerbose";
190 yell
"DEBUG:pathDirOut1.........:$pathDirOut1";
191 yell
"DEBUG:pathFileOut1........:$pathFileOut1";
192 yell
"DEBUG:pathDirIn1..........:$pathDirIn1";
193 yell
"DEBUG:pathFileIn1.........:$pathFileIn1";
194 yell
"DEBUG:opFileOut1_overwrite:$opFileOut1_overwrite";
197 # Author: Steven Baltaktei Sandoval