Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work under macOS #8

Closed
nodeg opened this issue Feb 13, 2023 · 9 comments · Fixed by #20
Closed

Does not work under macOS #8

nodeg opened this issue Feb 13, 2023 · 9 comments · Fixed by #20

Comments

@nodeg
Copy link
Member

nodeg commented Feb 13, 2023

The script does not work under macOS using zsh or bash. Most probably due to the fact that macOS uses getopt from FreeBSD and not the GNU-based one, which differs.

❯ ./uyuni-docs-helper
[ERROR] Incorrect syntax. Use uyuni-docs-helper -h for help
❯ ./uyuni-docs-helper -h
[ERROR] Incorrect syntax. Use uyuni-docs-helper -h for help
❯ ./uyuni-docs-helper --help
[ERROR] Incorrect syntax. Use uyuni-docs-helper -h for helpecho $SHELL
/bin/zsh
❯ sw_vers
ProductName:            macOS
ProductVersion:         13.2
BuildVersion:           22D49
@juliogonzalez
Copy link
Member

juliogonzalez commented Mar 30, 2023

@nodeg I don't have macOS system to debug. Can you tell what the differences are?

@nodeg
Copy link
Member Author

nodeg commented Apr 2, 2023

I do no know the exact differences, either, but I found some useful information.

The source behind getopt on macOS can be e.g. found in the Apple OSS repository. I also found out, one can use getopts which is POSIX compatible and not bash specific.

Links

@juliogonzalez
Copy link
Member

@nodeg can you provide the output of...

getopt -h

From your MacOS system?

@juliogonzalez
Copy link
Member

Looks to me that you are hitting https://stackoverflow.com/a/11778003, and if that's the case the only alternative would be installing and configuring gnu-getopt with brew.

But let's first inspect getopt help. Since I see https://github.com/apple-oss-distributions/Libc/blob/7861c72b1692b65f79c03f21a8a1a8e51e14c843/stdlib/FreeBSD/getopt_long.c, I'd asume getopt on MacOS should also support long options, but...

@nodeg
Copy link
Member Author

nodeg commented Apr 12, 2023

@nodeg can you provide the output of...

getopt -h

From your MacOS system?

There is sadly not much to see.

$ getopt -h 
 --

I somehow already have gnu-getopt installed because it is a dependency of other command line tools I am using. This version however is not used by default and has to be explicitly invoked via /usr/local/opt/gnu-getopt/bin/getopt.

@juliogonzalez
Copy link
Member

man getopt, maybe?

or just getopt?

@nodeg
Copy link
Member Author

nodeg commented Apr 12, 2023

Just getopt returns the same as shown in my last comment. The man page contains the following:

$ man getopt


GETOPT(1)                                                  General Commands Manual                                                 GETOPT(1)

NAME
     getopt – parse command options

SYNOPSIS
     args=`getopt optstring $*` ; errcode=$?; set -- $args

DESCRIPTION
     The getopt utility is used to break up options in command lines for easy parsing by shell procedures, and to check for legal options.
     Optstring is a string of recognized option letters (see getopt(3)); if a letter is followed by a colon, the option is expected to have
     an argument which may or may not be separated from it by white space.  The special option ‘--’ is used to delimit the end of the
     options.  The getopt utility will place ‘--’ in the arguments at the end of the options, or recognize it if used explicitly.  The shell
     arguments ($1 $2 ...) are reset so that each option is preceded by a ‘-’ and in its own shell argument; each option argument is also in
     its own shell argument.

EXIT STATUS
     The getopt utility prints an error message on the standard error output and exits with status > 0 when it encounters an option letter
     not included in optstring.

EXAMPLES
     The following code fragment shows how one might process the arguments for a command that can take the options -a and -b, and the option
     -o, which requires an argument.

           args=`getopt abo: $*`
           # you should not use `getopt abo: "$@"` since that would parse
           # the arguments differently from what the set command below does.
           if [ $? -ne 0 ]; then
                   echo 'Usage: ...'
                   exit 2
           fi
           set -- $args
           # You cannot use the set command with a backquoted getopt directly,
           # since the exit code from getopt would be shadowed by those of set,
           # which is zero by definition.
           while :; do
                   case "$1" in
                   -a|-b)
                           echo "flag $1 set"; sflags="${1#-}$sflags"
                           shift
                           ;;
                   -o)
                           echo "oarg is '$2'"; oarg="$2"
                           shift; shift
                           ;;
                   --)
                           shift; break
                           ;;
                   esac
           done
           echo "single-char flags: '$sflags'"
           echo "oarg is '$oarg'"

     This code will accept any of the following as equivalent:

           cmd -aoarg file1 file2
           cmd -a -o arg file1 file2
           cmd -oarg -a file1 file2
           cmd -a -oarg -- file1 file2

SEE ALSO
     getopts(1), sh(1), getopt(3)
HISTORY
     Written by Henry Spencer, working from a Bell Labs manual page.  Behavior believed identical to the Bell version.  Example changed in
     FreeBSD version 3.2 and 4.0.

BUGS
     Whatever getopt(3) has.

     Arguments containing white space or embedded shell metacharacters generally will not survive intact; this looks easy to fix but is not.
     People trying to fix getopt or the example in this manpage should check the history of this file in FreeBSD.

     The error message for an invalid option is identified as coming from getopt rather than from the shell procedure containing the
     invocation of getopt; this again is hard to fix.

     The precise best way to use the set command to set the arguments without disrupting the value(s) of shell options varies from one shell
     version to another.

     Each shellscript has to carry complex code to parse arguments halfway correctly (like the example presented here).  A better getopt-
     like tool would move much of the complexity into the tool and keep the client shell scripts simpler.

macOS 13.3                                                     August 1, 2015                                                     macOS 13.3
$ man 3 getopt

GETOPT(3)                                                 Library Functions Manual                                                 GETOPT(3)

NAME
     getopt – get option character from command line argument list

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <unistd.h>

     extern char *optarg;
     extern int optind;
     extern int optopt;
     extern int opterr;
     extern int optreset;

     int
     getopt(int argc, char * const argv[], const char *optstring);

DESCRIPTION
     The getopt() function incrementally parses a command line argument list argv and returns the next known option character.  An option
     character is known if it has been specified in the string of accepted option characters, optstring.

     The option string optstring may contain the following elements: individual characters, and characters followed by a colon to indicate
     an option argument is to follow.  If an individual character is followed by two colons, then the option argument is optional; optarg is
     set to the rest of the current argv word, or NULL if there were no more characters in the current word.  This is a GNU extension.  For
     example, an option string "x" recognizes an option “-x”, and an option string "x:" recognizes an option and argument “-x argument”.  It
     does not matter to getopt() if a following argument has leading white space.

     On return from getopt(), optarg points to an option argument, if it is anticipated, and the variable optind contains the index to the
     next argv argument for a subsequent call to getopt().  The variable optopt saves the last known option character returned by getopt().

     The variables opterr and optind are both initialized to 1.  The optind variable may be set to another value before a set of calls to
     getopt() in order to skip over more or less argv entries.

     In order to use getopt() to evaluate multiple sets of arguments, or to evaluate a single set of arguments multiple times, the variable
     optreset must be set to 1 before the second and each additional set of calls to getopt(), and the variable optind must be
     reinitialized.

     The getopt() function returns -1 when the argument list is exhausted.  The interpretation of options in the argument list may be
     cancelled by the option ‘--’ (double dash) which causes getopt() to signal the end of argument processing and return -1.  When all
     options have been processed (i.e., up to the first non-option argument), getopt() returns -1.

RETURN VALUES
     The getopt() function returns the next known option character in optstring.  If getopt() encounters a character not found in optstring
     or if it detects a missing option argument, it returns ‘?’ (question mark).  If optstring has a leading ‘:’ then a missing option
     argument causes ‘:’ to be returned instead of ‘?’.  In either case, the variable optopt is set to the character that caused the error.
     The getopt() function returns -1 when the argument list is exhausted.

EXAMPLES
     #include <unistd.h>
     int bflag, ch, fd;

     bflag = 0;
     while ((ch = getopt(argc, argv, "bf:")) != -1) {
             switch (ch) {
             case 'b':
                     bflag = 1;
                     break;
             case 'f':
                     if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
                             (void)fprintf(stderr,
                                 "myname: %s: %s\n", optarg, strerror(errno));
                             exit(1);
                     }
                     break;
             case '?':
             default:
                     usage();
             }
     }
     argc -= optind;
     argv += optind;

DIAGNOSTICS
     If the getopt() function encounters a character not found in the string optstring or detects a missing option argument it writes an
     error message to the stderr and returns ‘?’.  Setting opterr to a zero will disable these error messages.  If optstring has a leading
     ‘:’ then a missing option argument causes a ‘:’ to be returned in addition to suppressing any error messages.

     Option arguments are allowed to begin with “-”; this is reasonable but reduces the amount of error checking possible.

SEE ALSO
     getopt(1), getopt_long(3), getsubopt(3)

STANDARDS
     The optreset variable was added to make it possible to call the getopt() function multiple times.  This is an extension to the IEEE Std
     1003.2 (“POSIX.2”) specification.

HISTORY
     The getopt() function appeared in 4.3BSD.

BUGS
     The getopt() function was once specified to return EOF instead of -1.  This was changed by IEEE Std 1003.2-1992 (“POSIX.2”) to decouple
     getopt() from <stdio.h>.

     A single dash “-” may be specified as a character in optstring, however it should never have an argument associated with it.  This
     allows getopt() to be used with programs that expect “-” as an option flag.  This practice is wrong, and should not be used in any
     current development.  It is provided for backward compatibility only.  Care should be taken not to use ‘-’ as the first character in
     optstring to avoid a semantic conflict with GNU getopt(), which assigns different meaning to an optstring that begins with a ‘-’.  By
     default, a single dash causes getopt() to return -1.

     It is also possible to handle digits as option letters.  This allows getopt() to be used with programs that expect a number (“-3”) as
     an option.  This practice is wrong, and should not be used in any current development.  It is provided for backward compatibility only.
     The following code fragment works in most cases.

           int ch;
           long length;
           char *p, *ep;

           while ((ch = getopt(argc, argv, "0123456789")) != -1)
                   switch (ch) {
                   case '0': case '1': case '2': case '3': case '4':
                   case '5': case '6': case '7': case '8': case '9':
                           p = argv[optind - 1];
                           if (p[0] == '-' && p[1] == ch && !p[2]) {
                                   length = ch - '0';
                                   ep = "";
                           } else if (argv[optind] && argv[optind][1] == ch) {
                                   length = strtol((p = argv[optind] + 1),
                                       &ep, 10);
                                   optind++;
                                   optreset = 1;
                           } else
                                   usage();
                           if (*ep != '\0')
                                   errx(EX_USAGE, "illegal number -- %s", p);
                           break;
                   }

macOS 13.3                                                      June 5, 2014                                                      macOS 13.3

@juliogonzalez
Copy link
Member

@nodeg, seems it took me a long time to look at this but: The problem seems to be that your default getopt does not support --long.

And while we could use getopts the problem is getopts does not support long names for options.

So at this point, I'd say prepare a patch that:

  1. Detects if the wrapper is running on MacOS.
  2. If that's the case, checks if /usr/local/opt/gnu-getopt/bin/getopt exists.
  3. If it doesn't, exist with an error telling the user to install gnu-getopt on MacOS.
  4. If exists, make the script use it instead of getopt from PATH.

It should be fairly easy.

nodeg added a commit to nodeg/uyuni-docs-helper that referenced this issue Nov 17, 2023
This adds support for macOS by using gnu-getopt. Tested on macOS 14.1
(Sonoma). This requires the package gnu-getopt that can be installed via
Homebrew with `brew install gnu-getopt`.

Fixes uyuni-project#8
nodeg added a commit to nodeg/uyuni-docs-helper that referenced this issue Nov 17, 2023
This adds support for macOS by using gnu-getopt. Tested on macOS 14.1
(Sonoma). This requires the package gnu-getopt that can be installed via
Homebrew with `brew install gnu-getopt`.

Fixes uyuni-project#8
@nodeg
Copy link
Member Author

nodeg commented Nov 23, 2023

Done. This should be fixed with #20

@nodeg nodeg closed this as completed in #20 Nov 25, 2023
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 a pull request may close this issue.

2 participants