[Python-Dev] transitioning from % to {} formatting

Barry Warsaw barry at python.org
Wed Sep 30 17:50:28 CEST 2009


On Sep 30, 2009, at 11:39 AM, Steven Bethard wrote:

> Thanks for the clarification. I generally like this approach, though
> it's not so convenient for argparse which already takes format strings
> like this::
>
>    parser = ArgumentParser(usage='%(prog)s [--foo]')
>    parser.add_argument(
>        '--foo', type=int, default=42,
>        help='A foo of type %(type)s, defaulting to %(42)s)
>
> That is, existing keyword arguments that already have good names (and
> are pretty much always used as keyword arguments) take format strings.
> I'm not sure that changing the name of usage= or help= here is really
> an option.

Ah right.

> I guess in this case I'm stuck with something like Benjamin's
> suggestion of adding an additional flag to control which type of
> formatting, and the corresponding 4 versions of cleanup. Ew.

I missed Benjamin's suggestion, but in this case I would say add a  
flag to ArgumentParser.  I'm either going to want {} formatting all or  
nothing.  E.g.

import argparse
parser = ArgumentParser(usage='{prog} [--foo]', format=argparse.BRACES)
parser.add_argument(
     '--foo', type=int, default=42,
     help='A foo of type {type}, defaulting to {42}')

(although that last looks weird ;).

-Barry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 832 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-dev/attachments/20090930/7caccaf5/attachment.pgp>


More information about the Python-Dev mailing list