Skip to content

Commit bbbcb41

Browse files
Copilotobnoxxx
andcommitted
cli/builbo: replace GNU getopt with portable POSIX getopts
Co-authored-by: obnoxxx <8081179+obnoxxx@users.noreply.github.com>
1 parent 2b47d3b commit bbbcb41

1 file changed

Lines changed: 71 additions & 91 deletions

File tree

cli/builbo

Lines changed: 71 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function detect_container_command() {
3232
ACTION="${DEFAULT_ACTION}"
3333
# action that was explixitly set from the command line:
3434
SELECTED_ACTION=""
35-
ACTION=""
35+
ACTION="${DEFAULT_ACTION}"
3636
# shell for interactive use:
3737
DEFAULT_I_SHELL="bash"
3838
I_SHELL="${DEFAULT_I_SHELL}"
@@ -102,101 +102,81 @@ if [ -n "${SELECTED_ACTION}" ]; then
102102

103103

104104

105-
# parsing command line arguments:
106-
optstring="l:r:c:o:n:hbtes:d:"
107-
optstring_long="lang:,registry:,container-cmd:,help,test,build,enter,build-script:,deps:"
108-
105+
# parsing command line arguments (portable: works on stock macOS without GNU getopt):
106+
# We support GNU-style long options by translating them to short options first,
107+
# then using POSIX getopts.
109108

110-
parsed_args=$(getopt -n "builbo" -o "$optstring" -l "$optstring_long" -- "$@")
111-
args_are_valid=$?
109+
echo " processing command line arguments.."
112110

113-
if [ "$args_are_valid" != "0" ]; then
114-
echo "error:invalid args."
115-
usage
116-
exit 1
117-
fi
111+
rewrite_args=()
112+
while [ $# -gt 0 ]; do
113+
case "$1" in
114+
--lang) rewrite_args+=("-l" "$2"); shift 2 ;;
115+
--lang=*) rewrite_args+=("-l" "${1#*=}"); shift ;;
116+
--registry) rewrite_args+=("-r" "$2"); shift 2 ;;
117+
--registry=*) rewrite_args+=("-r" "${1#*=}"); shift ;;
118+
--container-cmd) rewrite_args+=("-c" "$2"); shift 2 ;;
119+
--container-cmd=*) rewrite_args+=("-c" "${1#*=}"); shift ;;
120+
--os) rewrite_args+=("-o" "$2"); shift 2 ;;
121+
--os=*) rewrite_args+=("-o" "${1#*=}"); shift ;;
122+
--registry-namespace) rewrite_args+=("-n" "$2"); shift 2 ;;
123+
--registry-namespace=*) rewrite_args+=("-n" "${1#*=}"); shift ;;
124+
--build-script) rewrite_args+=("-s" "$2"); shift 2 ;;
125+
--build-script=*) rewrite_args+=("-s" "${1#*=}"); shift ;;
126+
--deps) rewrite_args+=("-d" "$2"); shift 2 ;;
127+
--deps=*) rewrite_args+=("-d" "${1#*=}"); shift ;;
128+
--shell) rewrite_args+=("-i" "$2"); shift 2 ;;
129+
--shell=*) rewrite_args+=("-i" "${1#*=}"); shift ;;
130+
--help) rewrite_args+=("-h"); shift ;;
131+
--test) rewrite_args+=("-t"); shift ;;
132+
--build) rewrite_args+=("-b"); shift ;;
133+
--enter) rewrite_args+=("-e"); shift ;;
134+
--) shift; break ;;
135+
--*)
136+
echo "error: unknown option '$1'." >&2
137+
usage
138+
exit 1
139+
;;
140+
-*) rewrite_args+=("$1"); shift ;;
141+
*) break ;;
142+
esac
143+
done
118144

119-
# processing parsed args
145+
# append any remaining positional args unchanged
146+
while [ $# -gt 0 ]; do
147+
rewrite_args+=("$1")
148+
shift
149+
done
120150

121-
echo " processing command line arguments.."
122-
eval set -- "${parsed_args}"
123-
while :
124-
do
125-
arg="$1"
126-
echo "processing argument '${arg}'."
127-
case "${arg}" in
128-
-l | --lang)
129-
LANG="$2"
130-
echo "language '${LANG}' specified."
131-
shift 2
132-
;;
133-
-r | --registry)
134-
REGISTRY="$2"
135-
echo "registry '${REGISTRY}' specified."
136-
137-
shift 2
138-
;;
139-
-c | --container-cmd)
140-
CONTAINER_CMD="$2"
141-
echo "container command '${CONTAINER_CMD}' specified."
142-
shift 2
143-
;;
144-
-o | --os)
145-
OS="$2"
146-
echo "operating system '${OS}'specified."
147-
shift 2
148-
;;
149-
-n | --registry-namespace)
150-
NAMESPACE="$2"
151-
echo "namespace '${NAMESPACE}' specified."
152-
shift 2
153-
;;
154-
-s | --build-script)
155-
BUILD_CMD="$2"
156-
echo "build command '${BUILD_CMD}' specified."
157-
shift 2
158-
;;
159-
-d | --deps)
160-
DEPS="$2"
161-
echo "dependencies '${DEPS}' specified."
162-
shift 2
163-
;;
164-
-h | --help)
165-
set_action "help"
166-
echo "action help specified."
167-
shift
168-
;;
169-
-b| --build)
170-
echo "action build specified."
171-
set_action "build"
172-
echo
173-
shift
174-
;;
175-
-t| --test)
176-
set_action "test"
177-
echo "action test specified."
178-
shift
179-
;;
180-
-e|--enter)
181-
set_action "enter"
182-
echo "action enter specified."
183-
shift
184-
;;
185-
186-
187-
# -- means end of args. ignore and stop processing.
188-
--)
189-
shift
190-
break
191-
;;
192-
*)
193-
echo "Unexpected option $1. - this should not happen."
194-
# this should have been caught above with args_valid
195-
usage
196-
exit 1
197-
;;
198-
esac
151+
set -- "${rewrite_args[@]}"
152+
153+
# l=lang r=registry c=container-cmd o=os n=registry-namespace s=build-script d=deps i=shell h=help b=build t=test e=enter
154+
optstring="l:r:c:o:n:s:d:i:hbte"
155+
OPTIND=1
156+
157+
while getopts "${optstring}" opt; do
158+
case "${opt}" in
159+
l) LANG="${OPTARG}"; echo "language '${LANG}' specified." ;;
160+
r) REGISTRY="${OPTARG}"; echo "registry '${REGISTRY}' specified." ;;
161+
c) CONTAINER_CMD="${OPTARG}"; echo "container command '${CONTAINER_CMD}' specified." ;;
162+
o) OS="${OPTARG}"; echo "operating system '${OS}' specified." ;;
163+
n) NAMESPACE="${OPTARG}"; echo "namespace '${NAMESPACE}' specified." ;;
164+
s) BUILD_CMD="${OPTARG}"; echo "build command '${BUILD_CMD}' specified." ;;
165+
d) DEPS="${OPTARG}"; echo "dependencies '${DEPS}' specified." ;;
166+
i) I_SHELL="${OPTARG}"; echo "interactive shell '${I_SHELL}' specified." ;;
167+
h) set_action "help"; echo "action help specified." ;;
168+
b) set_action "build"; echo "action build specified." ;;
169+
t) set_action "test"; echo "action test specified." ;;
170+
e) set_action "enter"; echo "action enter specified." ;;
171+
\?)
172+
echo "error: invalid args." >&2
173+
usage
174+
exit 1
175+
;;
176+
esac
199177
done
178+
shift $((OPTIND-1))
179+
200180
echo "done processing arguments."
201181

202182
#post-processing args

0 commit comments

Comments
 (0)