为什么 argparse 不接受参数并打印新的 "magic" 用法?

Why argparse do not accept arguments and prints new "magic" usage?

我使用 argparse 编写了一个小的 python 脚本来处理命令行选项。 它已经开始工作,直到它突然表现得很奇怪。 现在它有时会拒绝我的论点并打印出不同的用法。 我 运行 它在 Windows 8.1 使用 python3.4 任何人都知道什么会造成这样的问题?

parser = argparse.ArgumentParser(description="Encodes or decodes a file or folder.")
parser.add_argument("-e", "--encode", action="store_true", help="Specify mode: encode")
parser.add_argument("-d", "--decode", action="store_true", help="Specify mode: decode")
parser.add_argument("-p", "--password", action="store", metavar="password", help="Specify password.")
parser.add_argument("-f", "--file", help="Specify file/folder.")
parser.add_argument("-t", "--test", action="store_true", help="Runs unittests.")
args = vars(parser.parse_args())

更新

在 -t 的情况下,执行了以下代码:

testMode = args["test"]
#...
if (testMode):
    unittest.main()#this produced the error
    input("Press Enter to leave")
    exit()

解决方案:

testMode = args["test"]
#...
if (testMode):
    unittest.main(argv=[sys.argv[0]])
    input("Press Enter to leave")
    exit()

在 -t 的情况下,单元测试已启动:

unittest.main()

这产生了新的用法消息,因为它也在使用 argv 修复它使用

unittest.main(argv=[sys.argv[0]])