Python3命令行和argparse问题
请在下面找到我根据作业编写的代码以及我收到的错误消息。我是python新手,所以如果这是非常明显的,我会提前道歉Python3命令行和argparse问题,python,python-3.x,Python,Python 3.x,请在下面找到我根据作业编写的代码以及我收到的错误消息。我是python新手,所以如果这是非常明显的,我会提前道歉 def get_input_args(): """ Retrieves and parses the 3 command line arguments provided by the user when they run the program from a terminal window. This function uses Python's
def get_input_args():
"""
Retrieves and parses the 3 command line arguments provided by the user when
they run the program from a terminal window. This function uses Python's
argparse module to created and defined these 3 command line arguments. If
the user fails to provide some or all of the 3 arguments, then the default
values are used for the missing arguments.
Command Line Arguments:
1. Image Folder as --dir with default value 'pet_images'
2. CNN Model Architecture as --arch with default value 'vgg'
3. Text File with Dog Names as --dogfile with default value 'dognames.txt'
This function returns these arguments as an ArgumentParser object.
Parameters:
None - simply using argparse module to create & store command line arguments
Returns:
parse_args() -data structure that stores the command line arguments object
"""
parser = argparse.ArgumentParser()
parser.add_argument('--dir', type = str, default = 'pet_images/', help = 'path to the folder of pet images')
parser.add_argument('--arch', type = str, default = 'vgg', help = 'CNN model Architecture VGG')
parser.add_argument('--dogfile', type = str, default = 'dognames.txt', help = 'Text File with Dog Names')
# Replace None with parser.parse_args() parsed argument collection that
# you created with this function**
args = parser.parse_args('--dir', '--arch', '--dogfile')
root@791d23aa2615:/home/workspace# python check_images.py
Traceback (most recent call last):
File "check_images.py", line 131, in <module>
main()
File "check_images.py", line 51, in main
in_arg = get_input_args()
File "/home/workspace/get_input_args.py", line 49, in get_input_args
args = parser.parse_args('--dir', '--arch', '--dogfile')
TypeError: parse_args() takes from 1 to 3 positional arguments but 4 were given
但是我不能得到我应该得到的默认值
* Doesn't Check the Command Line Arguments because 'get_input_args' hasn't been defined.
* Doesn't Check the Results Dictionary because 'get_pet_labels' hasn't been defined.
* Doesn't Check the Results Dictionary because 'classify_images' hasn't been defined.
* Doesn't Check the Results Dictionary because 'adjust_results4_isadog' hasn't been defined.
* Doesn't Check the Results Dictionary because 'calculates_results_stats' hasn't been defined.
** Total Elapsed Runtime: 0:0:10
parse_args在您的回溯中被指出是问题所在,您调用该方法的方式与它们在文档中描述的方式相反。parse_args默认情况下从命令行获取args,因此请尝试调用它args=parser.parse_args
否则,如果您想手动执行,您可以执行以下操作:parser.parse_args['-foo',foo']
医生们:谢谢大家的帮助 我找到了生成默认命令行的答案
return parser.parse_args()
在Charles的提示和更多帮助下,我终于找到了它谢谢Charles当我运行args=parser.parse_args时,我没有得到我应该得到的默认值。我得到的是:*不检查命令行参数,因为尚未定义“get\u input\u args”。*未检查结果字典,因为尚未定义“获取宠物标签”。*未检查结果字典,因为尚未定义“分类图像”。*未检查结果字典,因为尚未定义“adjust_results4_isadog”。*不检查结果字典,因为尚未定义“计算结果统计”。*总运行时间:0:0:1这不是回溯…这是家庭作业吗?如果你的家庭作业系统发送给你,那么我们不知道如何修复它。。。似乎是说您需要使用这些名称中的每一个来定义函数
return parser.parse_args()