Giter Club home page Giter Club logo

Comments (9)

GoogleCodeExporter avatar GoogleCodeExporter commented on August 12, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 12, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 12, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 12, 2024
'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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 12, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 12, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 12, 2024

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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 12, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 12, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.