I want to change the command so that command line flag(options) are placed before command line arguments, as which is done automatically by GNU getopt.
Mac use BSD getopt so that function is lacked. I want to tweak the bash so that upon executing of one command, I run a script that parse the flags and arguments reorder them and execute the reordered command.
In this way, both
ls -lh /tmp
ls /tmp -lh
will work in my Mac's terminal.
You can't safely write a general purpose tool to do the job of reordering arguments on a command line unless you know what the optstring
argument to the getopt()
function looks like for each command. Consider:
make something -f makefile
cp something -f makefile
In the first command, you have to move both the -f
and makefile
to the front to canonicalize the command invocation. In the second, you must only move the -f
; if you move the following file name too, you rewrite the command and destroy your data.
Of course, you also have to know what the getopt_long()
argument strings look like if the command takes long-form --force
or --file=makefile
style arguments too.
Frankly, you'd do better to use POSIXLY_CORRECT in your environment on Linux and forget about the insidious flexibility it provides, and learn to write your options before your arguments at all times. Your code will work across all Unix-like machines better if you do that.
You could install GNU software in some directory other than /bin
and /usr/bin
(e.g. /usr/gnu/bin
and then ensure that you place /usr/gnu/bin
on your PATH ahead of the system directories. There are pre-built systems like fink
, too. However, that won't help with tools from Apple that don't have analogues from GNU. And there's a 'danger' that shell scripts you write will not be portable to other Macs that don't have the same setup that you do.