无法让Argparse正确解析我的论点
总Python Newb。对不起。
无论如何,我正在使用pycharm,并且正在尝试将争论传递给我的python脚本。在pycharm中,在“编辑运行配置”对话框中,在参数文本框中,我有以下文本:
-s "C:\temp\" -a "C:\Temp\archive\"
在脚本中,我有一个称为parseandvalidatearguments的函数,如下所示:
def ParseAndValidateArguments():
parser = argparse.ArgumentParser()
parser.add_argument("-q", default=False, action="store_true")
parser.add_argument("-s", type=str, required=True)
parser.add_argument("-a", type=str, required=True)
args = parser.parse_args()
发生的是-s的参数最终出现在“ c:\ temp” -a“ c:\ temp \ archive”,一个参数一无所获,导致了以下错误:
usage: SmArchive.py [-h] [-q] -s S -a A
SmArchive.py: error: the following arguments are required: -a
Process finished with exit code 2
我已经尝试了-s和目录之间的空格,我尝试过没有。我不知道我在做什么错。
请帮忙。
先感谢您。
(顺便说一句,我是一个老C#家伙,我告诉您,他们需要将Argparse移植到.NET框架上,因为.NET中的控制台应用程序只有一系列字符串。没有其他。 ,只是弦乐,您不知道它是一个论点或价值,或者是什么。
Total python newb. Sorry.
Anyway, I'm using PyCharm and I'm trying to pass arguments to my python script. In pycharm, in the 'Edit Run Configuration' dialog box, in the Parameters text box, I have the following text:
-s "C:\temp\" -a "C:\Temp\archive\"
In my script I have a function called ParseAndValidateArguments, shown below:
def ParseAndValidateArguments():
parser = argparse.ArgumentParser()
parser.add_argument("-q", default=False, action="store_true")
parser.add_argument("-s", type=str, required=True)
parser.add_argument("-a", type=str, required=True)
args = parser.parse_args()
What's happening is that the -s argument is ending up with "C:\temp" -a"C:\Temp\archive" and the -a argument is getting nothing, resulting in the following error:
usage: SmArchive.py [-h] [-q] -s S -a A
SmArchive.py: error: the following arguments are required: -a
Process finished with exit code 2
I've tried it with spaces between the -s and the directories, I've tried it without. I don't know what I'm doing wrong.
Please help.
Thank you in advance.
(By the way, I'm an old C# guy, and I'm telling you, they need to port argparse to the .NET framework because the console apps in .NET just get an array of strings. Nothing else. Here you go, just a chuck of strings, enjoy. You've got no idea if its an argument or a value, or what the heck it is. WTF????)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论