1 [= AutoGen5 template foo=(base-name) -*- Mode: scheme -*-=]
6 (if (not (and (exist? "prog-name") (exist? "prog-title") (exist? "version")))
7 (error "prog-name and prog-title are required"))
8 (define prog-name (get "prog-name"))
10 (if (> (string-length prog-name) 16)
11 (error (sprintf "prog-name limited to 16 characters: %s"
13 (if (not (exist? "long-opts"))
14 (error "long-opts is required"))
16 ;; perl list containing string to initialize the option hash
18 ;; perl list containing option definitions for Getopt::Long
19 (define perl_defs " ")
21 (define perl_usage "")
23 (define optname-from "A-Z_^")
24 (define optname-to "a-z--")
27 (define q (lambda (s) (string-append "'" s "'")))
28 (define qp (lambda (s) (string-append "q{" s "}")))
34 (define optarg "") ;; the option argument for Getopt::Long
35 (define opttarget "''") ;; the value of a hash key that represents option
36 (define optargname "")
37 (define optisarray #f)
38 (define optname (string-tr! (get "name") optname-from optname-to))
41 ;; since autoopts doesn't support float we take the combination arg-name =
42 ;; float and arg-type = string as float
47 =* num =][= (set! optarg "=i") =][=
50 (if (and (exist? "arg-name") (== (get "arg-name") "float"))
56 (error (string-append "unknown arg type '"
57 (get "arg-type") "' for " (get "name"))) =][=
61 (if (exist? "stack-arg")
62 ;; set optarget to array reference if can take more than one value
63 ;; FIXME: if "max" exists, then just presume it is greater than 1
65 (if (and (exist? "max") (== (get "max") "NOLIMIT"))
67 (set! opttarget (string-append
69 (if (exist? "arg-default") (q (get "arg-default")) "")
75 (error "If stack-arg then max has to be NOLIMIT")
77 ;; just scalar otherwise
78 (if (exist? "arg-default") (set! opttarget (q (get "arg-default"))))
81 (set! perl_opts (string-append perl_opts
82 "'" (get "name") "' => " opttarget ",\n "))
84 (define def_add (string-append "'" optname (if (exist? "value")
85 (string-append "|" (get "value")) "") optarg "',"))
87 (define add_len (+ (string-length def_add) counter))
90 (set! perl_defs (string-append perl_defs "\n " def_add))
94 (set! perl_defs (string-append perl_defs " " def_add))
95 (set! counter (+ counter add_len))
99 (if (exist? "arg-type")
100 (if (and (exist? "arg-name") (== (get "arg-name") "float"))
101 (set! optargname "=float")
102 (set! optargname (string-append "=" (substring (get "arg-type") 0 3)))
104 (set! optargname " ")
107 (if (not (exist? "deprecated"))
108 (set! perl_usage (string-append perl_usage
109 (sprintf "\n %-28s %s" (string-append
110 (if (exist? "value") (string-append "-" (get "value") ",") " ")
117 (set! perl_usage (string-append perl_usage
118 "\n - may appear multiple times"))
123 ENDFOR each "flag" =]
125 use Getopt::Long qw(GetOptionsFromArray);
126 Getopt::Long::Configure(qw(no_auto_abbrev no_ignore_case_always));
138 my $pager = $ENV{PAGER} || '(less || more)';
140 open STDOUT, "| $pager" or die "Can't fork a pager: $!";
150 [= (. perl_opts) =]'help' => '', 'more-help' => ''
152 my $argument = '[= argument =]';
153 my $ret = GetOptionsFromArray($args, $opts, (
155 'help|?', 'more-help'));
158 [= prog-name =] - [= prog-title =] - Ver. [= version =]
159 USAGE: [= prog-name =] [ -<flag> [<val>] | --<name>[{=| }<val>] ]... [= argument =]
161 -?, --help Display usage information and exit
162 --more-help Pass the extended usage information through a pager
164 Options are specified by doubled hyphens and their name or by a single
165 hyphen and the flag character.
168 usage(0) if $opts->{'help'};
169 paged_usage(0) if $opts->{'more-help'};[=
176 if ($argument && $argument =~ /^[^\[]/ && !@$args) {
177 print STDERR "Not enough arguments supplied (See --help/-?)\n";
188 END { close STDOUT };