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
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"
20 # gitparseopt2helpm - convert a git rev-parse opts_spec string to a HelpMessage
22 # gitparseopt2helpm [<options>] [--] [<file>]
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 -- "$@"`
32 then echo >&2 "$PROGRAM_NAME: syntax error"; exit 255
34 if getopt
-T >/dev
/null
36 else eval set -- "$CL"
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 ;;
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
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)
67 skiphead = '$SKIPHEAD'
68 singlesynopsis = 0; options = 0
70 FNR == 1 && !skiphead {
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 }
85 # option group summary
91 # any other line is an option description
93 print " \t" opt_spec($1)
94 sub(/^[^ \t]*[ \t]*/, "")
95 if ($0) print "\t\t" $0
99 print PROGRAM_NAME ": error: options spec not found" > "/dev/fd/2"
102 if (singlesynopsis == 0 && skiphead == 0) {
103 print PROGRAM_NAME ": error: invalid multiline synopsis found" > "/dev/fd/2"
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
117 # The options are parsed by getopt(1).
119 # Output only a single section (without the title) that lists the
122 # If no option spec was found or if the synopsis contains multiple lines the
123 # return status is non zero.
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
129 # gitparseopt2helpm was written by G.raud Meyer.
131 # git-rev-parse(1), helpmessage(5)