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