diff options
| author | Barry Warsaw | 2009-02-04 23:35:49 -0500 |
|---|---|---|
| committer | Barry Warsaw | 2009-02-04 23:35:49 -0500 |
| commit | a0e24e42813198dab691d2a4d67ee6a6a0f7b084 (patch) | |
| tree | 8fa7a4f54eb10ca497fafec8c2190d55fd41e909 /src/mailman/options.py | |
| parent | d0ee12c62f0a2df437d6cd25ddf50be485a32248 (diff) | |
| download | mailman-a0e24e42813198dab691d2a4d67ee6a6a0f7b084.tar.gz mailman-a0e24e42813198dab691d2a4d67ee6a6a0f7b084.tar.zst mailman-a0e24e42813198dab691d2a4d67ee6a6a0f7b084.zip | |
Diffstat (limited to 'src/mailman/options.py')
| -rw-r--r-- | src/mailman/options.py | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/mailman/options.py b/src/mailman/options.py index 5431b2da8..d18c78cea 100644 --- a/src/mailman/options.py +++ b/src/mailman/options.py @@ -65,6 +65,29 @@ class MailmanOption(Option): TYPE_CHECKER['yesno'] = check_yesno +class SafeOptionParser(OptionParser): + """A unicode-compatible `OptionParser`. + + Python's standard option parser does not accept unicode options. Rather + than try to fix that, this class wraps the add_option() method and saves + having to wrap the options in str() calls. + """ + def add_option(self, *args, **kwargs): + # Check to see if the first or first two options are unicodes and turn + # them into 8-bit strings before calling the superclass's method. + if len(args) == 0: + return OptionParser.add_option(self, *args, **kwargs) + old_args = list(args) + new_args = [] + arg0 = old_args.pop(0) + new_args.append(str(arg0)) + if len(old_args) > 0: + arg1 = old_args.pop(0) + new_args.append(str(arg1)) + new_args.extend(old_args) + return OptionParser.add_option(self, *new_args, **kwargs) + + class Options: """Common argument parser.""" @@ -73,9 +96,10 @@ class Options: usage = None def __init__(self): - self.parser = OptionParser(version=MAILMAN_VERSION, - option_class=MailmanOption, - usage=self.usage) + self.parser = SafeOptionParser( + version=MAILMAN_VERSION, + option_class=MailmanOption, + usage=self.usage) self.add_common_options() self.add_options() options, arguments = self.parser.parse_args() @@ -97,7 +121,7 @@ class Options: """Add options common to all scripts.""" # Python requires str types here. self.parser.add_option( - str('-C'), str('--config'), + '-C', '--config', help=_('Alternative configuration file to use')) def initialize(self, propagate_logs=None): |
