feat(u/bktemp-checkFlt):Add bash function to check for floats
[BK-2020-03.git] / unitproc / bktemp-processArgs
CommitLineData
2928a8e4
SBS
1#!/bin/bash
2# Desc: Processes script arguments
3
4#==BEGIN Define script parameters==
5#===BEGIN Define variables===
6:
7#===END Define variables===
8
9#===BEGIN Declare local script functions===
10yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
11die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
12try() { "$@" || die "cannot $*"; } #o
13vbm() {
14 # Description: Prints verbose message ("vbm") to stderr if opVerbose is set to "true".
15 # Usage: vbm "DEBUG :verbose message here"
16 # Version 0.2.0
17 # Input: arg1: string
18 # vars: opVerbose
19 # Output: stderr
20 # Depends: bash 5.0.3, GNU-coreutils 8.30 (echo, date)
21
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.
25 fi
26
27 # End function
28 return 0; # Function finished.
29} # Displays message if opVerbose true
30showUsage() {
31 # Desc: Display script usage information
32 # Usage: showUsage
33 # Version 0.0.1
34 # Input: none
35 # Output: stdout
36 # Depends: GNU-coreutils 8.30 (cat)
37 cat <<'EOF'
38 USAGE:
39 bktemp-processArgs [ options ] [FILE...]
40
41 OPTIONS:
42 -h, --help
43 Display help information.
44 --version
45 Display script version.
46 -v, --verbose
47 Display debugging info.
48 -i, --input-file
49 Define input file path.
50 -I, --input-dir
51 Define input directory path.
52 -o, --output-file
53 Define output file path
54 -O, --output-dir
55 Define output directory path
56
57 EXAMPLE:
58 bktemp-processArgs -o foo.txt
59EOF
60} # Display information on how to use this script.
61showVersion() {
62 # Desc: Displays script version and license information.
63 # Usage: showVersion
64 # Version: 0.0.1
65 # Input: scriptVersion var containing version string
66 # Output: stdout
67 # Depends: vbm(), yell, GNU-coreutils 8.30
68
69 # Initialize function
70 vbm "DEBUG:showVersion function called."
71
72 cat <<'EOF'
73bktemp-processArgs 0.0.1
74Copyright (C) 2021 Steven Baltakatei Sandoval
75License GPLv3: GNU GPL version 3
76This is free software; you are free to change and redistribute it.
77There is NO WARRANTY, to the extent permitted by law.
78
79 GNU Coreutils 8.30
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.
84EOF
85
86 # End function
87 vbm "DEBUG:showVersion function ended."
88 return 0; # Function finished.
89} # Display script version.
90processArgs() {
91 # Desc: Processes arguments provided to script.
92 # Usage: processArgs "$@"
93 # Version: 0.0.1
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')
102 # Depends:
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
108 # Ref./Attrib.:
109 # [1]: Marco Aurelio (2014-05-08). "echo that outputs to stderr". https://stackoverflow.com/a/23550347
110
111 # Initialize function
112 vbm "DEBUG:processArgs function called."
113
114 # Perform work
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].
118 case "$1" in
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.
124 pathFileIn1="$2";
125 shift;
126 vbm "DEBUG:Input file pathFileIn1 set to:""$2";
127 else
128 yell "ERROR: Specified input file does not exist:""$2";
129 yell "Exiting.";
130 exit 1;
131 fi ;;
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.
134 pathDirIn1="$2";
135 shift;
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";
139 yell "Exiting.";
140 exit 1;
141 fi ;;
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;;
149 esac
150 if [ "$opFileOut1_overwrite" == "true" ]; then
151 pathFileOut1="$2";
152 shift;
153 vbm "DEBUG:Output file pathFileOut1 set to:""$2";
154 else
155 yell "ERORR:Exiting in order to not overwrite output file:""$pathFileOut1";
156 exit 1;
157 fi
158 else
159 pathFileOut1="$2";
160 shift;
161 vbm "DEBUG:Output file pathFileOut1 set to:""$2";
162 fi ;;
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
165 pathDirOut1="$2";
166 shift;
167 vbm "DEBUG:Output directory pathDirOut1 set to:""$2";
168 else
169 yell "ERROR:Specified output directory is not valid:""$2";
170 yell "Exiting.";
171 exit 1;
172 fi ;;
173 *) yell "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options. See [1].
174 esac
175 shift
176 done
177
178 # End function
179 vbm "DEBUG:processArgs function ended."
180 return 0; # Function finished.
181} # Evaluate script options from positional arguments (ex: $1, $2, $3, etc.).
182
183#===END Declare local script functions===
184#==END Define script parameters==
185
186#==BEGIN sample code==
187processArgs "$@";
188yell "DEBUG:Provided arguments..:""$*"
189yell "DEBUG:opVerbose...........:$opVerbose";
190yell "DEBUG:pathDirOut1.........:$pathDirOut1";
191yell "DEBUG:pathFileOut1........:$pathFileOut1";
192yell "DEBUG:pathDirIn1..........:$pathDirIn1";
193yell "DEBUG:pathFileIn1.........:$pathFileIn1";
194yell "DEBUG:opFileOut1_overwrite:$opFileOut1_overwrite";
195#==END sample code==
196
197# Author: Steven Baltaktei Sandoval
198# License: GPLv3+