你好,我在编写的函数中有一行,我无法找出错误所在。我编写了带有命令行解析的函数,尽管我未添加-a命令,但我编写的可选函数之一仍在运行。它无需我指定使用-a即可完成我想要执行的所有操作。
def parse_command_line():
"""
Parse the command line arguments and return the parse_args object.
There should be 1 positional argument and 6 optional arguments.
The help message generated by the parser should look like:
usage: cipher.py [-h] [-o outfile_path] [-k KEY] [-d] [-a] [-v] infile
positional arguments:
infile input file to be encrypted or decrypted
optional arguments:
-h, --help show this help message and exit
-o outfile_path, --outfile outfile_path
output file
-k KEY, --key KEY encryption/decryption key (must be positive) (default
= 1)
-d, --decrypt decrypt the input file
-a, --all decrypt using all keys [1, 25], save outputs in
different files. (useful in case the key is lost or
unknown)
-v, --verbose verbose mode
args:
None
returns:
args: generated argparse object with all the passed command line arguments
"""
parser = argparse.ArgumentParser()
#parser.add_argument('infile', type = str, help = 'input file to be encrypted of decrypted')
#parser.add_argument('-h', '--help', help = 'show this help message and exit')
parser.add_argument('-o', '--outfile', help = 'output file')
parser.add_argument('-k', '--key', default = 1, help = 'encryption/decryption key (must be postive) (default = 1)')
parser.add_argument('-d', '--decrypt', action = 'store_true', help = 'decrypt the input file')
parser.add_argument('-a', '--all', action = 'store_true', default = [1, 25], help = 'decrypt using all keys [1, 25], save outputs in diferent files. (useful in case the key is lost of unknown)')
parser.add_argument('-v', '--verbose', action = 'store_true', help = 'verbose mode')
parser.add_argument('infile', help = 'input file to be encrypted of decrypted')
args = parser.parse_args()
print('args:', args)
return args
程序更长,但是每次我从命令行运行测试命令时,它都会尝试使用每个密钥解密所有内容并将每个密钥保存到文件中。
%%bash
python3 cipher.py plain_message.txt -k 23 -v -o cipher_message.txt
我还有更多错误要调试,但是即使参数是可选的并且上面的语句中也缺少该参数,我也不明白为什么默认值仍在运行。我希望我不会在这里错过真正明显的东西。 -谢谢