7

I often open man pages of a certain CLI tool only to check a particular flag, e.g. man iptables to check -t flag.

Is there any tool that simplifies it? I could of course write a simple function in Bash grepping the contents of a man pages but I am looking for something that uses the structure of man pages to find exactly what I want (i.e. description of a certain flag).

1
  • I'm not sure if something similar exist or not, but since man is using less by default, you can type / followed by the flag, and you can then search for it. e.g. /-t , cos you may lose many things by just using grep. Commented Aug 11, 2016 at 10:15

4 Answers 4

3

Assuming your man pager is less, you can pass any command to less beforehand using the environment variable LESS.

So for searching for the -t option of man iptables:

LESS='+/-t' man iptables

This has the same effect of running /-t within man ipatbles. You can change the pattern for finer control.

If you want, you can make a function for easier access:

search_man () { LESS=+/"$2" man "$1" ;}

Now doing:

search_man iptables '-t'              

will have the same effect.


EDIT:

If you want to go to the specific option of the man page rather than searching, you can use Regex matching with LESS:

LESS='+/^[[:blank:]]+-t' man iptables

would take you straight to the -t option description of man iptables. You can also define a function likewise:

search_man () { LESS=+/^[[:blank:]]+"$2" man "$1" ;}
3
  • 1
    What it does effectively is it searches for the flag in the man page. I want it to take me to the flag immediately. See my answer, it looks like the best way to do this. Commented Aug 11, 2016 at 14:30
  • 1
    @ΔλЛ LESS can handle Regex, check my edits.. Commented Aug 11, 2016 at 14:35
  • I know, and your solution works well after this fix. Still I think the solution in my answer is cleaner. Note that none of this solution directly queries man database, which is what I really want. Commented Aug 11, 2016 at 14:39
2

I haven't found a API/mechanism for querying man pages for a certain flag. However, this simple function seems to do exactly what I need:

function manswitch () { man $1 | less -p "^ +$2" }

Usage:

manswitch iptables -t
2

I can't comment yet, but here is a more flexible variant of the nice manswitch function in the accepted answer, which also finds options like long options which are listed after other options:

function manswitch () { man "$1" | less -p "^ +(-.*, )?$2" }

For example, manswitch ls -r and manswitch ls --reverse both then work. With the original manswitch, only the first works as the relevant line in the man page is -r, --reverse.

0

There is one thing. Now. This function:

flag() {
    man "$1" | grep -- "$2";
}

It works like this:

$ flag iptables -t
iptables [-t table] {-A|-C|-D} chain rule-specification
ip6tables [-t table] {-A|-C|-D} chain rule-specification
iptables [-t table] -I chain [rulenum] rule-specification
iptables [-t table] -R chain rulenum rule-specification
iptables [-t table] -D chain rulenum
iptables [-t table] -S [chain [rulenum]]
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target
iptables [-t table] -E old-chain-name new-chain-name
target = -j targetname [per-target-options]
-t, --table table
           This  is  the  default table (if no -t option is passed). It
        iptables -t nat -n -L

Well, the last two lines are destroyed.

Anyhow, do you know how to add it to your .bashrc? Or do you prefer it as a script to your ~/bin?

VERSION 1.1

flag() {
    man "$1" | grep -A5 -- "$2";
}

$ flag iptables -t
       iptables [-t table] {-A|-C|-D} chain rule-specification

       ip6tables [-t table] {-A|-C|-D} chain rule-specification

       iptables [-t table] -I chain [rulenum] rule-specification

       iptables [-t table] -R chain rulenum rule-specification

       iptables [-t table] -D chain rulenum

       iptables [-t table] -S [chain [rulenum]]

       iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]

       iptables [-t table] -N chain

       iptables [-t table] -X [chain]

       iptables [-t table] -P chain target

       iptables [-t table] -E old-chain-name new-chain-name

       rule-specification = [matches...] [target]

       match = -m matchname [per-match-options]

       target = -j targetname [per-target-options]

DESCRIPTION
       Iptables  and ip6tables are used to set up, maintain, and inspect the tables of IPv4 and IPv6 packet filter rules in the Linux kernel.  Several different tables may be defined.  Each table contains a
       number of built-in chains and may also contain user-defined chains.

--
       -t, --table table
              This option specifies the packet matching table which the command should operate on.  If the kernel is configured with automatic module loading, an attempt will be made to load the appropriate
              module for that table if it is not already there.

              The tables are as follows:

--
                  This is the default table (if no -t option is passed). It contains the built-in chains INPUT (for packets destined to local sockets), FORWARD (for packets being routed  through  the  box),
                  and OUTPUT (for locally-generated packets).

              nat:
                  This  table is consulted when a packet that creates a new connection is encountered.  It consists of three built-ins: PREROUTING (for altering packets as soon as they come in), OUTPUT (for
                  altering locally-generated packets before routing), and POSTROUTING (for altering packets as they are about to go out).  IPv6 NAT support is available since kernel 3.7.
--
               iptables -t nat -n -L
              Please note that it is often used with the -n option, in order to avoid long reverse DNS lookups.  It is legal to specify the -Z (zero) option as well, in which case the chain(s) will be atom‐
              ically listed and zeroed.  The exact output is affected by the other arguments given. The exact rules are suppressed until you use
               iptables -L -v

       -S, --list-rules [chain]
5
  • It would also match "delta-transfer" or "--omit-dir-times" from the rsync man page. At the very least you probably ought to constrain the end of the RE grep -- "$1\b" Commented Aug 11, 2016 at 12:29
  • You also might want to use grep -A5 so that the results include a few lines of context. Commented Aug 11, 2016 at 12:30
  • -A5 is a great improvement. See update. With "$1\b" I still need come to terms. It should be included in Release 1.2. Commented Aug 11, 2016 at 13:10
  • I don't see much difference. What's that supposed to do? Commented Aug 11, 2016 at 13:12
  • sorry should have been "$2\b". Try flag rsync -t and you'll see the difference with and without \b Commented Aug 11, 2016 at 13:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.