Add the following function to ~/.bashrc:
# Show help for command in less like man. Usage: hp cmd. For example:
# hp trap
hp() { "$@" --help | less; }
Source it to make visible in the opened bash console (or just reopen console):
source ~/.bashrc
Usage:
hp some_shell_command
Example of usage:
hp trap
Update
hp() { "$1" --help | less; }
replaced to
hp() { "$@" --help | less; }
to support commands with script parameter like python test.py --help
I checked the following cases (with Python 3.8.10):
hp python # executed as: python --help | less
hp python test.py # executed as: python test.py --help | less
hp python "te st.py" # executed as: python "te st.py" --help | less
hp ./test # executed as: ./test.py --help | less
hp "./te st" # executed as: "./te st.py" --help | less
All this cases are working.
Thanks to ilkkachu and Chris Davies for theirs great advices!
Used test.py and "te st.py" with the following same content:
#!/bin/python
Python code here taken from the following answer on the question:
How do I access command line arguments? [duplicate]
https://stackoverflow.com/a/42929351/1455694
import argparse
parser = argparse.ArgumentParser("simple_example")
parser.add_argument("counter", help="An integer will be increased by 1 and printed.", type=int)
args = parser.parse_args()
print(args.counter + 1)
Output for hp python "te st.py":
usage: simple_example [-h] counter
positional arguments:
counter An integer will be increased by 1 and printed.
optional arguments:
-h, --help show this help message and exit
Related questions about help, man, info commands:
This solution made with the help of the questions:
traphas a man page, justman bashsince it's a built-in (which you can know viatype trap). – Ginnungagap Oct 06 '23 at 20:36trapinman bash? I found it withlesssearch bytrapkeyword but this takes time. – Anton Samokat Oct 07 '23 at 13:58man -P "less '+/^ *trap'" bash-builtinsYou might turn this into a shell function, parameterized in the page name and search string.
– TheNotoriousGBR Oct 13 '23 at 10:22