我已经实现了一个Git命令,并使用git别名将其挂接到Git上,但是有没有一种方法来挂接Git帮助文档呢? 我正在Windows上运行,如果我输入git help mycmd
,会弹出一个窗口告诉我Git找不到git-mycmnd.html文件。
我使用Python实现了我的命令,那么有没有一种适当的Git方式来添加帮助文档,而不是“只知道”帮助文件必须放在哪里呢?
您可以为自定义别名编写文档,这些文档可以本地保存或通过网络在线访问。以下是其工作原理:
git config --local help.format html
cd ~/Documents
git clone git://git.kernel.org/pub/scm/git/git-htmldocs.git git-doc
假设你的命令是love
,那么:
cd ~/Documents/git-doc
touch gitlove.html
Add some text to the gitlove.html
cd YOUR_PROJECT_REPO
git config --local help.htmlpath ~/Documents/git-doc
git help love
,然后你的HTML页面将弹出。创建一个脚本(例如git-love
)并将其放置在系统的PATH路径下的一个目录中。
该脚本可以用Python编写,正如你所提到的,你已经使用Python实现了你的命令。
为你的git-love
脚本创建一个man页。将其命名为git-love.1
并将其放置在存储man页的适当目录中(例如/usr/local/share/man/man1/
)。man页将允许git help love
命令显示你的脚本的帮助文本,因为Git的帮助系统使用man页来显示帮助文本。
import argparse
import subprocess
import sys
def main():
parser = argparse.ArgumentParser(description='Description of your git love command.', add_help=False)
parser.add_argument('--help', action='store_true', help='Show help message and exit')
args, unknown_args = parser.parse_known_args()
if args.help:
# Display the man page
subprocess.run(['man', 'git-love'])
else:
# rest of your command implementation
if __name__ == '__main__':
main()
add_help=False
创建argparse.ArgumentParser
,以防止argparse
自动处理--help
标志。
向解析器添加自定义的--help
参数。如果提供了--help
作为参数,则使用subprocess.run
函数执行man git-love
命令,显示git-love.1
手册页。
否则,将运行您的命令实现的其余部分。
hello
的命令,它运行的是git show HEAD
。当我输入git help hello
时,返回的结果是'hello' 被设为 'show HEAD' 的别名
。 - evolutionxboxhelp.format
和help.htmlpath
的文档 这里。运行命令git config --local help.htmlpath "http://www.google.com"
,然后输入git help love
,这将为您提供一个开始的方向。 - William Kinaan