Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_getopt.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | b36cb4602e21 |
| children |
| rev | line source |
|---|---|
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1 # test_getopt.py |
|
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2 # David Goodger <dgoodger@bigfoot.com> 2000-08-19 |
|
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3 |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
4 from test.test_support import verbose, run_doctest, run_unittest, EnvironmentVarGuard |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
5 import unittest |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
6 |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
7 import getopt |
|
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
8 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
9 sentinel = object() |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
10 |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
11 class GetoptTests(unittest.TestCase): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
12 def setUp(self): |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
13 self.env = EnvironmentVarGuard() |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
14 if "POSIXLY_CORRECT" in self.env: |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
15 del self.env["POSIXLY_CORRECT"] |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
16 |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
17 def tearDown(self): |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
18 self.env.__exit__() |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
19 del self.env |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
20 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
21 def assertError(self, *args, **kwargs): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
22 self.assertRaises(getopt.GetoptError, *args, **kwargs) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
23 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
24 def test_short_has_arg(self): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
25 self.assertTrue(getopt.short_has_arg('a', 'a:')) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
26 self.assertFalse(getopt.short_has_arg('a', 'a')) |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
27 self.assertError(getopt.short_has_arg, 'a', 'b') |
|
32934
91fa80a67811
avoid fragility: make sure POSIXLY_CORRECT is completely controlled
Fred Drake <fdrake@acm.org>
parents:
32922
diff
changeset
|
28 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
29 def test_long_has_args(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
30 has_arg, option = getopt.long_has_args('abc', ['abc=']) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
31 self.assertTrue(has_arg) |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
32 self.assertEqual(option, 'abc') |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
33 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
34 has_arg, option = getopt.long_has_args('abc', ['abc']) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
35 self.assertFalse(has_arg) |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
36 self.assertEqual(option, 'abc') |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
37 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
38 has_arg, option = getopt.long_has_args('abc', ['abcd']) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
39 self.assertFalse(has_arg) |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
40 self.assertEqual(option, 'abcd') |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
41 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
42 self.assertError(getopt.long_has_args, 'abc', ['def']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
43 self.assertError(getopt.long_has_args, 'abc', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
44 self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde']) |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
45 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
46 def test_do_shorts(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
47 opts, args = getopt.do_shorts([], 'a', 'a', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
48 self.assertEqual(opts, [('-a', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
49 self.assertEqual(args, []) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
50 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
51 opts, args = getopt.do_shorts([], 'a1', 'a:', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
52 self.assertEqual(opts, [('-a', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
53 self.assertEqual(args, []) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
54 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
55 #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
56 #self.assertEqual(opts, [('-a', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
57 #self.assertEqual(args, []) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
58 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
59 opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
60 self.assertEqual(opts, [('-a', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
61 self.assertEqual(args, []) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
62 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
63 opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
64 self.assertEqual(opts, [('-a', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
65 self.assertEqual(args, ['2']) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
66 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
67 self.assertError(getopt.do_shorts, [], 'a1', 'a', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
68 self.assertError(getopt.do_shorts, [], 'a', 'a:', []) |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
69 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
70 def test_do_longs(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
71 opts, args = getopt.do_longs([], 'abc', ['abc'], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
72 self.assertEqual(opts, [('--abc', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
73 self.assertEqual(args, []) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
74 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
75 opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
76 self.assertEqual(opts, [('--abc', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
77 self.assertEqual(args, []) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
78 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
79 opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
80 self.assertEqual(opts, [('--abcd', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
81 self.assertEqual(args, []) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
82 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
83 opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
84 self.assertEqual(opts, [('--abc', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
85 self.assertEqual(args, []) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
86 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
87 # Much like the preceding, except with a non-alpha character ("-") in |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
88 # option name that precedes "="; failed in |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
89 # http://python.org/sf/126863 |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
90 opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
91 self.assertEqual(opts, [('--foo', '42')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
92 self.assertEqual(args, []) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
93 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
94 self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
95 self.assertError(getopt.do_longs, [], 'abc', ['abc='], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
96 |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
97 def test_getopt(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
98 # note: the empty string between '-a' and '--beta' is significant: |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
99 # it simulates an empty string option argument ('-a ""') on the |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
100 # command line. |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
101 cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
102 '', '--beta', 'arg1', 'arg2'] |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
103 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
104 opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
105 self.assertEqual(opts, [('-a', '1'), ('-b', ''), |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
106 ('--alpha', '2'), ('--beta', ''), |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
107 ('-a', '3'), ('-a', ''), ('--beta', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
108 # Note ambiguity of ('-b', '') and ('-a', '') above. This must be |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
109 # accounted for in the code that calls getopt(). |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
110 self.assertEqual(args, ['arg1', 'arg2']) |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
111 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
112 self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta']) |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
113 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
114 def test_gnu_getopt(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
115 # Test handling of GNU style scanning mode. |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
116 cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2'] |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
117 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
118 # GNU style |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
119 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
120 self.assertEqual(args, ['arg1']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
121 self.assertEqual(opts, [('-a', ''), ('-b', '1'), |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
122 ('--alpha', ''), ('--beta', '2')]) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
123 |
|
50011
20ab2260dc93
#4458: recognize "-" as an argument, not a malformed option in gnu_getopt().
Georg Brandl <georg@python.org>
parents:
44034
diff
changeset
|
124 # recognize "-" as an argument |
|
20ab2260dc93
#4458: recognize "-" as an argument, not a malformed option in gnu_getopt().
Georg Brandl <georg@python.org>
parents:
44034
diff
changeset
|
125 opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', []) |
|
20ab2260dc93
#4458: recognize "-" as an argument, not a malformed option in gnu_getopt().
Georg Brandl <georg@python.org>
parents:
44034
diff
changeset
|
126 self.assertEqual(args, ['-']) |
|
20ab2260dc93
#4458: recognize "-" as an argument, not a malformed option in gnu_getopt().
Georg Brandl <georg@python.org>
parents:
44034
diff
changeset
|
127 self.assertEqual(opts, [('-a', ''), ('-b', '-')]) |
|
20ab2260dc93
#4458: recognize "-" as an argument, not a malformed option in gnu_getopt().
Georg Brandl <georg@python.org>
parents:
44034
diff
changeset
|
128 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
129 # Posix style via + |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
130 opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta=']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
131 self.assertEqual(opts, [('-a', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
132 self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2']) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
133 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
134 # Posix style via POSIXLY_CORRECT |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
135 self.env["POSIXLY_CORRECT"] = "1" |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
136 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
137 self.assertEqual(opts, [('-a', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
138 self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2']) |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
139 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
140 def test_libref_examples(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
141 s = """ |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
142 Examples from the Library Reference: Doc/lib/libgetopt.tex |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
143 |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
144 An example using only Unix style options: |
|
32934
91fa80a67811
avoid fragility: make sure POSIXLY_CORRECT is completely controlled
Fred Drake <fdrake@acm.org>
parents:
32922
diff
changeset
|
145 |
|
91fa80a67811
avoid fragility: make sure POSIXLY_CORRECT is completely controlled
Fred Drake <fdrake@acm.org>
parents:
32922
diff
changeset
|
146 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
147 >>> import getopt |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
148 >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
149 >>> args |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
150 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
151 >>> optlist, args = getopt.getopt(args, 'abc:d:') |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
152 >>> optlist |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
153 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
154 >>> args |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
155 ['a1', 'a2'] |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
15957
diff
changeset
|
156 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
157 Using long option names is equally easy: |
|
28891
d5e291f0ae39
Add doctest for example in the library reference.
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
158 |
|
d5e291f0ae39
Add doctest for example in the library reference.
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
159 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
160 >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
161 >>> args = s.split() |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
162 >>> args |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
163 ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
164 >>> optlist, args = getopt.getopt(args, 'x', [ |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
165 ... 'condition=', 'output-file=', 'testing']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
166 >>> optlist |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
167 [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
168 >>> args |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
169 ['a1', 'a2'] |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
170 """ |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
171 |
|
44034
5d1049d4c3c6
Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents:
41532
diff
changeset
|
172 import types |
|
5d1049d4c3c6
Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents:
41532
diff
changeset
|
173 m = types.ModuleType("libreftest", s) |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
174 run_doctest(m, verbose) |
|
28891
d5e291f0ae39
Add doctest for example in the library reference.
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
175 |
|
63191
ed328e672ace
Merged revisions 83117 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
44034
diff
changeset
|
176 def test_issue4629(self): |
|
ed328e672ace
Merged revisions 83117 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
44034
diff
changeset
|
177 longopts, shortopts = getopt.getopt(['--help='], '', ['help=']) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63189
diff
changeset
|
178 self.assertEqual(longopts, [('--help', '')]) |
|
63191
ed328e672ace
Merged revisions 83117 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
44034
diff
changeset
|
179 longopts, shortopts = getopt.getopt(['--help=x'], '', ['help=']) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63189
diff
changeset
|
180 self.assertEqual(longopts, [('--help', 'x')]) |
|
63191
ed328e672ace
Merged revisions 83117 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
44034
diff
changeset
|
181 self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help']) |
|
28891
d5e291f0ae39
Add doctest for example in the library reference.
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
182 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
183 def test_main(): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
184 run_unittest(GetoptTests) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
185 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
186 if __name__ == "__main__": |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
187 test_main() |
