version/0.2
[shelmfish.git] / gitparseopt2helpm
blob2e1007b426aea6da0a4bf9ee5b66f46d19248ac7
1 #!/bin/sh
2 # Copyright © 2014 Géraud Meyer <graud@gmx.com>
4 # This program is free software; you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License version 2 as published by the
6 # Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 # details.
13 # You should have received a copy of the GNU General Public License along with
14 # this program. If not, see <http://www.gnu.org/licenses/>.
16 PROGRAM_NAME="gitparseopt2helpm"
17 PROGRAM_VERSION="0.2"
18 # <<"HelpMessage"
19 #NAME
20 # gitparseopt2helpm - convert a git rev-parse opts_spec string to a HelpMessage
21 #SYNOPSIS
22 # gitparseopt2helpm [<options>] [--] [<file>]
23 #HelpMessage
25 GETOPT_OPT=oV
26 GETOPT_LOPT=options,version
27 if getopt -T >/dev/null
28 then CL=`getopt $GETOPT_OPT $*`
29 else CL=`getopt -o $GETOPT_OPT -l $GETOPT_LOPT -- "$@"`
31 if [ $? -ne 0 ]
32 then echo >&2 "$PROGRAM_NAME: syntax error"; exit 255
34 if getopt -T >/dev/null
35 then set -- $CL
36 else eval set -- "$CL"
38 SKIPHEAD=0
39 while [ $# -gt 0 ]
41 case $1 in
42 -V|--version) echo "$PROGRAM_NAME version $PROGRAM_VERSION"; exit ;;
43 -o|--options) SKIPHEAD=1 ;;
44 --?*) echo >&2 "$PROGRAM_NAME: error: invalid option"; exit 255 ;;
45 --) shift; break ;;
46 *) break ;;
47 esac
48 shift
49 done
50 if [ $# -gt 1 ]
51 then echo >&2 "$PROGRAM_NAME: error: too many arguments"; exit 255
53 if [ $# -gt 0 ] && expr "$1" : '[a-zA-Z_][a-zA-Z_0-9]*=' >/dev/null
54 then set -- ./"$1"
57 awk -v PROGRAM_NAME="$PROGRAM_NAME" '
58 function opt_spec(spec) {
59 if (sub(/^([a-zA-Z0-9]|[a-zA-Z0-9]([=*?!]|[=*?!][=*?!]))(,|$)/, "-&", spec))
60 sub(/^-(.|.([=*?!]|[=*?!][=*?!])),/, "& --", spec)
61 else
62 sub(/^/, "--&", spec)
63 return spec
66 BEGIN {
67 skiphead = '$SKIPHEAD'
68 singlesynopsis = 0; options = 0
70 FNR == 1 && !skiphead {
71 print "NAME"
72 print "\t" $1
73 print "SYNOPSIS"
74 print "\t" $0
75 print "DESCRIPTION"
76 next
78 FNR == 2 && /^[ \t]*$/ { singlesynopsis = 1; next } # 2nd line should be empty
79 # detect the end of the description
80 /^--$/ { options = 1; if (!skiphead) print "OPTIONS"; next }
81 !options && !skiphead { print "\t" $0 }
83 # skip empty lines
84 /^$/ { next }
85 # option group summary
86 options && /^[ \t]/ {
87 sub(/^[ \t]*/, "")
88 print " " $0
89 next
91 # any other line is an option description
92 options {
93 print " \t" opt_spec($1)
94 sub(/^[^ \t]*[ \t]*/, "")
95 if ($0) print "\t\t" $0
97 END {
98 if (options == 0) {
99 print PROGRAM_NAME ": error: options spec not found" > "/dev/fd/2"
100 exit 1
102 if (singlesynopsis == 0 && skiphead == 0) {
103 print PROGRAM_NAME ": error: invalid multiline synopsis found" > "/dev/fd/2"
104 exit 2
106 }' "${1--}"
108 # <<"HelpMessage"
109 #DESCRIPTION
110 # gitparseopt2helpm converts a specification of options to be parsed by
111 # C<git rev-parse --parseopt>, and converts it to a simple HelpMessage
112 # printed to the standard output.
114 # It <file> is not given or it is a dash, the specification is read from
115 # standard input.
116 #OPTIONS
117 # The options are parsed by getopt(1).
118 # -o, --options
119 # Output only a single section (without the title) that lists the
120 # options.
121 #EXIT STATUS
122 # If no option spec was found or if the synopsis contains multiple lines the
123 # return status is non zero.
124 #EXAMPLES
125 # Extract an option specification from a script and build a manual page:
126 # $ awk '/^"/ && p {exit}; p {print}; /^OPTS_SPEC="\\/ {p=1}' myprog.sh \
127 # |gitparseopt2helpm |helpm2pod --man >myprog.1
128 #AUTHOR
129 # gitparseopt2helpm was written by G.raud Meyer.
130 #SEE ALSO
131 # git-rev-parse(1), helpmessage(5)
132 #HelpMessage