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:
trap
has a man page, justman bash
since it's a built-in (which you can know viatype trap
).trap
inman bash
? I found it withless
search bytrap
keyword but this takes time.man -P "less '+/^ *trap'" bash-builtins
You might turn this into a shell function, parameterized in the page name and search string.