I often find myself writing shell functions or shell scripts that are meant to be wrappers around other commands.
It is also frequent that I want such a wrapper to support a few flags/options. The idea is that the wrapper should cull from the command-line arguments all the flags/options it supports (along with their arguments, when applicable), and pass the remaining arguments as the arguments to the wrapped command.
Now, more often than not, the wrapped command also supports flags and options of its own. This means that, according to the scheme described above, the wrapper must be able to handle command-line arguments that include both its own flags/options as well as those supported by the wrapped command.
One way to implement such a wrapper would be to specify both the wrapper's and the wrapped command's options in a call to GNU getopt
, then collect all the latter, together with any non-option arguments, in some array WRAPPED_COMMAND_ARGUMENTS
. Then, at some later time, the wrapped command gets invoked with "${WRAPPED_COMMAND_ARGUMENTS[@]}"
as its command-line arguments.
This approach has worked reasonably well for me, but it becomes prohibitively laborious when the wrapped command has a lot of options.
Instead, I would like to find what in this post's title I refer to as a "permissive alternative to GNU getopt
." By this I mean a tool that, like getopt
, helps me parse the options that I explicitly tell it about, and treats all the other remaining arguments equally, i.e. making no distinction based on the presence or not of leading hyphens.
Is there such a thing?