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
="helpmselect"
20 # helpmselect - filter out sections from a HelpMessage
22 # helpmselect [<options>] [--] <format>[-] [<file>]
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 -- "$@"`
32 then echo >&2 "$PROGRAM_NAME: syntax error"; exit 255
34 if getopt
-T >/dev
/null
36 else eval set -- "$CL"
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 ;;
53 [ -z "$_REMOVE" ] && REMOVE
='^$'
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
61 awk -v KEEP
="$KEEP" -v REMOVE
="$REMOVE" '
63 keep = '$HEADER' # keep or not the header
66 /^([^ \t#\/]|\/[^\/])/ {
67 if ($0 ~ KEEP) keep = 1
69 if ($0 ~ REMOVE) keep = 0
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
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
91 # The options are parsed by getopt(1).
93 # -r, --remove <regex>
96 # A non zero exit status would indicate an error for awk itself.
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' |
103 # helpmselect was written by G.raud Meyer.
105 # helpmessage(5), helpm2pod(1), helpm2text(1)