本文介绍了有办法清除python argparse吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下脚本:

import argparse
parser1 = argparse.ArgumentParser()
parser1.add_argument('-a')
args1 = parser1.parse_args()

parser2 = argparse.ArgumentParser()
parser2.add_argument('-b')
args2 = parser2.parse_args()

我有几个问题:

  1. parse_args是一种一次性方法还是有一种方法可以清除添加新参数之前的参数? (例如,类似args1.clear()parser1.clear())
  2. 此脚本的结果不可用.尽管此脚本接受-a参数,它不接受'a'的任何值.也没有接受任何-b参数.有什么方法可以使任何论点真正发挥作用吗?
  3. 这是我的实际情况:我有2个脚本.两者导入相同具有初始化代码的文件(加载配置文件,创建记录器等),让我们将其称为init.py此init.py文件也会解析这些参数仅是因为它需要一个值.问题我需要其中一个脚本来接受其他参数.由于init.py使用一个参数执行某项操作,因此我无法等待parse_args.我该如何运作?
  1. Is parse_args a one-time method or is there a way to clear thearguments before adding new ones? (e.g. something likeargs1.clear() or parser1.clear())
  2. The result of this script is unusable. Although this script acceptsthe -a argument, it does not accept any value for 'a'. Nor does itaccept any -b argument. Is there some way to make any of the arguments really work?
  3. This is my actual scenario: I have 2 scripts. Both import the samefile which has initialization code (load config files, createloggers, etc.), lets call it init.py This init.py file also parsesthe arguments only because it needs one value from it. The problemis that I need one of the scripts to accept other arguments as well.Since init.py does something with one argument, I cannot wait withparse_args. How can I make it work?

这是我的脚本的输出:

[提示]#python2.7 myscript.py -a

[prompt]# python2.7 myscript.py -a

用法:a.py [-h] [-a A]

usage: a.py [-h] [-a A]

myscript.py:错误:参数-a:预期一个参数

myscript.py: error: argument -a: expected one argument

[提示]#python2.7 myscript.py -a 1

[prompt]# python2.7 myscript.py -a 1

命名空间(a ='1')

Namespace(a='1')

用法:a.py [-h] [-b B]

usage: a.py [-h] [-b B]

myscript.py:错误:无法识别的参数:-a 1

myscript.py: error: unrecognized arguments: -a 1

推荐答案

您的情况尚不清楚,但是我想您正在寻找的是 parse_known_args

Your scenario is quite unclear, but I guess what you're looking for is parse_known_args

在这里我猜您是从其他文件(例如caller1.pycaller2.py

Here I guessed that you called init.py from the other files, say caller1.py and caller2.py

还假设init.py仅解析-a参数,而原始脚本将解析其余参数.

Also suppose that init.py only parses -a argument, while the original script will parse the rest.

您可以执行以下操作:

放在do_things方法中

parser = argparse.ArgumentParser()
parser.add_argument('-a')
parsed = parser.parse_known_args(sys.argv)
print 'From init.py: %s' % parsed['a']

caller1.py中:

init.do_things(sys.argv)
parser = argparse.ArgumentParser()
parser.add_argument('-b')
parsed = parser.parse_known_args(sys.argv)
print 'From caller1.py: %s' % parsed['b']

如果按以下方式调用caller1.py:python caller1.py -a foo -b bar,结果将是:

If you call caller1.py as follows: python caller1.py -a foo -b bar, the result will be:

From init.py: foo
From caller1.py: bar

但是如果您的情况实际上不是这样,我建议使用@ Michael0x2a答案,该答案只是在caller1.py中使用单个ArgumentParser对象,并为init.py

But if your scenario is not actually like this, I would suggest to use @Michael0x2a answer, which is just to use single ArgumentParser object in caller1.py and pass the value appropriately for init.py

这篇关于有办法清除python argparse吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:03