Skip to content

Commit 35a6df6

Browse files
greenc-FNALgoogle-labs-jules[bot]Copilot
authored
fix(cet_exec_test): Ensure BSD getopt compatibility (#35)
* fix(cet_exec_test): Ensure BSD getopt compatibility The `cet_exec_test` script previously used GNU `getopt` to parse command-line arguments, which is not available on macOS by default. This caused the script to fail when run on macOS. This change replaces the `getopt` call with a manual, POSIX-compliant parsing loop. This new implementation preserves the existing command-line syntax and semantics, including support for long options with and without `=`, while ensuring the script runs correctly on both Linux and macOS. * Check for presence and validity of required arguments. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 36d35fe commit 35a6df6

File tree

1 file changed

+83
-21
lines changed

1 file changed

+83
-21
lines changed

libexec/cet_exec_test

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,96 @@ function set_datafiles() {
4343
# Main.
4444
########################################################################
4545

46-
getopt -T >/dev/null 2>&1
47-
if (( $? != 4 )); then
48-
echo "ERROR: GNU getopt required! Check SETUP_GETOPT and PATH." 1>&2
49-
exit 1
50-
fi
51-
52-
TEMP=`getopt -n "$prog" --long datafiles:,debug,dirty-workdir,remove-on-failure:,required-files:,skip-return-code:,wd: -o + -- "${@}"`
53-
eval set -- "$TEMP"
54-
55-
while true; do
56-
case $1 in
57-
--datafiles) set_datafiles "$2"; shift;;
58-
--debug) vopt="v";;
59-
--dirty-workdir) unset want_clean;;
46+
# Manual parsing of command-line arguments.
47+
declare -a remaining_args
48+
while [[ $# -gt 0 ]]; do
49+
case "$1" in
50+
--datafiles)
51+
if [[ -z "$2" || "$2" == -* ]]; then
52+
echo "ERROR: --datafiles requires an argument" 1>&2
53+
usage 1
54+
fi
55+
set_datafiles "$2"
56+
shift 2
57+
;;
58+
--datafiles=*)
59+
set_datafiles "${1#*=}"
60+
shift
61+
;;
62+
--debug)
63+
vopt="v"
64+
shift
65+
;;
66+
--dirty-workdir)
67+
unset want_clean
68+
shift
69+
;;
6070
--remove-on-failure)
71+
if [[ -z "$2" || "$2" == -* ]]; then
72+
echo "ERROR: --remove-on-failure requires an argument" 1>&2
73+
usage 1
74+
fi
6175
old_IFS="${IFS}"; IFS=';'; removeOnFailure=($2); IFS=${old_IFS}
62-
shift;;
76+
shift 2
77+
;;
78+
--remove-on-failure=*)
79+
old_IFS="${IFS}"; IFS=';'; removeOnFailure=(${1#*=}); IFS=${old_IFS}
80+
shift
81+
;;
6382
--required-files)
83+
if [[ -z "$2" || "$2" == -* ]]; then
84+
echo "ERROR: --required-files requires an argument" 1>&2
85+
usage 1
86+
fi
6487
old_IFS="${IFS}"; IFS=';'; requiredfiles=($2); IFS=${old_IFS}
65-
shift;;
66-
--skip-return-code) (( skip_code = ${2} )); shift;;
67-
--wd) wd="$2"; shift;;
68-
--) shift; break;;
69-
*) echo "Bad argument $1" 1>&2; usage 1
88+
shift 2
89+
;;
90+
--required-files=*)
91+
old_IFS="${IFS}"; IFS=';'; requiredfiles=(${1#*=}); IFS=${old_IFS}
92+
shift
93+
;;
94+
--skip-return-code)
95+
if [[ -z "$2" || "$2" == -* ]]; then
96+
echo "ERROR: --skip-return-code requires an argument" 1>&2
97+
usage 1
98+
fi
99+
(( skip_code = ${2} ))
100+
shift 2
101+
;;
102+
--skip-return-code=*)
103+
(( skip_code = ${1#*=} ))
104+
shift
105+
;;
106+
--wd)
107+
if [[ -z "$2" || "$2" == -* ]]; then
108+
echo "ERROR: --wd requires an argument" 1>&2
109+
usage 1
110+
fi
111+
wd="$2"
112+
shift 2
113+
;;
114+
--wd=*)
115+
wd="${1#*=}"
116+
shift
117+
;;
118+
--)
119+
shift
120+
remaining_args+=( "$@" )
121+
break
122+
;;
123+
-*)
124+
echo "Bad argument $1" 1>&2; usage 1
125+
;;
126+
*)
127+
remaining_args+=( "$@" )
128+
break # Stop processing at the first non-option argument
129+
;;
70130
esac
71-
shift
72131
done
73132

133+
# Restore the positional parameters
134+
set -- "${remaining_args[@]}"
135+
74136
if [[ -z "$wd" ]]; then
75137
echo "Compulsory argument --wd missing." 1>&2
76138
usage 1

0 commit comments

Comments
 (0)