Exemplo n.º 1
0
 def test_getopt_with_arguments_mixed(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt arg0 -a arg1 -b arg2".split()
         gi = getopt.iter_getopt(argv, 'ab')
         for i in [('a', 1, '?', 3, None), ('b', 1, '?', 5, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], ['arg0', 'arg1', 'arg2'])
         self.assertTrue(stderr.tell() == 0)
Exemplo n.º 2
0
 def test_getopt_with_matched_option_combined(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -ab -cd".split()
         gi = getopt.iter_getopt(argv, 'abcd')
         for i in [('a', 1, '?', 1, None), ('b', 1, '?', 2, None),
                   ('c', 1, '?', 2, None), ('d', 1, '?', 3, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertTrue(stderr.tell() == 0)
Exemplo n.º 3
0
 def test_getopt_with_invalid_option(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -a -b -c -d".split()
         gi = getopt.iter_getopt(argv, 'abd')
         for i in [('a', 1, '?', 2, None), ('b', 1, '?', 3, None),
                   ('?', 1, 'c', 4, None), ('d', 1, 'c', 5, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], [])
         self.assertTrue(stderr.tell() > 0)
Exemplo n.º 4
0
 def test_getopt_with_two_hyphen(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -abc -- -- -a -b -c".split()
         gi = getopt.iter_getopt(argv, 'abc::')
         for i in [('a', 1, '?', 1, None), ('b', 1, '?', 1, None),
                   ('c', 1, '?', 2, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], ['--', '-a', '-b', '-c'])
         self.assertTrue(stderr.tell() == 0)
Exemplo n.º 5
0
 def test_getopt_with_option_with_optional_value(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -abcde".split()
         gi = getopt.iter_getopt(argv, 'abc::')
         for i in [('a', 1, '?', 1, None), ('b', 1, '?', 1, None),
                   ('c', 1, '?', 2, 'de')]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], [])
         self.assertTrue(stderr.tell() == 0)
Exemplo n.º 6
0
 def test_getopt_with_option_missing_required_value_quote(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -abc".split()
         gi = getopt.iter_getopt(argv, ':abc:')
         for i in [('a', 1, '?', 1, None), ('b', 1, '?', 1, None),
                   (':', 1, 'c', 2, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], [])
         self.assertTrue(stderr.tell() > 0)
Exemplo n.º 7
0
 def test_getopt_with_arguments_mixed_posixly_correct(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         with unittest.mock.patch('os.environ', {'POSIXLY_CORRECT': ''}):
             argv = "testopt -a arg0 -b arg1".split()
             gi = getopt.iter_getopt(argv, 'ab')
             for i in [('a', 1, '?', 2, None)]:
                 self.assertEqual(i , (gi.__next__(), \
                     gi.opterr, gi.optopt, gi.optind, gi.optarg))
             self.assertEqual(list(gi), [])
             self.assertEqual(gi.argv[gi.optind:], ['arg0', '-b', 'arg1'])
             self.assertTrue(stderr.tell() == 0)
Exemplo n.º 8
0
 def test_getopt_with_arguments_mixed_posixly_correct(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
       with unittest.mock.patch('os.environ', {'POSIXLY_CORRECT': ''}):
         argv = "testopt -a arg0 -b arg1".split()
         gi = getopt.iter_getopt(argv, 'ab')
         for i in [
             ('a', 1, '?', 2, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], ['arg0', '-b', 'arg1'])
         self.assertTrue(stderr.tell() == 0)
Exemplo n.º 9
0
 def test_getopt_with_arguments_mixed(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt arg0 -a arg1 -b arg2".split()
         gi = getopt.iter_getopt(argv, 'ab')
         for i in [
             ('a', 1, '?', 3, None),
             ('b', 1, '?', 5, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], ['arg0', 'arg1', 'arg2'])
         self.assertTrue(stderr.tell() == 0)
Exemplo n.º 10
0
 def test_getopt_with_matched_option_combined(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -ab -cd".split()
         gi = getopt.iter_getopt(argv, 'abcd')
         for i in [
             ('a', 1, '?', 1, None),
             ('b', 1, '?', 2, None),
             ('c', 1, '?', 2, None),
             ('d', 1, '?', 3, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertTrue(stderr.tell() == 0)
Exemplo n.º 11
0
 def test_getopt_with_two_hyphen_alt(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -abc de -- -- -a -b -c".split()
         gi = getopt.iter_getopt(argv, 'abc::')
         for i in [
             ('a', 1, '?', 1, None),
             ('b', 1, '?', 1, None),
             ('c', 1, '?', 2, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], ['de', '--', '-a', '-b', '-c'])
         self.assertTrue(stderr.tell() == 0)
Exemplo n.º 12
0
 def test_getopt_with_option_with_optional_value(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -abcde".split()
         gi = getopt.iter_getopt(argv, 'abc::')
         for i in [
             ('a', 1, '?', 1, None),
             ('b', 1, '?', 1, None),
             ('c', 1, '?', 2, 'de')]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], [])
         self.assertTrue(stderr.tell() == 0)
Exemplo n.º 13
0
 def test_getopt_with_option_missing_required_value_quote(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -abc".split()
         gi = getopt.iter_getopt(argv, ':abc:')
         for i in [
             ('a', 1, '?', 1, None),
             ('b', 1, '?', 1, None),
             (':', 1, 'c', 2, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], [])
         self.assertTrue(stderr.tell() > 0)
Exemplo n.º 14
0
 def test_getopt_with_invalid_option(self):
     with unittest.mock.patch('sys.stderr', new=io.StringIO()) as stderr:
         argv = "testopt -a -b -c -d".split()
         gi = getopt.iter_getopt(argv, 'abd')
         for i in [
             ('a', 1, '?', 2, None),
             ('b', 1, '?', 3, None),
             ('?', 1, 'c', 4, None),
             ('d', 1, 'c', 5, None)]:
             self.assertEqual(i , (gi.__next__(), \
                 gi.opterr, gi.optopt, gi.optind, gi.optarg))
         self.assertEqual(list(gi), [])
         self.assertEqual(gi.argv[gi.optind:], [])
         self.assertTrue(stderr.tell() > 0)
Exemplo n.º 15
0
#! /usr/bin/env python3
"""
Equivalent to
http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
"""

import sys
import os
from libcli.getopt import iter_getopt

if __name__ == '__main__':
    aflag = 0
    bflag = 0
    cvalue = None

    gi = iter_getopt(sys.argv, "abc:")
    for c in gi:
        if c == 'a':
            aflag = 1
        elif c == 'b':
            bflag = 1
        elif c == 'c':
            cvalue = gi.optarg
        elif c == '?':
            if gi.optopt == 'c':
                print("Option -{} requires an argument.".format(gi.optopt),
                      file=sys.stderr)
            else:
                print("Unknown option `{}'".format(repr(gi.optopt)),
                      file=sys.stderr)
            sys.exit(1)