penchant
penchant

Reputation: 2323

In python argparse, is there a use case for nargs=1?

It seems as though it would always make more sense to use the default action of store without specifying nargs so the output is always as expected, instead of sometimes being a list and sometimes not. I'm just curious if I missed something..

example

>>> import argparse                                                                                                                     
>>> parser = argparse.ArgumentParser()                                                                                                  
>>> parser.add_argument('--foo')                                                                                                        
_StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)                                                                                                                                      
>>> parser.add_argument('--bar', nargs=1)                                                                                               
_StoreAction(option_strings=['--bar'], dest='bar', nargs=1, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args('--foo 1 --bar 1'.split())                                                                                        
Namespace(bar=['1'], foo='1')                                                                                                           
>>> parser.parse_args('')                                                                                                               
Namespace(bar=None, foo=None)                                                                                                           

Upvotes: 19

Views: 12989

Answers (2)

k-h
k-h

Reputation: 456

The case for nargs=1 for me is that I can use it with the "append" action, then if I want to use the option once, I get a list of one item, if I use it twice, I get a list of two items, etc.

Upvotes: 1

hpaulj
hpaulj

Reputation: 231365

Both the default nargs=None and nargs=1 expect one value, but nargs=1 will put it in a list, e.g.

Namespace(foo='test')
Namespace(foo=['test'])

nargs=3 will require 3 values, and put them in a list as well. * and + also put the values in a list.

From the docs, under nargs=N: https://docs.python.org/3/library/argparse.html#nargs

Note that nargs=1 produces a list of one item. This is different from the default, in which the item is produced by itself.

nargs=1 is just an instance of nargs=n. It's not a special case.

For you, as an argparse user, you probably don't need to use 1. In fact to me it's a sign of a novice - unless there's a clear need for the list in the output. I can imagine, for example, building a parser that programmatically sets n to some number, may be 3, maybe 5, maybe 1, and expects the result to always be a list.

Upvotes: 26

Related Questions