コンソールに印刷するのではなく、特定のbashコマンドのヘルプをless(manのように)としてマークする方法

コンソールに印刷するのではなく、特定のbashコマンドのヘルプをless(manのように)としてマークする方法

trapたとえば、コマンドマニュアルページがありません。ランニング:

man trap

減らす:

No manual entry for trap

ランニング:

trap --help

lessdoと同じ方法を使用せずにbashコンソールに直接ヘルプを印刷しますman。そしてコンソール出力をブロックするのは不便です。ヘルプ情報を次にリダイレクトless:よりユーザーフレンドリー

trap --help | less

しかし、毎回助けを得るためにそのような長いコマンドを印刷することは便利ではありません。短く設定する方法(エイリアスなど) - たとえば、これを使用するかhelp2 traphelp2既存のhelpコマンドの動作をオーバーライドするのが正しくない可能性があるため)、短く使用する方が良いですかhp trap(hpはヘルプワードの略です)。

ベストアンサー1

次の機能を追加します~/.bashrc

# Show help for command in less like man. Usage: hp cmd. For example:
# hp trap
hp() { "$@" --help | less; }

開いているbashコンソールに表示されるようにインポートします(またはコンソールを再度開きます)。

source ~/.bashrc

使用法:

hp some_shell_command

使用例:

hp trap

修正する

hp() { "$1" --help | less; } スクリプト引数を持つサポートコマンドに置き換えられました hp() { "$@" --help | less; }python test.py --help

次のことを確認しました(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

これらすべての場合は有効です。ありがとうイルカチョそしてクリス・デイビス貴重なアドバイスありがとうございます!

同じ内容でtest.pyと「te st.py」を使用してください。

#!/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)

出力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

helpman、コマンドに関する質問info


このソリューションは、次の質問の助けを借りて作成されました。

おすすめ記事