cli/builbo: replace GNU getopt with portable POSIX getopts#25
Merged
Conversation
Copilot
AI
changed the title
[WIP] Fix command-line parsing for cli/builbo script on macOS
cli/builbo: replace GNU getopt with portable POSIX getopts
Mar 19, 2026
Contributor
Author
The "Initial plan" commit (b2a80d0) is already on the remote, so removing it requires a force push which isn't available through the automated tooling. To drop it locally and force-push: git fetch origin copilot/make-cli-portable-on-macos
git checkout copilot/make-cli-portable-on-macos
git rebase --onto 2b47d3b b2a80d0
git push --force origin copilot/make-cli-portable-on-macos |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates cli/builbo’s command-line parsing to work on stock macOS by removing the dependency on GNU getopt -l and replacing it with a long-option rewrite pass plus POSIX getopts.
Changes:
- Replace GNU
getoptlong-option parsing with a portable rewrite-to-short-options pass andgetopts. - Wire up
--shell/-ioption parsing so it’s actually recognized. - Improve error handling for unknown
--*options and fix theACTIONdefault initialization.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
65c2891 to
bbbcb41
Compare
typo fix in a comment Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Michael Adam <obnox@samba.org>
9e2456d to
e20fc19
Compare
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Michael Adam <obnox@samba.org>
The default action was redundantly assigned twice to the action to be run. This change fixes it by reducing assignments to one. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Michael Adam <obnox@samba.org>
1045004 to
b8335e9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ACTION=""bug (line 35 overrides the default action, changed toACTION="${DEFAULT_ACTION}")getoptblock with portable preprocessing pass + POSIXgetopts(noeval set --, no-lflag)--shell/-isupport (was missing from originaloptstring_long)--*) with a clear error messageOriginal prompt
Create a pull request against the
mainbranch ofTheBuildBox/buildboxto make thecli/builboscript’s command-line parsing portable on stock macOS (BSDgetopt).Context:
getoptwith long options at/around line 110:parsed_args=$(getopt -n "builbo" -o "$optstring" -l "$optstring_long" -- "$@")gnu-getopt, but fails on stock macOS where/usr/bin/getoptis BSD and doesn’t support GNU long-option parsing.Requirements:
getopt-based parsing with a portable approach that works on stock macOS.--opt valueand--opt=valueforms for long options.set_action.usage, and exit non-zero.--shellis actually supported (it’s in usage text but long-option parsing currently may not include it).eval set -- ...usage.Implementation suggestion (acceptable to adjust):
getoptswith an optstring likel:r:c:o:n:s:d:i:hbte.Files:
cli/builboTesting:
./cli/builbo --help,--test,--enter --shell bash, and a sample--build --build-script ...parse path on macOS and Linux.Reference:
The following is the prior conversation context from the user's chat exploration (may be truncated):
User: Whil everything is fine in the CI, locally, on one of my mac machines the command line processing does not work. It word fine on fedora linux machines and another mac. What could be the cause?
Assistant: The most likely cause is a
getoptmismatch on that one Mac.In
cli/builboline ~110 you do:parsed_args=$(getopt -n "builbo" -o "$optstring" -l "$optstring_long" -- "$@")That relies on GNU
getoptbehavior (supports-llong options and returns a normalized argument list that works witheval set -- ...).Why it works in CI + Fedora + “another Mac”, but not on one Mac
getoptis GNUutil-linux→ supports-land your parsing pattern./usr/bin/getoptis typically BSDgetopt, which does not support-l(and has different semantics). On a Mac with only the systemgetopt, this script’s parsing will fail or behave strangely.getoptinstalled (often via Homebrewgnu-getopt) and it’s earlier inPATH, sogetoptbehaves like Linux.Quick confirmation on the failing Mac
Run:
getopt --versionerrors or doesn’t mention GNU/util-linux, you’re on BSDgetopt./opt/homebrew/opt/gnu-getopt/bin/getopt/usr/local/opt/gnu-getopt/bin/getoptFix options (pick one)
getoptentirely and usegetopts(short options only) or a small bash argument parser.getoptif present.--lang, etc.) and stick to short options so you can use POSIXgetopts.One more mac-specific gotcha (secondary)
Make sure the script is executed with bash, not
sh/dash. Your shebang is#!/usr/bin/env bash, so it should be fine, but if someone runssh cli/builbo ...then${DEPS//,/ }and other bashisms can break.If you paste the exact command you ran and the output from the failing Mac (especially the “error:invalid args.” part, plus
which getopt/getopt --version), I can tell you which fix is the cleanest and what minimal patch to apply.User: rightm the working mac has gnu-getopt installed from hombrew, the other is on stock (BSD) getopt.
Assistant: Yep—that explains it. Your script uses GNU
getoptlong-option parsing (getopt ... -l "$optstring_long"), and macOS’s BSD/usr/bin/getoptdoesn’t support that interface, so argument parsing breaks on the stock Mac.What to do (practical options)
Option A (recommended): Detect GNU
getoptand fail with a clear messageAdd a check before line 110 that verifies GNU getopt is available, and on macOS suggests installing
gnu-getopt.Example snippet (bash):