添加"帮助"的简单方法模块到脚本

时间:2018-01-09 09:37:13

标签: python

我有一些我经常使用的简单python脚本,但有时我会忘记我是如何设置它们的(参数数量,参数顺序等)。此外,它们可能在某些时候对其他人有用。所以,我想添加一个简单的帮助模块。我现在这样做的方法是检查是否只给出了一个参数,以及这个参数是否是" help"。如果这是真的,则会打印一个小文本,说明脚本的作用以及所需的参数。下面给出的伪代码。我不认为这是理想的原因是因为我最终缩进我的脚本的主体,并在else子句中运行所有内容。如果" help"我觉得可能有一种更聪明的方式来打印帮助文本。作为参数给出。有什么想法吗?

#! /usr/bin/env python

import sys
import numpy as np
import re

if sys.argv[1:] == ['help']:
        print("""
================================HELP================================
This script generates a molden-readable file of all geometries
from an ORCA geometry optimization calculation. (Sometimes the
standard output file is not read by molden, for some reason...)

The script takes one argument: the output file
If the argument is "help", then the current messages is printed.
=================================================================""")

else:
        here comes the actual script

1 个答案:

答案 0 :(得分:2)

您可以使用sys.exit()在帮助消息

之后简单地终止程序
#! /usr/bin/env python

import sys
import numpy as np
import re

if sys.argv[1:] == ['help']:
        print("""
================================HELP================================
This script generates a molden-readable file of all geometries
from an ORCA geometry optimization calculation. (Sometimes the
standard output file is not read by molden, for some reason...)

The script takes one argument: the output file
If the argument is "help", then the current messages is printed.
=================================================================""")
        sys.exit()

#No need for indenting the rest of the script