Exemple #1
0
def getopts(spec):
    """Parse UNIX-style command line options.  Parsed options are returned as
    an object.  A value for option X is accessible trhough the object
    attribute X."""
    # initialize all options with None
    opt = _Options()
    for name in spec:
        if name != ':':
            opt.set(name, None)

    # call getopt in the standard library
    try:
        opts, args = getopt.getopt(sys.argv[1:], spec)
    except getopt.GetoptError as e:
        print(e)
        return None

    # save parsed options as object attributes
    for key, val in opts:
        name = key[1:]
        if getopt.short_has_arg(name, spec):
            opt.set(name, val)
        else:
            opt.set(name, True)

    # discard already parsed arguments
    sys.argv[1:] = args
    return opt
Exemple #2
0
 def test_short_has_arg(self):
     self.failUnless(getopt.short_has_arg('a', 'a:'))
     self.failIf(getopt.short_has_arg('a', 'a'))
     self.assertError(getopt.short_has_arg, 'a', 'b')
from test.test_support import verify, verbose, run_doctest
import os

def expectException(teststr, expected, failure=AssertionError):
    """Executes a statement passed in teststr, and raises an exception
       (failure) if the expected exception is *not* raised."""
    try:
        exec teststr
    except expected:
        pass
    else:
        raise failure

if verbose:
    print 'Running tests on getopt.short_has_arg'
verify(getopt.short_has_arg('a', 'a:'))
verify(not getopt.short_has_arg('a', 'a'))
expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)

if verbose:
    print 'Running tests on getopt.long_has_args'
has_arg, option = getopt.long_has_args('abc', ['abc='])
verify(has_arg)
verify(option == 'abc')
has_arg, option = getopt.long_has_args('abc', ['abc'])
verify(not has_arg)
verify(option == 'abc')
has_arg, option = getopt.long_has_args('abc', ['abcd'])
verify(not has_arg)
verify(option == 'abcd')
Exemple #4
0
 def test_short_has_arg(self):
     self.assertTrue(getopt.short_has_arg('a', 'a:'))
     self.assertFalse(getopt.short_has_arg('a', 'a'))
     self.assertError(getopt.short_has_arg, 'a', 'b')
Exemple #5
0
 def test_short_has_arg(self):
     self.failUnless(getopt.short_has_arg("a", "a:"))
     self.failIf(getopt.short_has_arg("a", "a"))
     self.assertError(getopt.short_has_arg, "a", "b")
Exemple #6
0
# test_getopt.py
# David Goodger <*****@*****.**> 2000-08-19
import getopt
from getopt import GetoptError
from test_support import verify, verbose
def expectException(teststr, expected, failure=AssertionError):
    """Executes a statement passed in teststr, and raises an exception
       (failure) if the expected exception is *not* raised."""
    try:
        exec teststr
    except expected:
        pass
    else:
        raise failure
if verbose:
    print 'Running tests on getopt.short_has_arg'
verify(getopt.short_has_arg('a', 'a:'))
verify(not getopt.short_has_arg('a', 'a'))
expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)
if verbose:
    print 'Running tests on getopt.long_has_args'
has_arg, option = getopt.long_has_args('abc', ['abc='])
verify(has_arg)
verify(option == 'abc')
has_arg, option = getopt.long_has_args('abc', ['abc'])
verify(not has_arg)
verify(option == 'abc')
has_arg, option = getopt.long_has_args('abc', ['abcd'])
verify(not has_arg)
Exemple #7
0
 def update_event(self, inp=-1):
     self.set_output_val(0,
                         getopt.short_has_arg(self.input(0), self.input(1)))
Exemple #8
0
       (failure) if the expected exception is *not* raised."""
    try:
        exec teststr
    except expected:
        pass
    else:
        raise failure


old_posixly_correct = os.environ.get("POSIXLY_CORRECT")
if old_posixly_correct is not None:
    del os.environ["POSIXLY_CORRECT"]

if verbose:
    print 'Running tests on getopt.short_has_arg'
verify(getopt.short_has_arg('a', 'a:'))
verify(not getopt.short_has_arg('a', 'a'))
expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)

if verbose:
    print 'Running tests on getopt.long_has_args'
has_arg, option = getopt.long_has_args('abc', ['abc='])
verify(has_arg)
verify(option == 'abc')
has_arg, option = getopt.long_has_args('abc', ['abc'])
verify(not has_arg)
verify(option == 'abc')
has_arg, option = getopt.long_has_args('abc', ['abcd'])
verify(not has_arg)
verify(option == 'abcd')
Exemple #9
0
from getopt import GetoptError
from test_support import verbose

def expectException(teststr, expected, failure=AssertionError):
    """Executes a statement passed in teststr, and raises an exception
       (failure) if the expected exception is *not* raised."""
    try:
        exec teststr
    except expected:
        pass
    else:
        raise failure

if verbose:
    print 'Running tests on getopt.short_has_arg'
assert getopt.short_has_arg('a', 'a:')
assert not getopt.short_has_arg('a', 'a')
expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)

if verbose:
    print 'Running tests on getopt.long_has_args'
has_arg, option = getopt.long_has_args('abc', ['abc='])
assert has_arg
assert option == 'abc'
has_arg, option = getopt.long_has_args('abc', ['abc'])
assert not has_arg
assert option == 'abc'
has_arg, option = getopt.long_has_args('abc', ['abcd'])
assert not has_arg
assert option == 'abcd'