运行-h时,Python argparse AssertionError

时间:2019-06-20 03:39:11

标签: python argparse

我在python代码中使用了argparse,但是当我在命令行中运行以下命令时

python samplecode.py —h

我得到断言错误。

这是samplecode.py中的代码

import argparse

def getCommandLineData():
    parser = argparse.ArgumentParser(description='Extract candidate data from Talentforce Archive')
    parser.add_argument('--email', metavar='',required=True, help='Email of the candidate')
    parser.add_argument('--archiveDir', metavar='',required=False, help='Directory where Archived Data is kept')
    parser.add_argument('--chunksize',type=int , metavar='',required=False, help='Number of Rows CSV files will be broken into')
    parser.add_argument('--outputDir', metavar='',required=False, help='Output Directory for Extracted Data')
    parser.add_argument('--databasePath', metavar='',required=False, help='Full Path to database File')
    args = parser.parse_args()

getCommandLineData()

我得到的错误是

Traceback (most recent call last):
  File "samplecode.py", line 12, in <module>
    getCommandLineData()
  File "samplecode.py", line 10, in getCommandLineData
    args = parser.parse_args()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 1749, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 1781, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 1987, in _parse_known_args
    start_index = consume_optional(start_index)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 1927, in consume_optional
    take_action(action, args, option_string)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 1855, in take_action
    action(self, namespace, argument_values, option_string)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 1037, in __call__
    parser.print_help()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 2474, in print_help
    self._print_message(self.format_help(), file)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 2458, in format_help
    return formatter.format_help()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 284, in format_help
    help = self._root_section.format_help()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 215, in format_help
    item_help = join([func(*args) for func, args in self.items])
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 215, in <listcomp>
    item_help = join([func(*args) for func, args in self.items])
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py", line 339, in _format_usage
    assert ' '.join(opt_parts) == opt_usage
AssertionError

我不知道为什么要得到错误。如果我注释掉parser.add_argument行中的几行,错误就会消失。但是我无法建立模式或根本原因

0 个答案:

没有答案