Comments (9)
Providing a patch to fix this problem. Apparently support for nargs=0 was
already in,
but there was a conditional preventing it from actually being used. Tested and
it works
great!
Original comment by [email protected]
on 18 Aug 2009 at 7:23
Attachments:
from argparse.
If it's just a flag, why don't you specify action='store_true'? Otherwise,
you're
getting the default action='store', which expects a value to store.
Original comment by [email protected]
on 18 Aug 2009 at 8:55
from argparse.
So maybe that means that nargs=0 needs to override the value of 'action' and
force it
to 'store_true', or throw an exception if it is anything other than
'store_true'. In
any case, I think it makes perfect sense for nargs to accept 0 as a value
(Especially
since the code already has support for it). Also, will specifying
action="store_true"
affect the help output? If not, that is another reason why I do not find your
proposed workaround acceptable.
Thanks for the info, though! I can extend the patch to address your concerns if
you'd
like. Would you prefer it throw an exception or silently override the value of
'action'?
Original comment by [email protected]
on 18 Aug 2009 at 8:58
from argparse.
'store_true' sets nargs=0 and so yes, it does display the way you want. What do
you
want nargs=0 to mean for the plain 'store' action? What should it store?
Original comment by [email protected]
on 18 Aug 2009 at 9:05
from argparse.
Hmm after studying the code a bit more, I think I understand where I'm getting
confused.
nargs=0 has many different meanings as far as actions go. Many actions set
nargs to
0, so it would be impossible to assume the user meant StoreTrueAction, or
StoreFalseAction.
I think an appropriate fix for this would be to make the error message for
nargs=0 a
bit more helpful. For example:
pa.add_argument( '--my-option', nargs=0 )
This should spit out:
You specified nargs=0 for the action 'store', which is not valid. The following
actions can be specified which will set nargs to 0 for you:
store_const
store_true
store_false
...
This would be very much more helpful! It may seem a bit over-kill for such a
narrow,
insignificant issue, but it will help redirect people (like me) in the right
direction when their intention was simply to make an option take no arguments.
Thanks for hearing me out!
Original comment by [email protected]
on 18 Aug 2009 at 9:42
from argparse.
Yeah, I think that's a reasonable request. It could at least say something like
"nargs for store action must be > 0; if you have nothing to store, you might
consider
other actions such as store_true". That would be an improvement.
(I don't really want to list all the possible actions because that will
probably get
out of sync, and I suspect that folks who try specifying nargs=0 probably wanted
action='store_true'.)
Original comment by [email protected]
on 19 Aug 2009 at 1:39
from argparse.
Original comment by [email protected]
on 19 Aug 2009 at 3:02
- Changed title: improve exception message for nargs=0
- Changed state: Accepted
from argparse.
Correct, it could get out of sync.. but in my example (which was entirely an
example
and very over-engineered) you would use string formatting to input the valid
action
types, that way they never are out of sync. However, your version is perfectly
acceptable and is indeed an improvement.
Thanks for taking care of this! argparse is great, I hope it gets accepted into
the
standard library (optparse sucks).
Original comment by [email protected]
on 19 Aug 2009 at 3:06
from argparse.
Error messages improved as of r47:
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', nargs=0)
Traceback (most recent call last):
...
ValueError: nargs for store actions must be > 0; if you have nothing to store,
actions such as store true or store const may be more appropriate
Original comment by [email protected]
on 12 Sep 2009 at 4:27
- Changed state: Fixed
from argparse.
Related Issues (20)
- Feature Request: Ability to remove argument groups HOT 1
- sub command set_defaults behaviour HOT 1
- Parsing multiple merged short options eats characters HOT 1
- Subcommands not printed in the same order they were added HOT 4
- __all__ is not complete HOT 1
- Handling arguments after -- HOT 3
- sha1sum of argparse-1.1.zip does not match HOT 3
- add_mutually_exclusive_group more than once has incorrectly formatted help. HOT 1
- re-license argparse package under same license as py 2.7 stdlib argparse code HOT 8
- argparse-1.1.zip is not same archive on pypi / on google code HOT 4
- About this issue tracker HOT 2
- argparse 1.2 incompatible with Python 3.1 HOT 10
- When -h is used, default values that fail should not matter HOT 2
- positional arguments before options cause all options to be in REMAINDER
- Argparse is not testable with nosetest HOT 1
- `choices=[...]` overrides `nargs='*'` HOT 1
- Serious issues with nested mutex groups in help output HOT 2
- Bug with negative numbers in scientific notation HOT 1
- pypi version does not handle empty arguments when fromfile_prefix_chars set (python issue 12353)
- .add_mutually_exclusive_group() only works when child of parser but not when a child of argument group
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from argparse.