Re: Some questions/suggestions

[email protected] Wed, 04 Dec 2002 02:42:01 -0700
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
Greg Ward writes: 

> However, you don't need to grub about in __dict__ to get odd-named
> attributes -- you can use getattr() and setattr(). 
> 
> Also, a truly strict check would not just ban "--foo!bar", it would also
> ban "--3-times", because Python identifiers cannot start with a digit.
> Combine that with the fact hat 
> 
>   getattr(options, '3_times') 
> 
> isn't *that* ugly, and you can see my point.

Conclusion: trust the programmer? I think a note in the documentation would 
be helpful though. 

> Yes, that's exactly what I was curious about.  It's like you said you
> had a proof for this theorem but didn't have room to write it in the
> margin.  ;-) 
> 
>         Greg

There was some miscommunication here, I meant that merging OptionContainer 
and OptionGroup would meet some problems with the current implementation, 
not that there are problems with the design now. 

Merging OptionContainer with OptionGroup just seems more natural to me. I 
usually use the same guideline David Goodger uses: external interface maps 
to internal structures. I don't think an abstract base class is needed here. 

And now for the tests (attached): 

These tests (all added by me) fail with revision 1.1 of optparse in
python CVS: 

test_opt_string_empty
test_opt_string_too_short
test_opt_string_long_invalid
test_opt_string_short_invalid
test_help_long_opts_first 

Changes the patch makes to make the tests work: 

* format_option_strings_short_first and
 format_option_strings_long_first have been merged into one function,
 format_options, to eliminate the almost complete duplication. To make
 this possible, short_first is now an attribute, which conveniently
 also eases changing short_first after instantiation. 

* _short_opts and _long_opts are set in the Option constructor,
 instead of in _check_option_strings, to prevent an AttributeError
 which would occur when no option strings were passed, making the "at
 least one option string must be supplied" OptionError useless. 

* Removed the check that would raise a RuntimeError in Option.__str__
 when no option strings existed in _short_opts or _long_opts. A
 RuntimeError would be raised when an OptionError was raised in
 _set_opt_strings, because, quite logically, no option strings were set
 at that point. 

 I'm not sure why the check was there, because _short_opts and
 _long_opts are only empty when instantation fails, or when somebody
 set those *internal* attributes to false. And the moment you start
 mucking with internal attributes, you're on your own. :) 

Johannes 

P.S.: By accident, I sent another mail to you personally that should have 
gone to the list: could you respond on the list?
test_optparse.py (text/plain, 45.5 KB)
#!/usr/bin/env python2.3

import sys
import os
import copy
import unittest
import traceback

from cStringIO import StringIO
from pprint import pprint
from test import test_support

from optparse import OptionParser, Option, OptionValueError, OptionGroup, \
     OptionError, OptionConflictError, BadOptionError, SUPPRESS_HELP, \
     SUPPRESS_USAGE, _match_abbrev, make_option, IndentedHelpFormatter, \
     TitledHelpFormatter, OptionContainer

class OptparseBase(unittest.TestCase):    
    def assert_opts(self, args, expected_opts, expected_positional_args):
        (options, positional_args) = self.parser.parse_args(args)

        self.assertEqual(vars(options), expected_opts,
                         "\nOptions are %s. \nShould be %s. \nArgs were %s."
                         % (vars(options), expected_opts, args))

        self.assertEqual(positional_args, expected_positional_args,
                         "\nPositional arguments are %s. \nShould be %s. \n"
                         "Args were %s."
                         % (positional_args, expected_positional_args, args))
        # For further assertions.
        return (options, positional_args)

    def assert_exc(self, func, expected_exc, expected_output, get_output=None,
                   check_output=None, funcargs=[], funckwargs={}):
        if get_output is None:
            get_output = self.exc
        if check_output is None:
            check_output = self.equals
            
        try:
            out = func(*funcargs, **funckwargs)
        except expected_exc, err:
            self.last_error = err
            output_ok, message = check_output(get_output(), expected_output)
            
            message += "\nFunction called: %s\n" \
                       "\nWith args/kwargs: %s/%s" \
                       % (func, funcargs, funckwargs)
            self.assert_(output_ok, message)
            # If we get here, return the error for further testing.
            return err
        else:
            self.fail("No %s raised. \nFunction called: %s\n" \
                      "\nWith args/kwargs: %s/%s"
                      % (expected_exc, func, funcargs, funckwargs))
        

    # -- Functions to be used as check_output --------------------------
    
    def find(self, output, expected_output):
        return (output.find(expected_output) != -1, 
                "\nMessage was: \n%s \nShould contain: \n%s"
                % (output, expected_output))

    def equals(self, output, expected_output):
        return (output == expected_output,
                "\nMessage was: \n%s \nShould be: \n%s"
                % (output, expected_output))

    def endswith(self, output, expected_output):
        return (output.endswith(expected_output),
                "\nMessage was: \n%s \nShould end with: \n%s"
                % (output, expected_output))

    # -- Functions to be used as get_output ----------------------------

    def exc(self):
        return str(self.last_error)

    def redirected_stdout(self):
        return sys.stdout.getvalue()

    # -- Convenience functions used in more than one class -------------

    def assert_parse_exit(self, cmdline_args, expected_output):
        self.assert_exc(self.parser.parse_args, SystemExit, expected_output,
                        self.exc, self.find, [cmdline_args])

    def assert_parse_exit_stdout(self, cmdline_args, expected_output):
        sys.stdout = StringIO()
        self.assert_exc(self.parser.parse_args, SystemExit, expected_output,
                        self.redirected_stdout, funcargs=[cmdline_args])
        sys.stdout = sys.__stdout__

    def assert_type_error(self, func, expected_output, args=[], kwargs={}):
        self.assert_exc(func, TypeError, expected_output, funcargs=args,
                        funckwargs=kwargs)

# -- Test make_option() aka Option -------------------------------------

# It's not necessary to test correct options here. All the tests in the
# parser.parse_args() section deal with those, because they're needed
# there. Duplication makes no sense to me.

class TestOptionChecks(OptparseBase):
    def setUp(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE)

    def assert_option_error(self, expected_output, args=[], kwargs={}):
        self.assert_exc(make_option, OptionError, expected_output,
                        self.exc, self.endswith, args, kwargs)

    def test_opt_string_empty(self):
        self.assert_option_error("at least one option string must be supplied")

    def test_opt_string_too_short(self):
        self.assert_option_error("invalid option string 'b': "
                                 "must be at least two characters long",
                                 ["b"])

    def test_opt_string_short_invalid(self):
        self.assert_option_error("invalid short option string '--': must be "
                                 "of the form -x, (x any non-dash char)",
                                 ["--"])

    def test_opt_string_long_invalid(self):
        self.assert_option_error("invalid long option string '---': "
                                 "must start with --, followed by non-dash",
                                 ["---"])

    def test_attr_invalid(self):
        self.assert_option_error("invalid keyword arguments: foo, bar",
                                 ["-b"], {'foo': None, 'bar': None})

    def test_action_invalid(self):
        self.assert_option_error("invalid action: 'foo'",
                                 ["-b"], {'action': 'foo'})

    def test_type_invalid(self):
        self.assert_option_error("invalid option type: 'foo'",
                                 ["-b"], {'type': 'foo'})

    def test_no_type_for_action(self):
        self.assert_option_error("must not supply a type for action 'count'",
                                 ["-b"], {'action': 'count', 'type': 'int'})
        
    def test_no_choices_list(self):
        self.assert_option_error("must supply a list of "
                                 "choices for type 'choice'",
                                 ["-b", "--bad"], {'type': "choice"})

    def test_bad_choices_list(self):
        self.assert_option_error("choices must be a list of "
                                 "strings ('str' supplied)",
                                 ["-b", "--bad"],
                                 {'type': "choice", 'choices':"bad choices"})

    def test_no_choices_for_type(self):
        self.assert_option_error("must not supply choices for type 'int'",
                                 ["-b"], {'type': 'int', 'choices':"bad"})

    def test_no_const_for_action(self):
        self.assert_option_error("'const' must not be supplied for action "
                                 "'store'",
                                 ["-b"], {'action': 'store', 'const': 1})

    def test_no_nargs_for_action(self):
        self.assert_option_error("'nargs' must not be supplied for action "
                                 "'count'",
                                 ["-b"], {'action': 'count', 'nargs': 2})

    def test_callback_not_callable(self):
        self.assert_option_error("callback not callable: 'foo'",
                                 ["-b"], {'action': 'callback',
                                          'callback': 'foo'})

    def dummy(self):
        pass

    def test_callback_args_no_tuple(self):
        self.assert_option_error("callback_args, if supplied, must be a tuple: "
                                 "not 'foo'",
                                 ["-b"], {'action': 'callback',
                                          'callback': self.dummy,
                                          'callback_args': 'foo'})

    def test_callback_kwargs_no_dict(self):
        self.assert_option_error("callback_kwargs, if supplied, must be a dict: "
                                 "not 'foo'",
                                 ["-b"], {'action': 'callback',
                                          'callback': self.dummy,
                                          'callback_kwargs': 'foo'})

    def test_no_callback_for_action(self):
        self.assert_option_error("callback supplied ('foo') for "
                                 "non-callback option",
                                 ["-b"], {'action': 'store',
                                          'callback': 'foo'})

    def test_no_callback_args_for_action(self):
        self.assert_option_error("callback_args supplied for non-callback "
                                 "option",
                                 ["-b"], {'action': 'store',
                                          'callback_args': 'foo'})
                                 
    def test_no_callback_kwargs_for_action(self):
        self.assert_option_error("callback_kwargs supplied for non-callback "
                                 "option",
                                 ["-b"], {'action': 'store',
                                          'callback_kwargs': 'foo'})

class TestOptionParser(OptparseBase):            
    def setUp(self):
        self.parser = OptionParser()
        self.parser.add_option("-v", "--verbose", "-n", "--noisy",
                          action="store_true", dest="verbose")
        self.parser.add_option("-q", "--quiet", "--silent",
                          action="store_false", dest="verbose")

    def test_add_option_no_Option(self):
        self.assert_type_error(self.parser.add_option,
                               "not an Option instance: None", [None])

    def test_add_option_invalid_arguments(self):
        self.assert_type_error(self.parser.add_option,
                               "invalid arguments", [None, None])

    def test_get_option(self):
        opt1 = self.parser.get_option("-v")
        self.assert_(isinstance(opt1, Option))
        self.assertEqual(opt1._short_opts, ["-v", "-n"])
        self.assertEqual(opt1._long_opts, ["--verbose", "--noisy"])
        self.assertEqual(opt1.action, "store_true")
        self.assertEqual(opt1.dest, "verbose")

    def test_get_option_equals(self):
        opt1 = self.parser.get_option("-v")
        opt2 = self.parser.get_option("--verbose")
        opt3 = self.parser.get_option("-n")
        opt4 = self.parser.get_option("--noisy")
        self.assert_(opt1 is opt2 is opt3 is opt4)

    def test_has_option(self):
        self.assert_(self.parser.has_option("-v"))
        self.assert_(self.parser.has_option("--verbose"))

    def assert_removed(self):
        self.assert_(self.parser.get_option("-v") is None)
        self.assert_(self.parser.get_option("--verbose") is None)
        self.assert_(self.parser.get_option("-n") is None)
        self.assert_(self.parser.get_option("--noisy") is None)

        self.failIf(self.parser.has_option("-v"))
        self.failIf(self.parser.has_option("--verbose"))
        self.failIf(self.parser.has_option("-n"))
        self.failIf(self.parser.has_option("--noisy"))

        self.assert_(self.parser.has_option("-q"))
        self.assert_(self.parser.has_option("--silent"))

    def test_remove_short_opt(self):
        self.parser.remove_option("-n")
        self.assert_removed()

    def test_remove_long_opt(self):
        self.parser.remove_option("--verbose")
        self.assert_removed()

    def test_remove_nonexistent(self):
        self.assert_exc(self.parser.remove_option, ValueError,
                        "no such option 'foo'", funcargs=['foo'])

# -- Test parser.parse_args() ------------------------------------------

class TestStandard(OptparseBase):
    def setUp(self):
        options = [make_option("-a", type="string"),
                   make_option("-b", "--boo", type="int", dest='boo'),
                   make_option("--foo", action="append")]
        
        self.parser = OptionParser(usage=SUPPRESS_USAGE, option_list=options)
        
    def test_required_value(self):
        self.assert_parse_exit(["-a"], "-a option requires a value")

    def test_invalid_integer(self):
        self.assert_parse_exit(["-b", "5x"],
                         "option -b: invalid integer value: '5x'")

    def test_no_such_option(self):
        self.assert_parse_exit(["--boo13"], "no such option: --boo13")

    def test_long_invalid_integer(self):
        self.assert_parse_exit(["--boo=x5"],
                         "option --boo: invalid integer value: 'x5'")

    def test_empty(self):
        self.assert_opts([], {'a': None, 'boo': None, 'foo': None}, [])

    def test_shortopt_empty_longopt_append(self):
        self.assert_opts(["-a", "", "--foo=blah", "--foo="],
                         {'a': "", 'boo': None, 'foo': ["blah", ""]},
                         [])

    def test_long_option_append(self):
        self.assert_opts(["--foo", "bar", "--foo", "", "--foo=x"],
                         {'a': None, 'boo': None, 'foo': ["bar", "", "x"]},
                         [])

    def test_option_argument_joined(self):
        self.assert_opts(["-abc"],
                         {'a': "bc", 'boo': None, 'foo': None},
                         [])

    def test_option_argument_split(self):
        self.assert_opts(["-a", "34"],
                         {'a': "34", 'boo': None, 'foo': None},
                         [])

    def test_option_argument_joined_integer(self):
        self.assert_opts(["-b34"],
                         {'a': None, 'boo': 34, 'foo': None},
                         [])

    def test_option_argument_split_negative_integer(self):
        self.assert_opts(["-b", "-5"],
                         {'a': None, 'boo': -5, 'foo': None},
                         [])

    def test_long_option_argument_joined(self):
        self.assert_opts(["--boo=13"],
                         {'a': None, 'boo': 13, 'foo': None},
                         [])

    def test_long_option_argument_split(self):
        self.assert_opts(["--boo", "111"],
                         {'a': None, 'boo': 111, 'foo': None},
                         [])

    def test_long_option_short_option(self):
        self.assert_opts(["--foo=bar", "-axyz"],
                         {'a': 'xyz', 'boo': None, 'foo': ["bar"]},
                         [])

    def test_abbrev_long_option(self):
        self.assert_opts(["--f=bar", "-axyz"],
                         {'a': 'xyz', 'boo': None, 'foo': ["bar"]},
                         [])
    
    def test_defaults(self):
        (options, args) = self.parser.parse_args([])
        defaults = self.parser.get_default_values()
        self.assertEqual(vars(defaults), vars(options))
    
    def test_ambiguous_option(self):
        self.parser.add_option("--foz", action="store",
                               type="string", dest="foo")
        self.assert_parse_exit(["--f=bar"], "ambiguous option: --f (")

    
    def test_short_and_long_option_split(self):
        self.assert_opts(["-a", "xyz", "--foo", "bar"],
                         {'a': 'xyz', 'boo': None, 'foo': ["bar"]},
                         []),

    def test_short_option_split_long_option_append(self):
        self.assert_opts(["--foo=bar", "-b", "123", "--foo", "baz"],
                         {'a': None, 'boo': 123, 'foo': ["bar", "baz"]},
                         [])

    def test_short_option_split_one_positional_arg(self):
        self.assert_opts(["-a", "foo", "bar"],
                         {'a': "foo", 'boo': None, 'foo': None},
                         ["bar"]),

    def test_short_option_consumes_separator(self):
        self.assert_opts(["-a", "--", "foo", "bar"],
                         {'a': "--", 'boo': None, 'foo': None},
                         ["foo", "bar"]),

    def test_short_option_joined_and_separator(self):
        self.assert_opts(["-ab", "--", "--foo", "bar"],
                         {'a': "b", 'boo': None, 'foo': None},
                         ["--foo", "bar"]),

    def test_invalid_option_becomes_positional_arg(self):
        self.assert_opts(["-ab", "-", "--foo", "bar"],
                         {'a': "b", 'boo': None, 'foo': ["bar"]},
                         ["-"])

    def test_no_append_versus_append(self):
        self.assert_opts(["-b3", "-b", "5", "--foo=bar", "--foo", "baz"],
                         {'a': None, 'boo': 5, 'foo': ["bar", "baz"]},
                         [])

    def test_option_consumes_optionlike_string(self):
        self.assert_opts(["-a", "-b3"],
                         {'a': "-b3", 'boo': None, 'foo': None},
                         [])           
    
class TestBool(OptparseBase):
    def setUp(self):
        options = [make_option("-v",
                               "--verbose",
                               action="store_true",
                               dest="verbose",
                               default=''),
                   make_option("-q",
                               "--quiet",
                               action="store_false",
                               dest="verbose")]
        self.parser = OptionParser(option_list = options)

    def test_bool_default(self):
        self.assert_opts([],
                         {'verbose': ''},
                         [])
        
    def test_bool_false(self):
        self.assert_opts(["-q"],
                         {'verbose': 0},
                         [])

    def test_bool_true(self):
        self.assert_opts(["-v"],
                         {'verbose': 1},
                         [])

    def test_bool_flicker_on_and_off(self):
        self.assert_opts(["-qvq", "-q", "-v"],
                         {'verbose': 1},
                         [])

class TestChoice(OptparseBase):
    def setUp(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE)
        self.parser.add_option("-c", action="store", type="choice",
                               dest="choice", choices=["one", "two", "three"])

    def test_valid_choice(self):
        self.assert_opts(["-c", "one", "xyz"],
                         {'choice': 'one'},
                         ["xyz"])

    def test_invalid_choice(self):
        self.assert_parse_exit(["-c", "four", "abc"],
                         "option -c: invalid choice: 'four' "
                         "(choose from 'one', 'two', 'three')")
        
    def test_add_choice_option(self):
        self.parser.add_option("-d", "--default",
                               choices=["four", "five", "six"])
        opt = self.parser.get_option("-d")
        self.assertEqual(opt.type, "choice")
        self.assertEqual(opt.action, "store")

class TestCount(OptparseBase):
    def setUp(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE)
        self.v_opt = make_option("-v", action="count", dest="verbose")
        self.parser.add_option(self.v_opt)
        self.parser.add_option("--verbose", type="int", dest="verbose")
        self.parser.add_option("-q", "--quiet",
                               action="store_const", dest="verbose", const=0)

    def test_empty(self):
        self.assert_opts([], {'verbose': None}, [])

    def test_count_one(self):
        self.assert_opts(["-v"], {'verbose': 1}, [])

    def test_count_three(self):
        self.assert_opts(["-vvv"], {'verbose': 3}, [])

    def test_count_three_apart(self):
        self.assert_opts(["-v", "-v", "-v"], {'verbose': 3}, [])

    def test_count_override_amount(self):
        self.assert_opts(["-vvv", "--verbose=2"], {'verbose': 2}, [])

    def test_count_override_quiet(self):
        self.assert_opts(["-vvv", "--verbose=2", "-q"], {'verbose': 0}, [])

    def test_count_overriding(self):
        self.assert_opts(["-vvv", "--verbose=2", "-q", "-v"],
                         {'verbose': 1}, [])

    def test_count_interspersed_args(self):
        self.assert_opts(["--quiet", "3", "-v"],
                         {'verbose': 1},
                         ["3"])

    def test_count_no_interspersed_args(self):
        self.parser.disable_interspersed_args()
        self.assert_opts(["--quiet", "3", "-v"],
                         {'verbose': 0},
                         ["3", "-v"])

    def test_count_no_such_option(self):
        self.assert_parse_exit(["-q3", "-v"], "no such option: -3")

    def test_count_option_no_value(self):
        self.assert_parse_exit(["--quiet=3", "-v"],
                               "--quiet option does not take a value")

    def test_count_with_default(self):
        self.parser.set_default('verbose', 0)
        self.assert_opts([], {'verbose':0}, [])

    def test_count_overriding_default(self):
        self.parser.set_default('verbose', 0)
        self.assert_opts(["-vvv", "--verbose=2", "-q", "-v"],
                         {'verbose': 1}, [])

class TestNArgs(OptparseBase):
    def setUp(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE)
        self.parser.add_option("-p", "--point",
                          action="store", nargs=3, type="float", dest="point")

    def test_nargs_with_positional_args(self):
        self.assert_opts(["foo", "-p", "1", "2.5", "-4.3", "xyz"],
                         {'point': (1.0, 2.5, -4.3)},
                         ["foo", "xyz"])

    def test_nargs_long_opt(self):
        self.assert_opts(["--point", "-1", "2.5", "-0", "xyz"],
                         {'point': (-1.0, 2.5, -0.0)},
                         ["xyz"])

    def test_nargs_invalid_float_value(self):
        self.assert_parse_exit(["-p", "1.0", "2x", "3.5"],
                               "option -p: "
                               "invalid floating-point value: '2x'")
           
    def test_nargs_required_values(self):
        self.assert_parse_exit(["--point", "1.0", "3.5"],
                               "--point option requires 3 values")

class TestNArgsAppend(OptparseBase):
    def setUp(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE)
        self.parser.add_option("-p", "--point",
                          action="store", nargs=3, type="float", dest="point")
        self.parser.add_option("-f", "--foo",
                          action="append", nargs=2, type="int", dest="foo")

    def test_nargs_append(self):
        self.assert_opts(["-f", "4", "-3", "blah", "--foo", "1", "666"],
                         {'point': None, 'foo': [(4, -3), (1, 666)]},
                         ["blah"])

    def test_nargs_append_required_values(self):
        self.assert_parse_exit(["-f4,3"],
                               "-f option requires 2 values")

    def test_nargs_append_simple(self):
        self.assert_opts(["--foo=3", "4"],
                               {'point': None, 'foo':[(3, 4)]},
                               [])

class TestVersion(OptparseBase):
    def test_version(self):
        oldargv = sys.argv[0]
        sys.argv[0] = "./foo/bar"
        self.parser = OptionParser(usage=SUPPRESS_USAGE, version="%prog 0.1")
        self.assert_parse_exit_stdout(["--version"],
                                      "bar 0.1\n")
        sys.argv[0] = oldargv

    def test_version_no_such_option(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE)
        self.assert_parse_exit(["--version"],
                               "no such option: --version")

# -- Test conflicting default values and parser.parse_args() -----------

class TestConflictDefaultValues(OptparseBase):
    """Conflicting default values: the last one should win."""
    def setUp(self):
        self.parser = OptionParser(option_list=[
            make_option("-v", action="store_true", dest="verbose", default=1),
            make_option("-q", action="store_false", dest="verbose", default=0)
            ])

    def test_conflict_default(self):
        self.assert_opts([],
                         {'verbose': 0},
                         [])


class TestConflictDefaultValuesNone(OptparseBase):
    def setUp(self):
        self.parser = OptionParser(option_list=[
            make_option("-v", action="store_true", dest="verbose", default=1),
            make_option("-q", action="store_false", dest="verbose",
                        default=None),])

    def test_conflict_default_none(self):
        self.assert_opts([],
                         {'verbose': None},
                         [])

class TestOptionGroup(OptparseBase):
    def setUp(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE)

    def test_option_group_create_instance(self):
        group = OptionGroup(self.parser, "Spam")
        self.parser.add_option_group(group)
        group.add_option("--spam", action="store_true",
                         help="spam spam spam spam")
        self.assert_opts(["--spam"], {'spam': 1}, [])

    def test_add_group_no_group(self):
        self.assert_type_error(self.parser.add_option_group,
                               "not an OptionGroup instance: None", [None])

    def test_add_group_invalid_arguments(self):
        self.assert_type_error(self.parser.add_option_group,
                               "invalid arguments", [None, None])

    def test_add_group_wrong_parser(self):
        group = OptionGroup(self.parser, "Spam")
        group.parser = OptionParser()
        self.assert_exc(self.parser.add_option_group, ValueError,
                        "invalid OptionGroup (wrong parser)", funcargs=[group])

    def test_group_manipulate(self):
        group = self.parser.add_option_group("Group 2",
                                             description="Some more options")
        group.set_title("Bacon")
        group.add_option("--bacon", type="int")
        self.assert_(self.parser.get_option_group("--bacon"), group)

# -- Test extending and parser.parse_args() ----------------------------

class TestExtendAddTypes(OptparseBase):
    def setUp(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE,
                                   option_class=self.MyOption)
        self.parser.add_option("-a", None, type="string", dest="a")
        self.parser.add_option("-f", "--file", type="file", dest="file")

    class MyOption (Option):
        def check_file (option, opt, value):
            if not os.path.exists(value):
                raise OptionValueError("%s: file does not exist" % value)
            elif not os.path.isfile(value):
                raise OptionValueError("%s: not a regular file" % value)
            return value
    
        TYPES = Option.TYPES + ("file",)
        TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER)
        TYPE_CHECKER["file"] = check_file

    def test_extend_file(self):
        open(test_support.TESTFN, "w").close()
        self.assert_opts(["--file", test_support.TESTFN, "-afoo"],
                         {'file': test_support.TESTFN, 'a': 'foo'},
                         [])

        os.unlink(test_support.TESTFN)

    def test_extend_file_nonexistent(self):
        self.assert_parse_exit(["--file", test_support.TESTFN, "-afoo"],
                               "%s: file does not exist" % test_support.TESTFN)

    def test_file_irregular(self):
        os.mkdir(test_support.TESTFN)
        self.assert_parse_exit(["--file", test_support.TESTFN, "-afoo"],
                               "%s: not a regular file" % test_support.TESTFN)
        os.rmdir(test_support.TESTFN)

class TestExtendAddActions(OptparseBase):
    def setUp(self):
        options = [self.MyOption("-a", "--apple", action="extend",
                                 type="string", dest="apple")]
        self.parser = OptionParser(option_list=options)

    class MyOption (Option):
        ACTIONS = Option.ACTIONS + ("extend",)
        STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
        TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)

        def take_action (self, action, dest, opt, value, values, parser):
            if action == "extend":
                lvalue = value.split(",")
                values.ensure_value(dest, []).extend(lvalue)
            else:
                Option.take_action(self, action, dest, opt, parser, value,
                                   values)

    def test_extend_add_action(self):
        self.assert_opts(["-afoo,bar", "--apple=blah"],
                         {'apple': ["foo", "bar", "blah"]},
                         [])

    def test_extend_add_action_normal(self):
        self.assert_opts(["-a", "foo", "-abar", "--apple=x,y"],
                         {'apple': ["foo", "bar", "x", "y"]},
                         [])

# -- Test callbacks and parser.parse_args() ----------------------------

class TestCallback(OptparseBase):
    def setUp(self):
        options = [make_option("-x",
                               None,
                               action="callback",
                               callback=self.process_opt),
                   make_option("-f",
                               "--file",
                               action="callback",
                               callback=self.process_opt,
                               type="string",
                               dest="filename")]
        self.parser = OptionParser(option_list=options)
        
    def process_opt(self, option, opt, value, parser_):
        if opt == "-x":
            self.assertEqual(option._short_opts, ["-x"])
            self.assertEqual(option._long_opts, [])
            self.assert_(parser_ is self.parser)
            self.assert_(value is None)
            self.assertEqual(vars(parser_.values), {'filename': None})
                   
            parser_.values.x = 42
        elif opt == "--file":
            self.assertEqual(option._short_opts, ["-f"])
            self.assertEqual(option._long_opts, ["--file"]) 
            self.assert_(parser_ is self.parser)
            self.assertEqual(value, "foo")
            self.assertEqual(vars(parser_.values), {'filename': None, 'x': 42})
            
            setattr(parser_.values, option.dest, value)
        else:
            self.fail("Unknown option %r in process_opt." % opt)

    def test_callback(self):
        self.assert_opts(["-x", "--file=foo"],
                         {'filename': "foo", 'x': 42},
                         [])

class TestCallBackExtraArgs(OptparseBase):
    def setUp(self):
        options = [make_option("-p", "--point", action="callback",
                               callback=self.process_tuple,
                               callback_args=(3, int), type="string",
                               dest="points", default=[])]
        self.parser = OptionParser(option_list=options)

    def process_tuple (self, option, opt, value, parser_, len, type):
        self.assertEqual(len, 3)
        self.assert_(type is int)
        
        if opt == "-p":
            self.assertEqual(value, "1,2,3")
        elif opt == "--point":
            self.assertEqual(value, "4,5,6")
            
        value = tuple(map(type, value.split(",")))
        getattr(parser_.values, option.dest).append(value)

    def test_callback_extra_args(self):
        self.assert_opts(["-p1,2,3", "--point", "4,5,6"],
                         {'points': [(1,2,3), (4,5,6)]},
                         [])

class TestCallBackMeddleArgs(OptparseBase):
    def setUp(self):
        options = [make_option(str(x), action="callback",
                               callback=self.process_n, dest='things')
                   for x in range(-1, -6, -1)]
        self.parser = OptionParser(option_list=options)

    # Callback that meddles in rargs, largs
    def process_n (self, option, opt, value, parser_):
        # option is -3, -5, etc.
        nargs = int(opt[1:])
        rargs = parser_.rargs
        if len(rargs) < nargs:
            self.fail("Expected %d arguments for %s option." % (nargs, opt))
        dest = parser_.values.ensure_value(option.dest, [])
        dest.append(tuple(rargs[0:nargs]))
        parser_.largs.append(nargs)
        del rargs[0:nargs]

    def test_callback_meddle_args(self):
        self.assert_opts(["-1", "foo", "-3", "bar", "baz", "qux"],
                         {'things': [("foo",), ("bar", "baz", "qux")]},
                         [1, 3])

    def test_callback_meddle_args_separator(self):
        self.assert_opts(["-2", "foo", "--"],
                         {'things': [('foo', '--')]},
                         [2])

class TestCallBackManyArgs(OptparseBase):
    def setUp(self):
        options = [make_option("-a", "--apple", action="callback", nargs=2,
                               callback=self.process_many, type="string"),
                   make_option("-b", "--bob", action="callback", nargs=3,
                               callback=self.process_many, type="int")]
        self.parser = OptionParser(option_list=options)

    def process_many (self, option, opt, value, parser_):
        if opt == "-a":
            self.assertEqual(value, ("foo", "bar"))
        elif opt == "--apple":
            self.assertEqual(value, ("ding", "dong"))
        elif opt == "-b":
            self.assertEqual(value, (1, 2, 3))
        elif opt == "--bob":
            self.assertEqual(value, (-666, 42, 0))

    def test_many_args(self):
        self.assert_opts(["-a", "foo", "bar", "--apple", "ding", "dong",
                              "-b", "1", "2", "3", "--bob", "-666", "42", "0"],
                             {},
                             [])

class TestCallBackCheckAbbrev(OptparseBase):
    def setUp(self):
        self.parser = OptionParser()
        self.parser.add_option("--foo-bar", action="callback",
                               callback=self.check_abbrev)

    def check_abbrev (self, option, opt, value, parser):
        self.assertEqual(opt, "--foo-bar")

    def test_abbrev_callback_expansion(self):
        self.assert_opts(["--foo"], {}, [])

class TestCallBackVarArgs(OptparseBase):
    def setUp(self):
        options = [make_option("-a", type="int", nargs=2, dest="a"),
                   make_option("-b", action="store_true", dest="b"),
                   make_option("-c", "--callback", action="callback",
                               callback=self.variable_args, dest="c")]
        self.parser = OptionParser(usage=SUPPRESS_USAGE, option_list=options)

    def variable_args (self, option, opt, value, parser):
        self.assert_(value is None)
        done = 0
        value = []
        rargs = parser.rargs
        while rargs:
            arg = rargs[0]
            if ((arg[:2] == "--" and len(arg) > 2) or
                (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")):
                break
            else:
                value.append(arg)
                del rargs[0]
        setattr(parser.values, option.dest, value)

    def test_variable_args(self):
        self.assert_opts(["-a3", "-5", "--callback", "foo", "bar"],
                         {'a': (3, -5), 'b': None, 'c': ["foo", "bar"]},
                         [])
    
    def test_consume_separator_stop_at_option(self):
        self.assert_opts(["-c", "37", "--", "xxx", "-b", "hello"],
                         {'a': None, 'b': True, 'c': ["37", "--", "xxx"]},
                         ["hello"])

    def test_positional_arg_and_variable_args(self):
        self.assert_opts(["hello", "-c", "foo", "-", "bar"],
                         {'a': None, 'b': None, 'c':["foo", "-", "bar"]},
                         ["hello"])

    def test_stop_at_option(self):
        self.assert_opts(["-c", "foo", "-b"],
                         {'a': None, 'b': True, 'c': ["foo"]},
                         [])

    def test_stop_at_invalid_option(self):
        self.assert_parse_exit(["-c", "3", "-5", "-a"], "no such option: -5")


# -- Test conflict handling and parser.parse_args() --------------------

class ConflictBase(OptparseBase):
    def setUp(self):
        options = [make_option("-v", "--verbose", action="count",
                               dest="verbose", help="increment verbosity")]
        self.parser = OptionParser(usage=SUPPRESS_USAGE, option_list=options)
        
    def show_version (self, option, opt, value, parser):
        parser.values.show_version = 1

class TestConflict(ConflictBase):
    """Use the default conflict resolution for Optik 1.2: error."""    
    def assert_conflict_error(self, func):
        err = self.assert_exc(func, OptionConflictError,
                              "option -v/--version: conflicting option "
                              "string(s): -v",
                              funcargs=["-v", "--version"],
                              funckwargs={'action':"callback",
                                          'callback':self.show_version,
                                          'help':"show version"})
        
        self.assertEqual(err.msg, "conflicting option string(s): -v")
        self.assertEqual(err.option_id, "-v/--version")
    
    def test_conflict_error(self):
        self.assert_conflict_error(self.parser.add_option)

    def test_conflict_error_group(self):
        group = OptionGroup(self.parser, "Group 1")
        self.assert_conflict_error(group.add_option)

    def test_no_such_conflict_handler(self):
        self.assert_exc(self.parser.set_conflict_handler, ValueError,
                        "invalid conflict_resolution value 'foo'",
                        funcargs=['foo'])
        

class TestConflictIgnore(ConflictBase):
    """Test the old (Optik <= 1.1 behaviour) -- arguably broken, but
    still available so should be tested.
    """

    def setUp(self):
        ConflictBase.setUp(self)
        self.parser.set_conflict_handler("ignore")
        self.parser.add_option("-v", "--version", action="callback",
                          callback=self.show_version, help="show version")

    def test_conflict_ignore(self):
        v_opt = self.parser.get_option("-v")
        verbose_opt = self.parser.get_option("--verbose")
        version_opt = self.parser.get_option("--version")

        self.assert_(v_opt is version_opt)
        self.assert_(v_opt is not verbose_opt)
        self.assertEqual(v_opt._long_opts, ["--version"])
        self.assertEqual(version_opt._short_opts, ["-v"])
        self.assertEqual(verbose_opt._short_opts, ["-v"])

    def test_conflict_ignore_help(self):
        self.assert_parse_exit_stdout(["-h"], """\
options:
  -v, --verbose  increment verbosity
  -h, --help     show this help message and exit
  -v, --version  show version
""")

    def test_conflict_ignore_short_opt(self):
        self.assert_opts(["-v"],
                         {'show_version': 1, 'verbose': None},
                         [])

class TestConflictResolve(ConflictBase):
    def setUp(self):
        ConflictBase.setUp(self)
        self.parser.set_conflict_handler("resolve")
        self.parser.add_option("-v", "--version", action="callback",
                               callback=self.show_version, help="show version")
    
    def test_conflict_resolve(self):
        v_opt = self.parser.get_option("-v")
        verbose_opt = self.parser.get_option("--verbose")
        version_opt = self.parser.get_option("--version")

        self.assert_(v_opt is version_opt)
        self.assert_(v_opt is not verbose_opt)
        self.assertEqual(v_opt._long_opts, ["--version"])
        self.assertEqual(version_opt._short_opts, ["-v"])
        self.assertEqual(version_opt._long_opts, ["--version"])
        self.assertEqual(verbose_opt._short_opts, [])
        self.assertEqual(verbose_opt._long_opts, ["--verbose"])

    def test_conflict_resolve_help(self):
        self.assert_parse_exit_stdout(["-h"], """\
options:
  --verbose      increment verbosity
  -h, --help     show this help message and exit
  -v, --version  show version
""")

    def test_conflict_resolve_short_opt(self):
        self.assert_opts(["-v"],
                         {'verbose': None, 'show_version': 1},
                         [])

    def test_conflict_resolve_long_opt(self):
        self.assert_opts(["--verbose"],
                         {'verbose': 1},
                         [])

    def test_conflict_resolve_long_opts(self):
        self.assert_opts(["--verbose", "--version"],
                         {'verbose': 1, 'show_version': 1},
                         [])

class TestConflictOverride(OptparseBase):
    def setUp(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE)
        self.parser.set_conflict_handler("resolve")
        self.parser.add_option("-n", "--dry-run",
                               action="store_true", dest="dry_run",
                               help="don't do anything")
        self.parser.add_option("--dry-run", "-n",
                               action="store_const", const=42, dest="dry_run",
                               help="dry run mode")

    def test_conflict_override_opts(self):
        opt = self.parser.get_option("--dry-run")
        self.assertEqual(opt._short_opts, ["-n"])
        self.assertEqual(opt._long_opts, ["--dry-run"])

    def test_conflict_override_help(self):
        self.assert_parse_exit_stdout(["-h"], """\
options:
  -h, --help     show this help message and exit
  -n, --dry-run  dry run mode
""")

    def test_conflict_override_args(self):
        self.assert_opts(["-n"],
                         {'dry_run': 42},
                         [])

# -- Other testing. ----------------------------------------------------

class TestHelp(OptparseBase):
    def setUp(self):
        options = [
            make_option("-a", type="string", dest='a',
                        metavar="APPLE", help="throw APPLEs at basket"),
            make_option("-b", "--boo", type="int", dest='boo',
                        metavar="NUM",
                        help=
                        "shout \"boo!\" NUM times (in order to frighten away "
                        "all the evil spirits that cause trouble and mayhem)"),
            make_option("--foo", action="append", type="string", dest='foo',
                        help="store FOO in the foo list for later fooing"),
            ]

        usage = "%prog [options]"
        self.parser = OptionParser(usage=usage, option_list=options)

    def assert_help_equal(self, expected_output):
        # XXX: Also use assert_parse_exit_stdout here.
        # This trick is used to make optparse believe bar.py is being executed.
        oldargv = sys.argv[0]
        sys.argv[0] = "./foo/bar.py"

        output = self.parser.format_help()
        
        # Changed the message to ease reading help messages in case of
        # error. Uses str()-style formatting instead of backticks.
        self.assertEqual(output, expected_output,
                         '\n%s != \n%s' % (output, expected_output))
        sys.argv[0] = oldargv
        
    def test_help(self):
        self.assert_help_equal("""\
usage: bar.py [options]

options:
  -aAPPLE           throw APPLEs at basket
  -bNUM, --boo=NUM  shout "boo!" NUM times (in order to frighten away all
                    the evil spirits that cause trouble and mayhem)
  --foo=FOO         store FOO in the foo list for later fooing
  -h, --help        show this help message and exit
""")

    def test_help_old_usage(self):
        self.parser.set_usage("usage: %prog [options]")
        self.assert_help_equal("""\
usage: bar.py [options]

options:
  -aAPPLE           throw APPLEs at basket
  -bNUM, --boo=NUM  shout "boo!" NUM times (in order to frighten away all
                    the evil spirits that cause trouble and mayhem)
  --foo=FOO         store FOO in the foo list for later fooing
  -h, --help        show this help message and exit
""")

    def test_help_long_opts_first(self):
        self.parser.formatter.short_first = 0
        self.assert_help_equal("""\
usage: bar.py [options]

options:
  -aAPPLE           throw APPLEs at basket
  --boo=NUM, -bNUM  shout "boo!" NUM times (in order to frighten away all
                    the evil spirits that cause trouble and mayhem)
  --foo=FOO         store FOO in the foo list for later fooing
  --help, -h        show this help message and exit
""")

    def test_help_title_formatter(self):
        self.parser.formatter = TitledHelpFormatter()
        self.assert_help_equal("""\
Usage
=====
  bar.py [options]

options
=======
-aAPPLE           throw APPLEs at basket
--boo=NUM, -bNUM  shout "boo!" NUM times (in order to frighten away all
                  the evil spirits that cause trouble and mayhem)
--foo=FOO         store FOO in the foo list for later fooing
--help, -h        show this help message and exit
""")

    def test_help_description_groups(self):
        self.parser.set_description(
            "This is the program description.  This program has "
            "an option group as well as single options.")

        group = OptionGroup(
            self.parser, "Dangerous Options",
            "Caution: use of these options is at your own risk.  "
            "It is believed that some of them bite.")
        group.add_option("-g", action="store_true", help="Group option.")
        self.parser.add_option_group(group)

        self.assert_help_equal("""\
usage: bar.py [options]

This is the program description.  This program has an option group as well as
single options.
options:
  -aAPPLE           throw APPLEs at basket
  -bNUM, --boo=NUM  shout "boo!" NUM times (in order to frighten away all
                    the evil spirits that cause trouble and mayhem)
  --foo=FOO         store FOO in the foo list for later fooing
  -h, --help        show this help message and exit

  Dangerous Options:
    Caution: use of these options is at your own risk.  It is believed that
    some of them bite.
    -g              Group option.
""")

class TestMatchAbbrev(OptparseBase):
    def test_match_abbrev(self):
        self.assertEqual(_match_abbrev("--f",
                                       {"--foz": None,
                                        "--foo": None,
                                        "--fie": None,
                                        "--f": None}),
                         "--f")

    def test_match_abbrev_error(self):
        s = "--f"
        wordmap = {"--foz": None, "--foo": None, "--fie": None}
        self.assert_exc(_match_abbrev, BadOptionError,
                        "ambiguous option: --f (",
                        check_output=self.find, funcargs=[s, wordmap])

def _testclasses():
    mod = sys.modules[__name__]
    return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]

def suite():
    suite = unittest.TestSuite()
    for testclass in _testclasses():
        suite.addTest(unittest.makeSuite(testclass))
    return suite

def test_main():
    test_support.run_suite(suite())

if __name__ == '__main__':
    test_main()
optparse.diff (text/plain, 3.9 KB)
Index: optparse.py
===================================================================
RCS file: /var/lib/cvs/python/dist/src/Lib/optparse.py,v
retrieving revision 1.1
diff -u -r1.1 optparse.py
--- optparse.py	24 Nov 2002 09:48:58 -0000	1.1
+++ optparse.py	3 Dec 2002 17:47:38 -0000
@@ -118,10 +118,7 @@
         self.current_indent = 0
         self.level = 0
         self.help_width = width - max_help_position
-        if short_first:
-            self.format_option_strings = self.format_option_strings_short_first
-        else:
-            self.format_option_strings = self.format_option_strings_long_first
+        self.short_first = short_first
 
     def indent (self):
         self.current_indent += self.indent_increment
@@ -198,38 +195,20 @@
 
     def format_option_strings (self, option):
         """Return a comma-separated list of option strings & metavariables."""
-        raise NotImplementedError(
-            "abstract method: use format_option_strings_short_first or "
-            "format_option_strings_long_first instead.")
-
-    def format_option_strings_short_first (self, option):
-        opts = []                       # list of "-a" or "--foo=FILE" strings
-        takes_value = option.takes_value()
-        if takes_value:
+        if option.takes_value():
             metavar = option.metavar or option.dest.upper()
-            for sopt in option._short_opts:
-                opts.append(sopt + metavar)
-            for lopt in option._long_opts:
-                opts.append(lopt + "=" + metavar)
+            short_opts = [sopt + metavar for sopt in option._short_opts]
+            long_opts = [lopt + "=" + metavar for lopt in option._long_opts]
         else:
-            for opt in option._short_opts + option._long_opts:
-                opts.append(opt)
-        return ", ".join(opts)
+            short_opts = option._short_opts
+            long_opts = option._long_opts
 
-    def format_option_strings_long_first (self, option):
-        opts = []                       # list of "-a" or "--foo=FILE" strings
-        takes_value = option.takes_value()
-        if takes_value:
-            metavar = option.metavar or option.dest.upper()
-            for lopt in option._long_opts:
-                opts.append(lopt + "=" + metavar)
-            for sopt in option._short_opts:
-                opts.append(sopt + metavar)
+        if self.short_first:
+            opts = short_opts + long_opts
         else:
-            for opt in option._long_opts + option._short_opts:
-                opts.append(opt)
-        return ", ".join(opts)
+            opts = long_opts + short_opts
 
+        return ", ".join(opts)
 
 class IndentedHelpFormatter (HelpFormatter):
     """Format help with indented section bodies.
@@ -400,7 +379,10 @@
     # -- Constructor/initialization methods ----------------------------
 
     def __init__ (self, *opts, **attrs):
-        # Set _short_opts, _long_opts attrs from 'opts' tuple
+        # Set _short_opts, _long_opts attrs from 'opts' tuple.
+        # Have to be set now, in case no option strings are supplied.
+        self._short_opts = []
+        self._long_opts = []
         opts = self._check_opt_strings(opts)
         self._set_opt_strings(opts)
 
@@ -426,8 +408,6 @@
         return opts
 
     def _set_opt_strings (self, opts):
-        self._short_opts = []
-        self._long_opts = []
         for opt in opts:
             if len(opt) < 2:
                 raise OptionError(
@@ -569,10 +549,7 @@
     # -- Miscellaneous methods -----------------------------------------
 
     def __str__ (self):
-        if self._short_opts or self._long_opts:
-            return "/".join(self._short_opts + self._long_opts)
-        else:
-            raise RuntimeError, "short_opts and long_opts both empty!"
-
+        return "/".join(self._short_opts + self._long_opts)
+    
     def takes_value (self):
         return self.type is not None