bookalope / Bookalope

Everything related to Bookalope and its REST API.
http://bookalope.net/
MIT License
14 stars 5 forks source link

epubdate.sh has problems with older versions of getopt. #6

Closed jenstroeger closed 5 years ago

jenstroeger commented 5 years ago

Regarding this use of the getopt command:

https://github.com/jenstroeger/Bookalope/blob/10c2828b176bf84f268cac0b788cfd4fc5b18484/examples/epubdate.sh#L9-L10

MacOS 10.11.6 seems to ship with version 1.10 2002/09/04 or thereabouts, whereas the latest version seems to be from 2014 (link). This then causes

./epubdate.sh --help
Error parsing command line arguments, exiting

which I can reproduce on my local Mac on Yosemite (10.14.5) as well when using /usr/bin/getopt directly.

Not sure about the fix quite yet:

jenstroeger commented 5 years ago

Ok… so here are more details. On a Mac, use

> /usr/bin/man /usr/share/man/man1/getopt.1 

to get to the documentation of the original getopt command from April 3, 1999 whose command line is simply as follows:

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.

The implication is that epubdate.sh would lose the long options. Thus, adjusting the code according to the old documentation:

# Parse the options and arguments of this script.
OPTIONS=`GETOPT_COMPATIBLE="" getopt hbo:kt:a:i:p: -- $*`
set -- $OPTIONS
…

seems to work:

> ./epubdate.sh -h
Usage: epubdate.sh [OPTIONS] epub
…

Fix

I think it’s best to use the -T option to perform a version check, and offer long options if the proper getopt command is available and only short ones if it’s not.

jenstroeger commented 5 years ago

Commit https://github.com/jenstroeger/Bookalope/commit/12abb231eb3901cc33df278142b3587e347c90ba.