Ejemplo n.º 1
0
 def test_long_has_args(self):
     has_arg, option = getopt.long_has_args('abc', ['abc='])
     self.assertTrue(has_arg)
     self.assertEqual(option, 'abc')
     has_arg, option = getopt.long_has_args('abc', ['abc'])
     self.assertFalse(has_arg)
     self.assertEqual(option, 'abc')
     has_arg, option = getopt.long_has_args('abc', ['abcd'])
     self.assertFalse(has_arg)
     self.assertEqual(option, 'abcd')
     self.assertError(getopt.long_has_args, 'abc', ['def'])
     self.assertError(getopt.long_has_args, 'abc', [])
     self.assertError(getopt.long_has_args, 'abc', ['abcd', 'abcde'])
Ejemplo n.º 2
0
    def test_long_has_args(self):
        has_arg, option = getopt.long_has_args('abc', ['abc='])
        self.failUnless(has_arg)
        self.assertEqual(option, 'abc')

        has_arg, option = getopt.long_has_args('abc', ['abc'])
        self.failIf(has_arg)
        self.assertEqual(option, 'abc')

        has_arg, option = getopt.long_has_args('abc', ['abcd'])
        self.failIf(has_arg)
        self.assertEqual(option, 'abcd')

        self.assertError(getopt.long_has_args, 'abc', ['def'])
        self.assertError(getopt.long_has_args, 'abc', [])
        self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
    def test_long_has_args(self):
        has_arg, option = getopt.long_has_args('abc', ['abc='])
        self.assertTrue(has_arg)
        self.assertEqual(option, 'abc')

        has_arg, option = getopt.long_has_args('abc', ['abc'])
        self.assertFalse(has_arg)
        self.assertEqual(option, 'abc')

        has_arg, option = getopt.long_has_args('abc', ['abcd'])
        self.assertFalse(has_arg)
        self.assertEqual(option, 'abcd')

        self.assertError(getopt.long_has_args, 'abc', ['def'])
        self.assertError(getopt.long_has_args, 'abc', [])
        self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
Ejemplo n.º 4
0
    def test_long_has_args(self):
        has_arg, option = getopt.long_has_args("abc", ["abc="])
        self.failUnless(has_arg)
        self.assertEqual(option, "abc")

        has_arg, option = getopt.long_has_args("abc", ["abc"])
        self.failIf(has_arg)
        self.assertEqual(option, "abc")

        has_arg, option = getopt.long_has_args("abc", ["abcd"])
        self.failIf(has_arg)
        self.assertEqual(option, "abcd")

        self.assertError(getopt.long_has_args, "abc", ["def"])
        self.assertError(getopt.long_has_args, "abc", [])
        self.assertError(getopt.long_has_args, "abc", ["abcd", "abcde"])
Ejemplo n.º 5
0
def do_longs(opts, opt, longopts, args):
    """Modified version of `getopt.do_longs()` which doesn't bail out if
    a long option is not in the list of command line arguments.

    :Parameters:
      - `opts` (`list`) list of tuples (long option, value)
      - `opt` (`str`) option to check
      - `longopts` (`list`) defined long options
      - `args` (`str`) list of provided arguments

    :return: (opts, args) `opts` tuple (long option, value) and arguments
    :rtype: `tuple`
    :raise `getopt.GetoptError`: problem with arguments to an option
    """
    try:
        i = opt.index('=')
    except ValueError:
        optarg = None
    else:
        opt, optarg = opt[:i], opt[i+1:]

    # has_arg, opt = getopt.long_has_args(opt, longopts)

    # catch 'not recognized' exception and add some logic
    # ============
    try:
        opt_save = opt
        has_arg, opt = getopt.long_has_args(opt, longopts)
    except getopt.GetoptError, e:
        if 'not recognized' in str(e):
            if args and not args[0].startswith('-'):
                has_arg = True
                opt = opt_save
            else:
                opts.append(('--' + opt_save, ''))
                return opts, args
        else:
            raise getopt.GetoptError(e.msg, e.opt)
Ejemplo n.º 6
0
        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')
expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
                GetoptError)
expectException("has_arg, option = getopt.long_has_args('abc', [])",
                GetoptError)
expectException("has_arg, option = " + \
                     "getopt.long_has_args('abc', ['abcd','abcde'])",
                GetoptError)
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
 def update_event(self, inp=-1):
     self.set_output_val(0,
                         getopt.long_has_args(self.input(0), self.input(1)))
Ejemplo n.º 9
0

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')
expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
                GetoptError)
expectException("has_arg, option = getopt.long_has_args('abc', [])",
                GetoptError)
expectException("has_arg, option = " + \
                     "getopt.long_has_args('abc', ['abcd','abcde'])",
                GetoptError)