#15 check for reserved words when auto creating destinations

open
Greg Ward
None
5
2006-06-13
2006-05-25
Brian Egge
No

It can be easy to include a reserved word using the
default destination generated from the long option.
This patch would make such reserved words 'safe'.

Example:

parser.add_option("-F", "--from", help="specifies
the From address")
...
# print options.from <- this is a SyntaxError
print options._from

Here's the patch that I'm proposing:

@@ -69,6 +69,7 @@
import sys, os
import types
import textwrap
+import keyword
from gettext import gettext as _

def _repr(self):
@@ -609,6 +610,10 @@
self.dest =
self._long_opts[0][2:].replace('-', '_')
else:
self.dest = self._short_opts[0][1]
+
+ # prefix any keywords with an _
+ while keyword.iskeyword(self.dest):
+ self.dest = "_" + self.dest

def _check_const(self):
if self.action != "store_const" and self.const
is not None:

Discussion

  • Greg Ward
    Greg Ward
    2006-06-13

    • assigned_to: nobody --> gward
     
  • Greg Ward
    Greg Ward
    2006-06-13

    Logged In: YES
    user_id=14422

    The obvious workaround is to use getattr() on your Values
    object. I assume anyone who's been using Optik with
    keywords-as-long-options is already using this workaround,
    and your fix would break this. So if I accept it, it needs
    to be made optional.

    Can you 1) bring this up on
    optik-users@lists.sourceforge.net to see what other Optik
    users think and 2) add a unit test to your patch (i.e. patch
    test/test_optik.py)? Also, please upload your patch rather
    than trying to paste it into a comment -- the SF tracker
    does horrible things to code pasted into comments. ;-(