perlopt.tpl revision 1.1.1.1.10.2 1 1.1.1.1.10.2 msaitoh [= AutoGen5 template foo=(base-name) -*- Mode: scheme -*-=]
2 1.1.1.1.10.2 msaitoh [=
3 1.1.1.1.10.2 msaitoh
4 1.1.1.1.10.2 msaitoh (emit (dne "# "))
5 1.1.1.1.10.2 msaitoh
6 1.1.1.1.10.2 msaitoh (if (not (and (exist? "prog-name") (exist? "prog-title") (exist? "version")))
7 1.1.1.1.10.2 msaitoh (error "prog-name and prog-title are required"))
8 1.1.1.1.10.2 msaitoh (define prog-name (get "prog-name"))
9 1.1.1.1.10.2 msaitoh
10 1.1.1.1.10.2 msaitoh (if (> (string-length prog-name) 16)
11 1.1.1.1.10.2 msaitoh (error (sprintf "prog-name limited to 16 characters: %s"
12 1.1.1.1.10.2 msaitoh prog-name)) )
13 1.1.1.1.10.2 msaitoh (if (not (exist? "long-opts"))
14 1.1.1.1.10.2 msaitoh (error "long-opts is required"))
15 1.1.1.1.10.2 msaitoh
16 1.1.1.1.10.2 msaitoh ;; perl list containing string to initialize the option hash
17 1.1.1.1.10.2 msaitoh (define perl_opts "")
18 1.1.1.1.10.2 msaitoh ;; perl list containing option definitions for Getopt::Long
19 1.1.1.1.10.2 msaitoh (define perl_defs " ")
20 1.1.1.1.10.2 msaitoh ;; usage string
21 1.1.1.1.10.2 msaitoh (define perl_usage "")
22 1.1.1.1.10.2 msaitoh
23 1.1.1.1.10.2 msaitoh (define optname-from "A-Z_^")
24 1.1.1.1.10.2 msaitoh (define optname-to "a-z--")
25 1.1.1.1.10.2 msaitoh (define counter 0)
26 1.1.1.1.10.2 msaitoh
27 1.1.1.1.10.2 msaitoh (define q (lambda (s) (string-append "'" s "'")))
28 1.1.1.1.10.2 msaitoh (define qp (lambda (s) (string-append "q{" s "}")))
29 1.1.1.1.10.2 msaitoh
30 1.1.1.1.10.2 msaitoh =][=
31 1.1.1.1.10.2 msaitoh
32 1.1.1.1.10.2 msaitoh FOR flag =][=
33 1.1.1.1.10.2 msaitoh
34 1.1.1.1.10.2 msaitoh (define optarg "") ;; the option argument for Getopt::Long
35 1.1.1.1.10.2 msaitoh (define opttarget "''") ;; the value of a hash key that represents option
36 1.1.1.1.10.2 msaitoh (define optargname "")
37 1.1.1.1.10.2 msaitoh (define optisarray #f)
38 1.1.1.1.10.2 msaitoh (define optname (string-tr! (get "name") optname-from optname-to))
39 1.1.1.1.10.2 msaitoh
40 1.1.1.1.10.2 msaitoh =][= #
41 1.1.1.1.10.2 msaitoh ;; since autoopts doesn't support float we take the combination arg-name =
42 1.1.1.1.10.2 msaitoh ;; float and arg-type = string as float
43 1.1.1.1.10.2 msaitoh =][=
44 1.1.1.1.10.2 msaitoh IF arg-type =][=
45 1.1.1.1.10.2 msaitoh CASE arg-type =][=
46 1.1.1.1.10.2 msaitoh
47 1.1.1.1.10.2 msaitoh =* num =][= (set! optarg "=i") =][=
48 1.1.1.1.10.2 msaitoh
49 1.1.1.1.10.2 msaitoh =* str =][=
50 1.1.1.1.10.2 msaitoh (if (and (exist? "arg-name") (== (get "arg-name") "float"))
51 1.1.1.1.10.2 msaitoh (set! optarg "=f")
52 1.1.1.1.10.2 msaitoh (set! optarg "=s")
53 1.1.1.1.10.2 msaitoh ) =][=
54 1.1.1.1.10.2 msaitoh
55 1.1.1.1.10.2 msaitoh * =][=
56 1.1.1.1.10.2 msaitoh (error (string-append "unknown arg type '"
57 1.1.1.1.10.2 msaitoh (get "arg-type") "' for " (get "name"))) =][=
58 1.1.1.1.10.2 msaitoh ESAC arg-type =][=
59 1.1.1.1.10.2 msaitoh ENDIF =][=
60 1.1.1.1.10.2 msaitoh
61 1.1.1.1.10.2 msaitoh (if (exist? "stack-arg")
62 1.1.1.1.10.2 msaitoh ;; set optarget to array reference if can take more than one value
63 1.1.1.1.10.2 msaitoh ;; FIXME: if "max" exists, then just presume it is greater than 1
64 1.1.1.1.10.2 msaitoh ;;
65 1.1.1.1.10.2 msaitoh (if (and (exist? "max") (== (get "max") "NOLIMIT"))
66 1.1.1.1.10.2 msaitoh (begin
67 1.1.1.1.10.2 msaitoh (set! opttarget (string-append
68 1.1.1.1.10.2 msaitoh "["
69 1.1.1.1.10.2 msaitoh (if (exist? "arg-default") (q (get "arg-default")) "")
70 1.1.1.1.10.2 msaitoh "]"
71 1.1.1.1.10.2 msaitoh )
72 1.1.1.1.10.2 msaitoh )
73 1.1.1.1.10.2 msaitoh (set! optisarray #t)
74 1.1.1.1.10.2 msaitoh )
75 1.1.1.1.10.2 msaitoh (error "If stack-arg then max has to be NOLIMIT")
76 1.1.1.1.10.2 msaitoh )
77 1.1.1.1.10.2 msaitoh ;; just scalar otherwise
78 1.1.1.1.10.2 msaitoh (if (exist? "arg-default") (set! opttarget (q (get "arg-default"))))
79 1.1.1.1.10.2 msaitoh )
80 1.1.1.1.10.2 msaitoh
81 1.1.1.1.10.2 msaitoh (set! perl_opts (string-append perl_opts
82 1.1.1.1.10.2 msaitoh "'" (get "name") "' => " opttarget ",\n "))
83 1.1.1.1.10.2 msaitoh
84 1.1.1.1.10.2 msaitoh (define def_add (string-append "'" optname (if (exist? "value")
85 1.1.1.1.10.2 msaitoh (string-append "|" (get "value")) "") optarg "',"))
86 1.1.1.1.10.2 msaitoh
87 1.1.1.1.10.2 msaitoh (define add_len (+ (string-length def_add) counter))
88 1.1.1.1.10.2 msaitoh (if (> add_len 80)
89 1.1.1.1.10.2 msaitoh (begin
90 1.1.1.1.10.2 msaitoh (set! perl_defs (string-append perl_defs "\n " def_add))
91 1.1.1.1.10.2 msaitoh (set! counter 8)
92 1.1.1.1.10.2 msaitoh )
93 1.1.1.1.10.2 msaitoh (begin
94 1.1.1.1.10.2 msaitoh (set! perl_defs (string-append perl_defs " " def_add))
95 1.1.1.1.10.2 msaitoh (set! counter (+ counter add_len))
96 1.1.1.1.10.2 msaitoh )
97 1.1.1.1.10.2 msaitoh )
98 1.1.1.1.10.2 msaitoh
99 1.1.1.1.10.2 msaitoh (if (exist? "arg-type")
100 1.1.1.1.10.2 msaitoh (if (and (exist? "arg-name") (== (get "arg-name") "float"))
101 1.1.1.1.10.2 msaitoh (set! optargname "=float")
102 1.1.1.1.10.2 msaitoh (set! optargname (string-append "=" (substring (get "arg-type") 0 3)))
103 1.1.1.1.10.2 msaitoh )
104 1.1.1.1.10.2 msaitoh (set! optargname " ")
105 1.1.1.1.10.2 msaitoh )
106 1.1.1.1.10.2 msaitoh
107 1.1.1.1.10.2 msaitoh (if (not (exist? "deprecated"))
108 1.1.1.1.10.2 msaitoh (set! perl_usage (string-append perl_usage
109 1.1.1.1.10.2 msaitoh (sprintf "\n %-28s %s" (string-append
110 1.1.1.1.10.2 msaitoh (if (exist? "value") (string-append "-" (get "value") ",") " ")
111 1.1.1.1.10.2 msaitoh " --"
112 1.1.1.1.10.2 msaitoh (get "name")
113 1.1.1.1.10.2 msaitoh optargname)
114 1.1.1.1.10.2 msaitoh (get "descrip"))
115 1.1.1.1.10.2 msaitoh ) ) )
116 1.1.1.1.10.2 msaitoh (if optisarray
117 1.1.1.1.10.2 msaitoh (set! perl_usage (string-append perl_usage
118 1.1.1.1.10.2 msaitoh "\n - may appear multiple times"))
119 1.1.1.1.10.2 msaitoh )
120 1.1.1.1.10.2 msaitoh
121 1.1.1.1.10.2 msaitoh =][=
122 1.1.1.1.10.2 msaitoh
123 1.1.1.1.10.2 msaitoh ENDFOR each "flag" =]
124 1.1.1.1.10.2 msaitoh
125 1.1.1.1.10.2 msaitoh use Getopt::Long qw(GetOptionsFromArray);
126 1.1.1.1.10.2 msaitoh Getopt::Long::Configure(qw(no_auto_abbrev no_ignore_case_always));
127 1.1.1.1.10.2 msaitoh
128 1.1.1.1.10.2 msaitoh my $usage;
129 1.1.1.1.10.2 msaitoh
130 1.1.1.1.10.2 msaitoh sub usage {
131 1.1.1.1.10.2 msaitoh my ($ret) = @_;
132 1.1.1.1.10.2 msaitoh print STDERR $usage;
133 1.1.1.1.10.2 msaitoh exit $ret;
134 1.1.1.1.10.2 msaitoh }
135 1.1.1.1.10.2 msaitoh
136 1.1.1.1.10.2 msaitoh sub paged_usage {
137 1.1.1.1.10.2 msaitoh my ($ret) = @_;
138 1.1.1.1.10.2 msaitoh my $pager = $ENV{PAGER} || '(less || more)';
139 1.1.1.1.10.2 msaitoh
140 1.1.1.1.10.2 msaitoh open STDOUT, "| $pager" or die "Can't fork a pager: $!";
141 1.1.1.1.10.2 msaitoh print $usage;
142 1.1.1.1.10.2 msaitoh
143 1.1.1.1.10.2 msaitoh exit $ret;
144 1.1.1.1.10.2 msaitoh }
145 1.1.1.1.10.2 msaitoh
146 1.1.1.1.10.2 msaitoh sub processOptions {
147 1.1.1.1.10.2 msaitoh my $args = shift;
148 1.1.1.1.10.2 msaitoh
149 1.1.1.1.10.2 msaitoh my $opts = {
150 1.1.1.1.10.2 msaitoh [= (. perl_opts) =]'help' => '', 'more-help' => ''
151 1.1.1.1.10.2 msaitoh };
152 1.1.1.1.10.2 msaitoh my $argument = '[= argument =]';
153 1.1.1.1.10.2 msaitoh my $ret = GetOptionsFromArray($args, $opts, (
154 1.1.1.1.10.2 msaitoh [= (. perl_defs) =]
155 1.1.1.1.10.2 msaitoh 'help|?', 'more-help'));
156 1.1.1.1.10.2 msaitoh
157 1.1.1.1.10.2 msaitoh $usage = <<'USAGE';
158 1.1.1.1.10.2 msaitoh [= prog-name =] - [= prog-title =] - Ver. [= version =]
159 1.1.1.1.10.2 msaitoh USAGE: [= prog-name =] [ -<flag> [<val>] | --<name>[{=| }<val>] ]... [= argument =]
160 1.1.1.1.10.2 msaitoh [= (. perl_usage) =]
161 1.1.1.1.10.2 msaitoh -?, --help Display usage information and exit
162 1.1.1.1.10.2 msaitoh --more-help Pass the extended usage information through a pager
163 1.1.1.1.10.2 msaitoh
164 1.1.1.1.10.2 msaitoh Options are specified by doubled hyphens and their name or by a single
165 1.1.1.1.10.2 msaitoh hyphen and the flag character.
166 1.1.1.1.10.2 msaitoh USAGE
167 1.1.1.1.10.2 msaitoh
168 1.1.1.1.10.2 msaitoh usage(0) if $opts->{'help'};
169 1.1.1.1.10.2 msaitoh paged_usage(0) if $opts->{'more-help'};[=
170 1.1.1.1.10.2 msaitoh
171 1.1.1.1.10.2 msaitoh CASE argument =][=
172 1.1.1.1.10.2 msaitoh !E =][=
173 1.1.1.1.10.2 msaitoh ==* "[" =][=
174 1.1.1.1.10.2 msaitoh * =]
175 1.1.1.1.10.2 msaitoh
176 1.1.1.1.10.2 msaitoh if ($argument && $argument =~ /^[^\[]/ && !@$args) {
177 1.1.1.1.10.2 msaitoh print STDERR "Not enough arguments supplied (See --help/-?)\n";
178 1.1.1.1.10.2 msaitoh exit 1;
179 1.1.1.1.10.2 msaitoh }[=
180 1.1.1.1.10.2 msaitoh
181 1.1.1.1.10.2 msaitoh ESAC
182 1.1.1.1.10.2 msaitoh
183 1.1.1.1.10.2 msaitoh =]
184 1.1.1.1.10.2 msaitoh $_[0] = $opts;
185 1.1.1.1.10.2 msaitoh return $ret;
186 1.1.1.1.10.2 msaitoh }
187 1.1.1.1.10.2 msaitoh
188 1.1.1.1.10.2 msaitoh END { close STDOUT };
189