]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/getopt-parse.tcsh
3 # A small example script for using the getopt(1) program.
4 # This script will only work with tcsh(1).
5 # A similar script using the bash(1) language can be found
6 # as getopt-parse.bash.
8 # Example input and output (from the tcsh prompt):
9 # ./parse.tcsh -a par1 'another arg' --c-long 'wow\!*\?' -cmore -b " very long "
11 # Option c, no argument
12 # Option c, argument `more'
13 # Option b, argument ` very long '
14 # Remaining arguments:
19 # Note that we had to escape the exclamation mark in the wow-argument. This
20 # is _not_ a problem with getopt, but with the tcsh command parsing. If you
21 # would give the same line from the bash prompt (ie. call ./parse.tcsh),
22 # you could remove the exclamation mark.
24 # This is a bit tricky. We use a temp variable, to be able to check the
25 # return value of getopt (eval nukes it). argv contains the command arguments
26 # as a list. The ':q` copies that list without doing any substitutions:
27 # each element of argv becomes a separate argument for getopt. The braces
28 # are needed because the result is also a list.
29 set temp
=(`getopt -s tcsh -o ab:c:: --long a-long,b-long:,c-long:: -- $argv:q`)
31 echo "Terminating..." >/dev
/stderr
35 # Now we do the eval part. As the result is a list, we need braces. But they
36 # must be quoted, because they must be evaluated when the eval is called.
37 # The 'q` stops doing any silly substitutions.
38 eval set argv
=\
($temp:q\
)
44 echo "Option a" ; shift
48 echo "Option b, argument "\
`$2:q\' ; shift ; shift
52 # c has an optional argument. As we are in quoted mode,
53 # an empty parameter will be generated if its optional
54 # argument is not found.
57 echo "Option c, no argument"
59 echo "Option c, argument "\`$2:q
\'
67 echo "Internal error!" ; exit 1
71 echo "Remaining arguments:"
72 # foreach el ($argv:q) created problems for some tcsh-versions (at least
73 # 6.02). So we use another shift-loop here: