Suppose some favorite util foo has a useful switch -n bar which is almost but not quite what's needed. To get what's needed, one could:

  • Edit the source code to add what's needed. (Usually not portable, unless an upstream author likes the edit.)
  • Make a shell wrapper script or function to add what's needed -- but unless the syntax of foo is very simple, parsing a new or extended switch can be difficult. Not because the function is difficult, but because the parsing might be.

Is there some tool that works like an alias or wrapper function for single switches of utils? Something that would work like:

myfunction() { new code goes here... }
wrap '-n=myfunction bar' foo ... -n 3 ...

Or if the -n switch will see a lot of use:

myfunction() { new code goes here... }
foo() { wrap '-n=myfunction bar' foo ... -n 3 ... ; }

Or suppose -n bar only allows bar to be an integer, and we need a little tweak to handle a floating point number, but needn't replace -n unless it's floating point:

wrap '-n=myfunction bar==fp_only` foo ... -n 3.2 ...

Another use for a wrap might be to temporarily handle an unfixed bug:

wrap '-n=myfunction bar==x' foo ... -n x ...

...where x is some value, (or set of values), sure to cause trouble without some judicious pre-parsing and reformatting.

Another use would be to add a switch where none exists, (provided it's made up of existing switch functions, or only effects output):

newswitchcode() { ...code... }
wrap '-p=newswitchcode bar'   foo ... -p 3 ...

Note that a wrap would only be feasible for utils with simple output. Utils with complex internal data pipelines like find wouldn't be be a good fit.

  • 1
    I think the normal way to do this would be to simply write the shell wrapper, as you describe in your second bullet. In fact, I'm not sure I see any difference between that and what you want. All your shell wrapper would have to do is deal with your exception cases, then just call the original with the adjusted parameters and flags. – Jeff Zeitlin Mar 31 '18 at 9:51
  • 1
    @JeffZeitlin, Really the only difference is convenience, but many utils are like that. Instead of firing up an external editor, it'd just be the CLI line-editor for quick hacks. The wrap util would include some automatic prefab type checking or simple data conversion, (suppose the target util only understands decimal input, but hex would be simper, etc...), which would be more of a pain to do on-the-fly. – agc Mar 31 '18 at 16:59
  • 1
    The thing is, each 'wrappable' command will have different needs; what you eventually write to tell your hypothetical wrap what to do with a particular command wouldn't actually be any less complex than writing a wrapping script with ed, vi, emacs, etc. – Jeff Zeitlin Apr 1 '18 at 12:35
  • @JeffZeitlin, Think of it like xargs -- xargs does nothing a data feeder wrapper script couldn't do, but xargs, despite internally being more complex than a wrapper script, is often more convenient to use than writing such a script would be. Helper utils like xargs are not universal fixes -- they help sometimes, and it's up to users to decide when. – agc Apr 1 '18 at 21:46
  • 1
    xargs is a serious hack, and really should be abolished - it breaks the philosophy of *ix utils; it does too much. What it should do is simply convert stdin into a parameter for a util that wasn't written to read from stdin - anything else is hackery that should have been handled with other utils like sed, awk, grep, and so on, possibly with additional invocations of the 'simple xargs' as needed. [More...] – Jeff Zeitlin Apr 2 '18 at 9:00

Possibly one of these (from a longer list) may help. Not simple, however.

3) getopt, enhanced getopts, part of the util-linux, allows GNU "--"
   Examples: /usr/share/doc/util-linux/examples, 2016.03.27

4) argp.sh, wrapper for getopt, creates man and help (text, XML), etc.
   Allows mixed options and arguments.
   Compiled argp.c -> argp for faster execution.
   https://sourceforge.net/projects/argpsh/, 2016.03.27

5) shflags, wrapper for getopt, creates help, allow mixed options
   and arguments
   https://github.com/kward/shflags, 2016.08.01

Best wishes ... cheers, drl

| improve this answer | |
  • 1
    Those are all useful for writing versatile wrapper scripts, but it's not clear how they help with this question, which is about a util to avoid writing wrapper scripts. – agc Apr 5 '18 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.