version/0.2
[shelmfish.git] / helpmselect
bloba9f179032cfefd37b372c21aadfab6f8ed7814a2
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="helpmselect"
17 PROGRAM_VERSION="0.1"
18 # <<"HelpMessage"
19 #NAME
20 # helpmselect - filter out sections from a HelpMessage
21 #SYNOPSIS
22 # helpmselect [<options>] [--] <format>[-] [<file>]
23 #HelpMessage
25 GETOPT_OPT=r:k:HV
26 GETOPT_LOPT=remove:,keep:,no-header,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 REMOVE=''; _REMOVE=
39 KEEP=''
40 HEADER=1
41 while [ $# -gt 0 ]
43 case $1 in
44 -V|--version) echo "$PROGRAM_NAME version $PROGRAM_VERSION"; exit ;;
45 -r|--remove) shift; REMOVE="$REMOVE${_REMOVE:+ }$1"; _REMOVE=set ;;
46 -k|--keep) shift; KEEP="$KEEP$1" ;;
47 -H|--no-header) HEADER=0 ;;
48 --) shift; break ;;
49 *) break ;;
50 esac
51 shift
52 done
53 [ -z "$_REMOVE" ] && REMOVE='^$'
54 if [ $# -gt 1 ]
55 then echo >&2 "$PROGRAM_NAME: error: too many arguments"; exit 255
57 if [ $# -gt 0 ] && expr "$1" : '[a-zA-Z_][a-zA-Z_0-9]*=' >/dev/null
58 then set -- ./"$1"
61 awk -v KEEP="$KEEP" -v REMOVE="$REMOVE" '
62 BEGIN {
63 keep = '$HEADER' # keep or not the header
65 # section title
66 /^([^ \t#\/]|\/[^\/])/ {
67 if ($0 ~ KEEP) keep = 1
68 else keep = 0
69 if ($0 ~ REMOVE) keep = 0
71 # keep
72 keep {
73 print
74 }' "${1--}"
76 # <<"HelpMessage"
77 #DESCRIPTION
78 # helpmselect removes some sections from a HelpMessage read from <file> or
79 # from the standard input. By default all the sections are kept, and none
80 # are removed.
82 # If a section matches the ERE (awk Extended Regular Expression) given to
83 # --remove, it is removed. Otherwise if it does not match the ERE given to
84 # --keep, it is removed. The remaining sections are kept in the standard
85 # output. If several identical options are found, the value used is the
86 # concatenation of the options separated by spaces.
88 # If --no-header is given, anything that comes before the first section is
89 # removed.
90 #OPTIONS
91 # The options are parsed by getopt(1).
92 # -k, --keep <regex>
93 # -r, --remove <regex>
94 # -H, --no-header
95 #EXIT STATUS
96 # A non zero exit status would indicate an error for awk itself.
97 #EXAMPLES
98 # In a shell script, make a usage message from the embedded HelpMessage:
99 # $ helpm4sh $0 2>/dev/null |
100 # helpmselect --no-header --keep 'SYNOPSIS|OPTIONS' |
101 # helpm2text
102 #AUTHOR
103 # helpmselect was written by G.raud Meyer.
104 #SEE ALSO
105 # helpmessage(5), helpm2pod(1), helpm2text(1)
106 #HelpMessage