shflags icon indicating copy to clipboard operation
shflags copied to clipboard

Failed to parse long option on Mac OSX Snow Leopard with standard getopt

Open GoogleCodeExporter opened this issue 9 years ago • 10 comments

$ ./hello_world.sh --help
flags:WARN getopt: illegal option -- -
getopt: illegal option -- e
getopt: illegal option -- l
getopt: illegal option -- p
 -h --
flags:FATAL unable to parse provided options with getopt.

---

$ ./hello_world.sh --name Bob
flags:WARN getopt: illegal option -- -
 -n ame -- Bob
flags:FATAL unable to parse provided options with getopt.

---

$ which getopt
/usr/bin/getopt

Original issue reported on code.google.com by [email protected] on 29 Jul 2010 at 1:51

GoogleCodeExporter avatar Jun 26 '15 04:06 GoogleCodeExporter

Fixed in CL# 146.

Original comment by [email protected] on 28 Jun 2011 at 1:31

  • Changed state: Fixed

GoogleCodeExporter avatar Jun 26 '15 04:06 GoogleCodeExporter

Hi, has the fix been tested on Mac OS X?

When I try the example as given on the Wiki, I still get this reported error on 
Mac OS X 10.6.8


GoetheMBP-2:~ andreas$ head shflags
# $Id: shflags 147 2011-06-28 13:29:40Z [email protected] $
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008 Kate Ward. All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
#
# shFlags -- Advanced command-line flag library for Unix shell scripts.
# http://code.google.com/p/shflags/
#
# Author: [email protected] (Kate Ward)
GoetheMBP-2:~ andreas$ 
GoetheMBP-2:~ andreas$ 
GoetheMBP-2:~ andreas$ 
GoetheMBP-2:~ andreas$ cat test.sh
#! /bin/sh

source ./shflags

DEFINE_string name 'world' "somebody's name" n

FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

echo $FLAGS_name
exit 0
GoetheMBP-2:~ andreas$ 
GoetheMBP-2:~ andreas$ 
GoetheMBP-2:~ andreas$ ./test.sh -n Kate
Kate
GoetheMBP-2:~ andreas$ ./test.sh --name Andreas
flags:WARN getopt: illegal option -- -
 -n ame -- Andreas
flags:FATAL unable to parse provided options with getopt.
GoetheMBP-2:~ andreas$ which getopt
/usr/bin/getopt

Original comment by [email protected] on 15 Aug 2011 at 6:42

GoogleCodeExporter avatar Jun 26 '15 04:06 GoogleCodeExporter

Apparently, my getopt version is not the enhanced one. This seems not to be 
recognized by the test in the beginning of shflags (line 136 of CL# 174).

GoetheMBP-2:~ andreas$ echo "\"`getopt --version`\""
" --"

Instead of "-- " as tested for by shflags.


GoetheMBP-2:~ andreas$ if [ "`getopt --version`" = '-- ' ]; then echo "Standard 
getopt"; fi
GoetheMBP-2:~ andreas$ if [ "`getopt --version`" = ' --' ]; then echo "Standard 
getopt"; fi
Standard getopt
GoetheMBP-2:~ andreas$ 

Original comment by [email protected] on 15 Aug 2011 at 7:08

GoogleCodeExporter avatar Jun 26 '15 04:06 GoogleCodeExporter

Sorry, wrote too soon... shflags sets correctly __FLAGS_GETOPT_VERS to 
__FLAGS_GETOPT_VERS_STD (0)

Original comment by [email protected] on 15 Aug 2011 at 7:26

GoogleCodeExporter avatar Jun 26 '15 04:06 GoogleCodeExporter

Final comment, for anyone having the same issue on Mac OS X, you can get the 
enhanced GNU getopt version via MacPorts (http://www.macports.org/).

  $ sudo port install getopt
  (after you installed MacPorts, see http://www.macports.org/install.php)

Original comment by [email protected] on 15 Aug 2011 at 7:41

GoogleCodeExporter avatar Jun 26 '15 04:06 GoogleCodeExporter

it's not fixed

@andreas: shFlags's plot being "Most shell scripts use getopt for flags 
processing, but the different versions of getopt on various OSes make writing 
portable shell scripts difficult. shFlags instead provides an API that doesn't 
change across shell and OS versions so the script writer can be confident that 
the script will work."

I don't see how installing getopt from MacPorts is satisfying. The whole point 
of shFlags is to avoid relying on a specific version of getopt...

Original comment by [email protected] on 8 Jan 2012 at 11:01

GoogleCodeExporter avatar Jun 26 '15 04:06 GoogleCodeExporter

Opening until there is a reasonable native fix for OS X.

Original comment by [email protected] on 6 Jan 2013 at 5:15

  • Changed state: Accepted

GoogleCodeExporter avatar Jun 26 '15 04:06 GoogleCodeExporter

Any word on this? Yosemite is about to launch and this is still an issue.

Original comment by [email protected] on 7 Oct 2014 at 4:37

GoogleCodeExporter avatar Jun 26 '15 04:06 GoogleCodeExporter

Hi, any news here? This still does not work on current MacOS (High Sierra) and it also does not work on my servers (DragonFlyBSD).

I'm using zsh.

mahono avatar Jul 04 '18 07:07 mahono

@mahono everything seems to work if you run brew install gnu-getopt and brew link --force gnu-getopt the default getopt doesn't play nice with long flags and shFlags - something that should be fixed, but installing the better version always works. Not sure the equivalent commands for macports or anything else as I don't use them.

jblac avatar Jul 12 '18 23:07 jblac