1######################################################################## 2## 3## generate count description for usage-function, i.e. s.th. like 4## 3...6 integer values 5## or 6## 1 or more string values 7## 8proc usageCount {opt} { 9 global D typeMap 10 11 foreach {cmin cmax} $D($opt,count) break 12 13 append usage $cmin 14 if {[Infty $cmax]} { 15 append usage " or more" 16 } elseif { $cmax>$cmin } { 17 append usage "...$cmax" 18 } 19 20 if {"$opt"!="--"} { 21 append usage " $typeMap(C,$D($opt,type))" 22 } 23 24 if { $cmax==1 && $cmin==1} { 25 append usage " value" 26 } else { 27 append usage " values" 28 } 29} 30######################################################################## 31proc genUsage {} { 32 global D 33 34 ##### 35 ##### generate synopsis 36 ##### 37 set usage {} 38 foreach opt $D(opts) { 39 if {[string match "--" $opt]} continue 40 41 set optional [expr {![info exist D($opt,mandatory)]}] 42 43 if {$optional} { 44 append usage " \[$opt" 45 } else { 46 append usage " $opt" 47 } 48 49 if { $D($opt,type)!="Flag" } { 50 if {0==[lindex $D($opt,count) 0]} { 51 append usage " \[$D($opt,var)\]" 52 } else { 53 append usage " $D($opt,var)" 54 } 55 } 56 57 if $optional { append usage "\]" } 58 } 59 60 if { [info exist D(--,type)] } { 61 append usage " \[--\] " 62 foreach {cmin cmax} $D(--,count) break 63 if { $cmin==0 } {append usage "\["} 64 append usage "$D(--,var)" 65 if { [Infty $cmax] || $cmax>1 } {append usage " ..."} 66 if { $cmin==0 } {append usage "\]"} 67 } 68 regsub -all "\n" $D(usage) "\\n\n " s 69 append usage "\\n\n" 70 append usage " $s\\n\n" 71 72 ##### 73 ##### find maximum length of options 74 ##### 75 if { [info exist D(--,var)] } { 76 set max [string length $D(--,var)] 77 } else { 78 set max 0 79 } 80 foreach opt $D(opts) { 81 set l [string length $opt] 82 if { $l>$max } {set max $l} 83 } 84 incr max 2 85 86 ##### 87 ##### description of every option 88 ##### 89 foreach opt $D(opts) { 90 if {[string match "--" $opt]} continue 91 92 set l [string length $opt] 93 append usage [b [expr $max-$l]] 94 set tab [b [expr $max+2]] 95 regsub -all "\n" $D($opt,usage) "\\n\n$tab" s 96 append usage "$opt: $s\\n\n" 97 98 if { $D($opt,type)=="Flag" } continue 99 100 ##### count 101 append usage [b [expr $max+2]] [usageCount $opt] 102 103 ##### range 104 if {[info exist D($opt,range)]} { 105 foreach {rmin rmax} $D($opt,range) break 106 if {!([negInfty $rmin] && [Infty $rmax])} { 107 append usage " between $rmin and $rmax" 108 } 109 } 110 111 ##### default: 112 if {[info exist D($opt,default)]} { 113 append usage "\\n\n[b $max] default:" 114 foreach x $D($opt,default) { 115 append usage " `$x'" 116 } 117 } 118 append usage "\\n\n" 119 } 120 ##### 121 ##### description of non-option arguments 122 ##### 123 if {[info exist D(--,var)]} { 124 set l [string length $D(--,var)] 125 append usage [b [expr $max-$l]] 126 set tab [b [expr $max+2]] 127 regsub -all "\n" $D(--,usage) "\\n\n$tab" s 128 129 append usage "$D(--,var): $s\\n\n" 130 append usage [b [expr $max+2]] [usageCount --] "\\n\n" 131 } 132 133 ##### 134 ##### version string 135 ##### 136 if {[info exist D(version)]} { 137 append usage "version: $D(version)\\n\n" 138 } 139 140 set res {} 141 append res "void\n" \ 142 "usage(void)\n" \ 143 "{\n" 144 foreach line [split $usage \n] { 145 append res " fprintf(stderr,\"%s\",\" $line\");\n" 146 } 147 148 append res " exit(EXIT_FAILURE);\n" \ 149 "}\n" \ 150 "/***********************************" \ 151 "***********************************/" 152 return $res 153} 154######################################################################## 155