Skip to content

cli/builbo: portable option parsing with GNU getopt + getopts fallback#24

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/update-cli-build-option-support
Closed

cli/builbo: portable option parsing with GNU getopt + getopts fallback#24
Copilot wants to merge 3 commits intomainfrom
copilot/update-cli-build-option-support

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 15, 2026

Original prompt

Rewrite the cli/builbo script so it supports both short (-b) and long (--build) options in a portable way. Achieve this by:

  • Using GNU getopt if available. With GNU getopt, support both long and short options.
  • If GNU getopt is not available (e.g., macOS or minimal Linux), fallback to Bash built-in getopts; in this mode, support only short options and print a warning if a long option is detected.
  • Ensure help/usage instructions clearly explain this behavior.

Preserve all original option/flag logic, including exclusive-action constraints, and maintain existing script functionality for both CI and end users on modern macOS and Linux. Test the script on both environments to confirm correct flag handling and robust error messages.

Update documentation/help text to describe the compatibility and fallback logic. Reference recent CI and user issues caused by getopt incompatibility for context (see https://github.com/TheBuildBox/buildbox/actions/runs/23117166893/job/67144640770?pr=23).

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Please find a solution for this failing job. Use the logs, job definition, and any referenced files where the failure occurred. Keep your response focused on the solution and include code suggestions when appropriate.

Assistant: The job failed because the builbo command in your workflow is passing an invalid option -b to getopt. The error log shows:

getopt: invalid option -- 'b'
error:invalid args.

According to the builbo output, for the build action you must provide the -s option, and the -b option is not recognized by getopt.

To fix the issue in your workflow (.github/workflows/ci.yml, lines 39 and 58), remove the -b flag from the builbo command. The correct usage should be:

For LaTeX (line 39):
builbo -l latex -o suse -s "make doc"

For tinyproxy (line 58):
builbo -l c -o fedora -s "./autogen.sh && make"

Update your workflow as above to resolve this job failure.

User: This must be a bug in the builbo script: -b is definitely intended to be a valid option (synonym to --build)

Assistant: You are correct: according to the builbo script, -b is intended to be a valid synonym for --build. The relevant portion of the script's usage message documents -b, and the argument parsing includes b in optstring and build in optstring_long.

However, your workflow is still failing with getopt: invalid option -- 'b'.

This suggests the bug is likely due to either:

  • Improper or inconsistent use of getopt in the builbo script, not handling short flags as intended
  • A mismatch between how options are parsed and passed to getopt

How to fix:

  1. Review Argument Parsing:
    Check how getopt is invoked in the script after the optstring definition. Make sure it is compatible with both short (-b) and long (--build) flags.

  2. Possible root cause:
    On some platforms, the default getopt might behave differently (not supporting GNU-style long options), or the way flags are combined/parsed could trip it up.

Suggested fix for the script:

  • Ensure you're using GNU getopt, or consider switching to getopts for basic short-option parsing.
  • Double-check how the options are defined and passed; -b must be handled just like -h, -t, etc.

Example code suggestion for Bash:
If you want a simple solution using getopts (supporting only short options):

while getopts "l:r:c:o:n:hbtes:d:" opt; do
  case "$opt" in
    b) set_action build ;;
    # handle other flags here...
    *)
      usage
      exit 1
      ;;
  esac
done

If you need both short/long options robustly, consider installing and using GNU getopt explicitly, and documenting that dependency.

Summary:

  • The workflow is correct; -b should work.
  • Patch the builbo script to correctly parse -b as a valid flag for your intended usage.

You can review the relevant code and usage here:
cli/builbo: argument parsing

If you want to share the getopt invocation section, I can suggest an exact fix.

User: the getopt invocation is in line 109 of the script.

Assistant: Thanks for pointing out the getopt invocation (line 101):

parsed_args=$(getopt  -n "builbo" -o "$optstring" -l "$optstring_long" -- "$@")

Your getopt bug root cause:
The short option string (optstring) is:

l:r:c:o:n:hbtes:d:

This means all of l, r, c, o, n, s, and d expect an argument (because of the :), while h, b, t, e are flags and do not expect an argument.

However:
If you pass -b (intending it as a flag), but somewhere in the workflow an option argument immediately follows (e.g., -b -s ...) and your default getopt (particularly non-GNU getopt, such as on Ubuntu) does not support proper interleaving of flags and options, this triggers the invalid option -- 'b' or, worse, tries to parse -s/make doc as argum...

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…portability

Co-authored-by: obnoxxx <8081179+obnoxxx@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for short and long options in cli/build script cli/builbo: portable option parsing with GNU getopt + getopts fallback Mar 15, 2026
Copilot AI requested a review from obnoxxx March 15, 2026 19:32
Copy link
Copy Markdown
Collaborator

@obnoxxx obnoxxx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Thanks for the PR!

The option parsing has been fixed with PR #23 .

So it seems this PR is not required any more right now and can be closed.

We can keep the general idea for a possible later change.

Co-authored-by: obnoxxx <8081179+obnoxxx@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 16, 2026

@copilot Thanks for the PR!

The option parsing has been fixed with PR #23 .
...

Understood — I've reverted cli/builbo back to its original state in commit cdfabe9. The PR is now clean and ready to close.

Copilot AI requested a review from obnoxxx March 16, 2026 18:15
@obnoxxx
Copy link
Copy Markdown
Collaborator

obnoxxx commented Mar 16, 2026

based on the above comment, the purpose of this PR has mostly been covered by #23 . Closing.

@obnoxxx obnoxxx closed this Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants