0

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.

luanjunyi
  • 1,634
  • 1
  • 15
  • 17
  • what kind of order changing do you assume on case of 3 or more options `cp -R /root /bkp` for example – triclosan Sep 10 '12 at 04:34
  • 1
    Now that I think about it, do you know you can get a lot of the GNU tools on your Mac via the coreutils macport http://www.macports.org/ports.php?by=name&substr=coreutils (and probably others)? Maybe this could help you in your endeavor. – Mat Sep 10 '12 at 09:56
  • @Mat, I think replacing them is tedious and dangerous. Besides, it won't work with other executables that depend on BSD getopt. I was considering building a shell that do the reorder and then call bash. – luanjunyi Sep 10 '12 at 12:29

1 Answers1

2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278