from distutils.errors import DistutilsOptionError
from setuptools.command import test

class TestCommand(test.test):
    """A derivative of the setuptools test command that automatically
    scans the main package for tests."""
    
    user_options = [
        ('packages=', 'p', "Run tests in package"),
        ('regex=', 'r', 
            'Search filenames/testnames for a regular expression')
    ]
    
    packages = None
    regex = None
    
    def finalize_options(self):
        import collector
        if self.packages is None:
            self.packages = self.distribution.packages
        elif isinstance(self.packages, str):
            self.packages = [self.packages]
            
        package = self.packages[0]
        mod = __import__(package, {}, {})
        mod.test_suite = collector.Collector(package, self.regex)
        
        self.test_module = package
        
        test.test.finalize_options(self)
    

syntax highlighted by Code2HTML, v. 0.9.1