有没有办法清除python argparse? [英] is there a way to clear python argparse?

查看:45
本文介绍了有没有办法清除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 用一个参数做某事,我不能等待解析参数.我怎样才能让它发挥作用?
  1. Is parse_args a one-time method or is there a way to clear the arguments before adding new ones? (e.g. something like args1.clear() or parser1.clear())
  2. The result of this script is unusable. Although this script accepts the -a argument, it does not accept any value for 'a'. Nor does it accept 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 same file which has initialization code (load config files, create loggers, etc.), lets call it init.py This init.py file also parses the arguments only because it needs one value from it. The problem is that I need one of the scripts to accept other arguments as well. Since init.py does something with one argument, I cannot wait with parse_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

这里我猜你从其他文件中调用了 init.py,比如 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.

你可以这样做:

init.py 中把它放在 do_things 方法中:

in init.py put this in do_things method:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆