Python argparse with nargs behaviour incorrect

萝らか妹 提交于 2019-12-01 08:50:40

问题


Here is my argparse sample say sample.py

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-p", nargs="+", help="Stuff")
args = parser.parse_args()
print args

Python - 2.7.3

I expect that the user supplies a list of arguments separated by spaces after the -p option. For example, if you run

$ sample.py -p x y 
Namespace(p=['x', 'y'])

But my problem is that when you run

$ sample.py -p x -p y
Namespace(p=['y'])

Which is neither here nor there. I would like one of the following

  • Throw an exception to the user asking him to not use -p twice instead just supply them as one argument
  • Just assume it is the same option and produce a list of ['x','y'].

I can see that python 2.7 is doing neither of them which confuses me. Can I get python to do one of the two behaviours documented above?


回答1:


To produce a list of ['x','y'] use action='append'. Actually it gives

Namespace(p=[['x'], ['y']])

For each -p it gives a list ['x'] as dictated by nargs='+', but append means, add that value to what the Namespace already has. The default action just sets the value, e.g. NS['p']=['x']. I'd suggest reviewing the action paragraph in the docs.

optionals allow repeated use by design. It enables actions like append and count. Usually users don't expect to use them repeatedly, or are happy with the last value. positionals (without the -flag) cannot be repeated (except as allowed by nargs).

How to add optional or once arguments? has some suggestions on how to create a 'no repeats' argument. One is to create a custom action class.




回答2:


I ran into the same issue. I decided to go with the custom action route as suggested by mgilson.

import argparse
class ExtendAction(argparse.Action):
  def __call__(self, parser, namespace, values, option_string=None):
    if getattr(namespace, self.dest, None) is None:
      setattr(namespace, self.dest, [])
    getattr(namespace, self.dest).extend(values)
parser = argparse.ArgumentParser()
parser.add_argument("-p", nargs="+", help="Stuff", action=ExtendAction)
args = parser.parse_args()
print args

This results in

$ ./sample.py -p x -p y -p z w
Namespace(p=['x', 'y', 'z', 'w'])

Still, it would have been much neater if there was an action='extend' option in the library by default.



来源:https://stackoverflow.com/questions/19228516/python-argparse-with-nargs-behaviour-incorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!