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: optparse special usage tokens conflict with formatting characters
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: QuantumTim, andybuckley, draghuram, rhettinger
Priority: normal Keywords: patch

Created on 2009-02-26 14:08 by andybuckley, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
optparse-prog-escape.patch andybuckley, 2009-03-17 23:45 Doc patch for Doc/library/optparse.rst
Messages (9)
msg82755 - (view) Author: Andy Buckley (andybuckley) Date: 2009-02-26 14:08
optparse's OptionParser takes a usage string as a constructor argument,
in which a "%prog" token is replaced with the executable name. Nice
idea, but the choice of a percent sign for token delimiter is
troublesome since it means that attempting to substitute any variables
into the usage string, e.g.

    usage = """
    %prog [options] foo bar ...
    
    Some computed info: %s""" % myinfo

gives a ValueError:

    ValueError: unsupported format character 'p' (0x70) at index 2

Maybe this ceases to be a problem with what I've heard about the
formatting re-design in Python 3.x, but it will probably continue to be
awkward for the 2.x series where the "%" operator still applies. Maybe
optparse could also use a safer token ("@prog", say), so that those of
use who would like to substitute variables into usage strings don't have
to artificially break our strings apart every time we use the program name?
msg82757 - (view) Author: Tim Gordon (QuantumTim) Date: 2009-02-26 14:14
Try escaping the '%prog' in your usage string (i.e. use '%%prog' 
instead) so you don't get the error when you substitute values in.
msg82759 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2009-02-26 14:27
Please use the fix suggested by Tim Gordon.
msg82760 - (view) Author: Andy Buckley (andybuckley) Date: 2009-02-26 14:32
Dang, why didn't I think of that? Cheers. Might be worth mentioning that
in the documentation, in case there are other people with my particular
brand of tunnel vision ;)
msg82761 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2009-02-26 14:35
On Thu, Feb 26, 2009 at 9:32 AM, Andy Buckley <report@bugs.python.org> wrote:

> Dang, why didn't I think of that? Cheers. Might be worth mentioning that
> in the documentation, in case there are other people with my particular
> brand of tunnel vision ;)

I am not entirely sure if it is worth changing the documentation but
it wouldn't hurt, I suppose. It will be helpful if you can submit a
simple doc patch for that purpose.
msg83716 - (view) Author: Andy Buckley (andybuckley) Date: 2009-03-17 23:42
Doc patch for Doc/library/optparse.rst attached.
msg83717 - (view) Author: Andy Buckley (andybuckley) Date: 2009-03-17 23:45
Sorry, browser error last time. Should work now (fingers crossed).
msg83727 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-03-18 01:36
I don't think this patch should be applied for two reasons.  First, this
is just a general comment on % formatting that could apply everywhere %
formatting is used (the correct place for it is in docs for %
formatting).  Second, it has become a non-issue with the PEP 3101 style
of formatting that is the new preferred way to do it.
msg83741 - (view) Author: Andy Buckley (andybuckley) Date: 2009-03-18 10:49
I'm easy either way --- I appreciate that it is a non-issue with new
formatting, but until developers can rely on the presence of Py >= 2.6,
%-formatting wil continue to be widely used.

Since optparse's special use of %-delimited tokens clashes with the
established formatting in a way that could arguably do with
clarification (I was blind enough to the solution to start this ticket),
I think it could be useful to document it close to the description of
%prog usage. Of course, since I now know the answer, *I* don't need it
to be in the documentation, but it might be helpful to others ;)
History
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49624
2009-03-18 10:49:50andybuckleysetmessages: + msg83741
2009-03-18 01:36:55rhettingersetnosy: + rhettinger
messages: + msg83727
2009-03-17 23:45:56andybuckleysetfiles: + optparse-prog-escape.patch
keywords: + patch
messages: + msg83717
2009-03-17 23:42:34andybuckleysetmessages: + msg83716
2009-02-26 14:35:29draghuramsetmessages: + msg82761
2009-02-26 14:32:29andybuckleysetmessages: + msg82760
2009-02-26 14:27:04draghuramsetstatus: open -> closed
resolution: works for me
messages: + msg82759
nosy: + draghuram
2009-02-26 14:14:59QuantumTimsetnosy: + QuantumTim
messages: + msg82757
2009-02-26 14:08:19andybuckleycreate