This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Argparse Revert config_file defaults
Type: enhancement Stage: resolved
Components: Extension Modules Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: bethard, d0n, paul.j3, r.david.murray
Priority: normal Keywords:

Created on 2014-06-19 03:49 by d0n, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg220960 - (view) Author: d0n (d0n) Date: 2014-06-19 03:49
Hi,

first of all I want to thank you (bethard) for your great work and efforts on developing argparse which is a great tool of versatile use in mostly all of my programs. 
I've faced a specific problem while implementing argparse which I have not been able to fix by the functions supplied through argparse or its functions. And here we are getting to it:

---
Lets assume I have a configuration file which sets the argparser argument of "verbose" to True. Further we assume I would have done that by reading a configuration file into an python-dictionary (maybe via ConfigParser or sth. like that) and passing it to the function "set_defaults" of the class "ArgumentParser" from argparse. 

So far so good - now I have my default set. But now my "verbose" switch still is setting the value for "verbose" to True (or False) regardless of the actual state. So, I do not want to set my value for "verbose" to True and neither I want it to set to False (or None) i want it to be set to the opposite value it has till now


{code}

def opposite(bool):
  if bool is True:
    return False
  elif bool is False:
    return True

from argparse import ArgumentParser
parser = ArgumentParser()
parser.set_defaults(**config_defaults)
parser.add_argument('-V', '--verbose', dest='vrb', action='store_const', const=opposite(config_defaults['verbose']), help='switch on/off verbosity')

{/code}


In that case I would be glad to just set my action to 'store_opposite' (or sth. like that).

I am sorry if I have missed something here or was misguiding somehow.

Best regards,
d0n
msg220976 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-19 13:58
I don't understand your use case.  As a user I would expect a switch to either set the value to true or to false, not to toggle it based on some default that might be changed in a configuration file.

But, your method of accomplishing your goal looks fine to me, except that it is unnecessarily verbose.  You can just write:
  
   const=not config_defaults['verbose']

You might also want to make the help text conditional, so that the user knows whether the switch is going to turn verbosity on or off.

You *could* write your own store_opposite action routine, but that seems like overkill, especially if you decide to also make the help text conditional.

Given how simple this is to do, I'm rejecting the feature request.
msg221067 - (view) Author: d0n (d0n) Date: 2014-06-20 05:30
Thanks for your reply. It seems you quite good understood my use case =) and I get your point. Also, I just did not mention your (far more easier method) of accomplishing my goal. 
Indeed, where I use that kind of switching I conditionally change the help text as well. So the actual pice of code I use to satisfy my use case now, taking your advice into practice, looks like the following:

parser.add_argument('--verbose', action='store_const', const=not defaults['verbose'], help='switch on/off verbosity (is '+str(defaults['verbose'])+')')

I quite often use that "config - argparse switch" combination and till now I was doing it far more complicated I do by now :D
Thank you very much for your fast assistance and considering how easy it really is to do what I had in mind I agree with you decision to decline this feature request.

kind regards
msg221101 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2014-06-20 16:59
Another approach would be make the 'argparse' argument a 'switch' command, and act on it after parsing.  Roughly what I have in mind is:

    parser.add_argument('--verbosity', dest='switch_verbosity, action='store_true', help='....'%config_defaults['verbose'])
    ....
    args = parser.parse_args()

    verbosity = config_defaults['verbose']
    if args.switch_verbosity:
       verbosity = not verbosity

In other words, you don't need to do all of the manipulation of values in 'argparse' itself.  Its primary purpose is to decipher what the user wants, and secondarily to guide him (with help, error messages, etc).
History
Date User Action Args
2022-04-11 14:58:05adminsetgithub: 66004
2014-06-20 16:59:27paul.j3setnosy: + paul.j3
messages: + msg221101
2014-06-20 05:30:13d0nsetmessages: + msg221067
2014-06-19 13:58:34r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg220976

resolution: rejected
stage: resolved
2014-06-19 03:49:54d0ncreate