summaryrefslogtreecommitdiffstats
path: root/getopt/parse.tcsh
diff options
context:
space:
mode:
authorKarel Zak2006-12-07 00:25:35 +0100
committerKarel Zak2006-12-07 00:25:35 +0100
commit2b6fc908bc368b540845a313c3b8a867c5ad9a42 (patch)
tree6fad48a239bc90515a5dc4084d6e3c3ee1f41e29 /getopt/parse.tcsh
parentImported from util-linux-2.7.1 tarball. (diff)
downloadkernel-qcow2-util-linux-2b6fc908bc368b540845a313c3b8a867c5ad9a42.tar.gz
kernel-qcow2-util-linux-2b6fc908bc368b540845a313c3b8a867c5ad9a42.tar.xz
kernel-qcow2-util-linux-2b6fc908bc368b540845a313c3b8a867c5ad9a42.zip
Imported from util-linux-2.8 tarball.
Diffstat (limited to 'getopt/parse.tcsh')
-rw-r--r--getopt/parse.tcsh76
1 files changed, 76 insertions, 0 deletions
diff --git a/getopt/parse.tcsh b/getopt/parse.tcsh
new file mode 100644
index 000000000..7d4f42719
--- /dev/null
+++ b/getopt/parse.tcsh
@@ -0,0 +1,76 @@
+#!/bin/tcsh
+
+# A small example program for using the new getopt(1) program.
+# This program will only work with bash(1)
+# An similar program using the tcsh(1) script language can be found
+# as parse.tcsh
+
+# Example input and output (from the tcsh prompt):
+# ./parse.tcsh -a par1 'another arg' --c-long 'wow\!*\?' -cmore -b " very long "
+# Option a
+# Option c, no argument
+# Option c, argument `more'
+# Option b, argument ` very long '
+# Remaining arguments:
+# --> `par1'
+# --> `another arg'
+# --> `wow!*\?'
+
+# Note that we had to escape the exclamation mark in the wow-argument. This
+# is _not_ a problem with getopt, but with the tcsh command parsing. If you
+# would give the same line from the bash prompt (ie. call ./parse.tcsh),
+# you could remove the exclamation mark.
+
+# This is a bit tricky. We use a temp variable, to be able to check the
+# return value of getopt (eval nukes it). argv contains the command arguments
+# as a list. The ':q` copies that list without doing any substitutions:
+# each element of argv becomes a separate argument for getopt. The braces
+# are needed because the result is also a list.
+set temp=(`getopt -s tcsh -o ab:c:: --long a-long,b-long:,c-long:: -- $argv:q`)
+if ($? != 0) then
+ echo "Terminating..." >/dev/stderr
+ exit 1
+endif
+
+# Now we do the eval part. As the result is a list, we need braces. But they
+# must be quoted, because they must be evaluated when the eval is called.
+# The 'q` stops doing any silly substitutions.
+eval set argv=\($temp:q\)
+
+set
+
+while (1)
+ switch($1:q)
+ case -a:
+ case --a-long:
+ echo "Option a" ; shift
+ breaksw;
+ case -b:
+ case --b-long:
+ echo "Option b, argument "\`$2\' ; shift ; shift
+ breaksw
+ case -c:
+ case --c-long:
+ # c has an optional argument. As we are in quoted mode,
+ # an empty parameter will be generated if its optional
+ # argument is not found.
+
+ if ($2:q == "") then
+ echo "Option c, no argument"
+ else
+ echo "Option c, argument "\`$2\'
+ endif
+ shift; shift
+ breaksw
+ case --:
+ shift
+ break
+ default:
+ echo "Internal error!" ; exit 1
+ endsw
+end
+
+echo "Remaining arguments:"
+foreach el ($argv:q)
+ echo '--> '\`$el:q\'
+end