参数组帮助输出

时间:2017-04-09 11:35:04

标签: python python-2.7 argparse

我试图找出使用Argparse模块的解决方案。帮助输出和我的代码如下。代码似乎有效。

但是,帮助输出不是最佳的。我希望通过"用法:"进行沟通。那" -E"要求至少为-s DATE,并且-s和-e都是与-E一起使用时只有意义的参数。 (我知道我可以在帮助信息的文本中做到这一点,但我正在为用户寻找更明显的东西。)我尝试了subparser,这似乎让事情变得更加清晰。但是,我无法找到一种方法让subparser在没有开关的情况下接受参数。例如,我的s​​ubparser版本强迫我输入" SO_Test.py文件-f somefile.txt"而不是下面的代码允许" SO_Test.py -f somefile.txt"。对于更好的格式化帮助,我有哪些选择?

此致 Burzin

usage: SO_Test.py [-h] [-i | -f FILE | -E ENDPOINT] [-a] [-x] [-s DATE]
                  [-e DATE]

This is my test ArgParse script.

optional arguments:
  -h, --help   show this help message and exit
  -i           Run the script interactively. This is the default mode if n
               options are specfied.
  -f FILE      Run the script against an input file.
  -E ENDPOINT  Run the script against a specific endpoint.
  -a           Audit only.
  -x           Switches queries to use staging.
  -s DATE      Specify a start date. This is mandatory with the -E switch.
  -e DATE      Specify an end date. This optional with the -E switch.
import argparse
from time import time as timer
import sys
def valid_date(iso8601):
    pass
def valid_file(parser, fname):
    pass
    return fname
def file_mode(fname, audit, staging):
    print("Start in file mode.  Audit is {} and staging is {}.".format(audit, staging))
    sys.exit(0)
def endpoint_mode(endpoint, start_time, end_time, audit, staging):
    print("Start in endpoint mode.  Audit is {} and staging is {}.".format(audit, staging))
    sys.exit(0)
def interactive_mode(audit, staging):
    print("Start in interactive mode.  Audit is {} and staging is {}.".format(audit, staging))
def main():
    parser = argparse.ArgumentParser(description="This is my test ArgParse script.")
    group = parser.add_mutually_exclusive_group()
    group.add_argument("-i", \
                        dest="interactive", \
                        action="store_true", \
                        help="Run the script interactively.  This is the default " + \
                             "mode if no options are specfied.")
    group.add_argument("-f", \
                        dest="fname", \
                        help="Run the script against an input file.", \
                        metavar="FILE", \
                        type=lambda x: valid_file(parser, x))
    group.add_argument("-E", \
                        dest="endpoint",
                        help="Run the script against a specific endpoint.",
                        metavar="ENDPOINT")
    parser.add_argument("-a", \
                        dest="audit", \
                        action="store_true", \
                        help="Audit only.")
    parser.add_argument("-x", \
                        dest="staging", \
                        action="store_true", \
                        help="Switches queries to use staging.")
    parser.add_argument("-s", \
                        dest="start_time", \
                        help="Specify a start date.  This is mandatory with the -E switch.", \
                        metavar="DATE",
                        type=valid_date)
    parser.add_argument("-e", \
                        dest="end_time", \
                        help="Specify an end date.  This optional with the -E switch.", \
                        metavar="DATE")
    args = parser.parse_args()
    audit = args.audit
    staging = args.staging
    interactive = args.interactive
    fname = args.fname
    endpoint = args.endpoint
    start_time = args.start_time
    end_time = args.end_time
    if not len(sys.argv) > 1:
        interactive_mode(audit, staging)
    if args.interactive is True:
        interactive_mode(audit, staging)
    if '-f' in sys.argv:
        file_mode(fname, audit, staging)
    if '-E' in sys.argv:
        endpoint_mode(endpoint, start_time, end_time, audit, staging)

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

对于子分析师,您的file案例会从

更改
"SO_Test.py -f somefile.txt".

"SO_Test.py file somefile.txt".

file子分析符采用一个位置参数。

同样,可以将endpoint子分析符定义为需要endpoint位置,sdate位置(或必需的可选)和标记edate

我认为你不需要直接看sys.argvargs中有足够的信息。未给出的参数具有默认值,通常为None

args = parser.parse_args()
audit = args.audit     # True/False
staging = args.staging     # True/False
interactive = args.interactive    # True/False
fname = args.fname
endpoint = args.endpoint
start_time = args.start_time
end_time = args.end_time

# so if there's any commands do this?
if not len(sys.argv) > 1:
    interactive_mode(audit, staging)

# if args.interactive:
if args.interactive is True:
    interactive_mode(audit, staging)

# if fname is not None:
if '-f' in sys.argv:
    file_mode(fname, audit, staging)

# if endpoint is None:
if '-E' in sys.argv:
    endpoint_mode(endpoint, start_time, end_time, audit, staging)