2

Is there a packaged program that shows command line arguments? I often find it helpful to have users run this type of program to either help them understand what their shell commands do, or to get clearer, less ambiguous feedback (e.g. when I can't see their screen and we're communicating online with paste sites).

I've written the below script in Python, which works, but I'd find it easier to tell people a Ubuntu package they can grab rather than explain how write to a file, chmod +x, and change their system (e.g. maybe they don't have ~/bin setup in PATH, ...).

I'd be happy to see a command-line sed/awk/perl/etc. script to do this functionality as well, even if it's ugly. ("If"... doing this in one-line will be ugly.)

Any solution needs to have somewhat similar results to this script; which are, in English: print each argument with a number (escaping problematic characters is just a bonus).

#!/usr/bin/env python2.6
import sys

if len(sys.argv) == 1:
  print "No args!"
else:
  args = enumerate(sys.argv)
  args.next()  # skip program name
  for i, v in args:
    r = repr(v)
    if v.strip() != v or v != r[1:-1]:
      v = r + "\t(repr)"
    print "%3d: %s" % (i, v)
4
  • How do you handle concatenated options or optional spaces and such? The way the commandline is parsed is different between applications, so I'm not sure you can have one utility making sense of all of them...
    – JanC
    Oct 25, 2010 at 19:47
  • @JanC: The goal isn't to parse the arguments into what they mean, but simply to show the shell's tokenization, which can be confusing when using $variables and $(other things).
    – Roger Pate
    Oct 25, 2010 at 19:51
  • @JanC: Try var=f\ ee; showargs $(echo "co f") $var, using either my showargs or enzotib's, for example.
    – Roger Pate
    Oct 25, 2010 at 19:59
  • 1
    If you only try to illustrate the shell's tokenization after expanding it makes more sense of course.
    – JanC
    Oct 25, 2010 at 21:19

2 Answers 2

1

My solution (you already know ;) as a function (no need to deal with PATH) to become one-liner:

showargs() { i=1; for f; do printf "arg%d: <%s>\n" $i "$f"; ((i++)); done; }
2
  • Giving you something like this right from the start would've made explaining that other issue easier. Thanks for posting. Still would love to know if there's a package, as I think that would be easiest for explaining command-lines to non-programmer types.
    – Roger Pate
    Oct 25, 2010 at 10:03
  • 1
    If there is no package yet, it might be useful to add it to one of the existing packages that contain all sorts of little tools (or create a new one to collect such scripts).
    – JanC
    Oct 25, 2010 at 21:18
0

This is particularly easy and useful in Bash. This guide at IBM walks through the use of $@, $#, $1, $2, etc. and has a nice testargs.sh script (see listings 7 and 8) that does something like you're looking for: it shows users what arguments and options they've entered.

Your Answer

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