[issue21805] Argparse Revert config_file defaults

2014-06-18 Thread d0n

New submission from d0n:

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

--
components: Extension Modules
messages: 220960
nosy: bethard, d0n
priority: normal
severity: normal
status: open
title: Argparse Revert config_file defaults
type: enhancement
versions: Python 3.5

___
Python tracker 
<http://bugs.python.org/issue21805>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21805] Argparse Revert config_file defaults

2014-06-19 Thread d0n

d0n added the comment:

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

--

___
Python tracker 
<http://bugs.python.org/issue21805>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com