/[public]/getopt/trunk/getopt-parse.tcsh
ViewVC logotype

Annotation of /getopt/trunk/getopt-parse.tcsh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 259 - (hide annotations)
Mon Nov 7 22:09:10 2005 UTC (18 years, 4 months ago) by frodo
File size: 2253 byte(s)
Imported sources

1 frodo 259 #!/bin/tcsh
2    
3     # A small example program for using the new getopt(1) program.
4     # This program will only work with tcsh(1)
5     # An similar program using the bash(1) script language can be found
6     # as parse.bash
7    
8     # Example input and output (from the tcsh prompt):
9     # ./parse.tcsh -a par1 'another arg' --c-long 'wow\!*\?' -cmore -b " very long "
10     # Option a
11     # Option c, no argument
12     # Option c, argument `more'
13     # Option b, argument ` very long '
14     # Remaining arguments:
15     # --> `par1'
16     # --> `another arg'
17     # --> `wow!*\?'
18    
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.
23    
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`)
30     if ($? != 0) then
31     echo "Terminating..." >/dev/stderr
32     exit 1
33     endif
34    
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\)
39    
40     while (1)
41     switch($1:q)
42     case -a:
43     case --a-long:
44     echo "Option a" ; shift
45     breaksw;
46     case -b:
47     case --b-long:
48     echo "Option b, argument "\`$2:q\' ; shift ; shift
49     breaksw
50     case -c:
51     case --c-long:
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.
55    
56     if ($2:q == "") then
57     echo "Option c, no argument"
58     else
59     echo "Option c, argument "\`$2:q\'
60     endif
61     shift; shift
62     breaksw
63     case --:
64     shift
65     break
66     default:
67     echo "Internal error!" ; exit 1
68     endsw
69     end
70    
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:
74     while ($#argv > 0)
75     echo '--> '\`$1:q\'
76     shift
77     end

Properties

Name Value
svn:executable *

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26