Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_getopt.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 85542715a249 |
| children | 6768b2350c57 |
| 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 |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
15957
diff
changeset
|
8 import os |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
9 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
10 sentinel = object() |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
11 |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
12 class GetoptTests(unittest.TestCase): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
13 def setUp(self): |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
14 self.env = EnvironmentVarGuard() |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
15 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
|
16 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
|
17 |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
18 def tearDown(self): |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
19 self.env.__exit__() |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
50011
diff
changeset
|
20 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
|
21 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
22 def assertError(self, *args, **kwargs): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
23 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
|
24 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
25 def test_short_has_arg(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
26 self.failUnless(getopt.short_has_arg('a', 'a:')) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
27 self.failIf(getopt.short_has_arg('a', 'a')) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
28 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
|
29 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
30 def test_long_has_args(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
31 has_arg, option = getopt.long_has_args('abc', ['abc=']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
32 self.failUnless(has_arg) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
33 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
|
34 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
35 has_arg, option = getopt.long_has_args('abc', ['abc']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
36 self.failIf(has_arg) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
37 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
|
38 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
39 has_arg, option = getopt.long_has_args('abc', ['abcd']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
40 self.failIf(has_arg) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
41 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
|
42 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
43 self.assertError(getopt.long_has_args, 'abc', ['def']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
44 self.assertError(getopt.long_has_args, 'abc', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
45 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
|
46 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
47 def test_do_shorts(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
48 opts, args = getopt.do_shorts([], 'a', 'a', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
49 self.assertEqual(opts, [('-a', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
50 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
|
51 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
52 opts, args = getopt.do_shorts([], 'a1', 'a:', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
53 self.assertEqual(opts, [('-a', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
54 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
|
55 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
56 #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
57 #self.assertEqual(opts, [('-a', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
58 #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
|
59 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
60 opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
61 self.assertEqual(opts, [('-a', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
62 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
|
63 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
64 opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
65 self.assertEqual(opts, [('-a', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
66 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
|
67 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
68 self.assertError(getopt.do_shorts, [], 'a1', 'a', []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
69 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
|
70 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
71 def test_do_longs(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
72 opts, args = getopt.do_longs([], 'abc', ['abc'], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
73 self.assertEqual(opts, [('--abc', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
74 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
|
75 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
76 opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
77 self.assertEqual(opts, [('--abc', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
78 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
|
79 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
80 opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
81 self.assertEqual(opts, [('--abcd', '1')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
82 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
|
83 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
84 opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
85 self.assertEqual(opts, [('--abc', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
86 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
|
87 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
88 # 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
|
89 # option name that precedes "="; failed in |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
90 # http://python.org/sf/126863 |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
91 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
|
92 self.assertEqual(opts, [('--foo', '42')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
93 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
|
94 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
95 self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
96 self.assertError(getopt.do_longs, [], 'abc', ['abc='], []) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
97 |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
98 def test_getopt(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
99 # 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
|
100 # 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
|
101 # command line. |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
102 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
|
103 '', '--beta', 'arg1', 'arg2'] |
|
14078
b652a9aa94f1
David Goodger's new getopt test module (thanks, David!).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
104 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
105 opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
106 self.assertEqual(opts, [('-a', '1'), ('-b', ''), |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
107 ('--alpha', '2'), ('--beta', ''), |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
108 ('-a', '3'), ('-a', ''), ('--beta', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
109 # 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
|
110 # accounted for in the code that calls getopt(). |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
111 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
|
112 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
113 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
|
114 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
115 def test_gnu_getopt(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
116 # Test handling of GNU style scanning mode. |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
117 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
|
118 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
119 # GNU style |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
120 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
121 self.assertEqual(args, ['arg1']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
122 self.assertEqual(opts, [('-a', ''), ('-b', '1'), |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
123 ('--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
|
124 |
|
50011
20ab2260dc93
#4458: recognize "-" as an argument, not a malformed option in gnu_getopt().
Georg Brandl <georg@python.org>
parents:
44034
diff
changeset
|
125 # 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
|
126 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
|
127 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
|
128 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
|
129 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
130 # Posix style via + |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
131 opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta=']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
132 self.assertEqual(opts, [('-a', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
133 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
|
134 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
135 # 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
|
136 self.env["POSIXLY_CORRECT"] = "1" |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
137 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
138 self.assertEqual(opts, [('-a', '')]) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
139 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
|
140 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
141 def test_libref_examples(self): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
142 s = """ |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
143 Examples from the Library Reference: Doc/lib/libgetopt.tex |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
144 |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
145 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
|
146 |
|
91fa80a67811
avoid fragility: make sure POSIXLY_CORRECT is completely controlled
Fred Drake <fdrake@acm.org>
parents:
32922
diff
changeset
|
147 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
148 >>> import getopt |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
149 >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
150 >>> args |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
151 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
152 >>> optlist, args = getopt.getopt(args, 'abc:d:') |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
153 >>> optlist |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
154 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
155 >>> args |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
156 ['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
|
157 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
158 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
|
159 |
|
d5e291f0ae39
Add doctest for example in the library reference.
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
160 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
161 >>> 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
|
162 >>> args = s.split() |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
163 >>> args |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
164 ['--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
|
165 >>> optlist, args = getopt.getopt(args, 'x', [ |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
166 ... 'condition=', 'output-file=', 'testing']) |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
167 >>> optlist |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
168 [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
169 >>> args |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
170 ['a1', 'a2'] |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
171 """ |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41383
diff
changeset
|
172 |
|
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
|
173 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
|
174 m = types.ModuleType("libreftest", s) |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
175 run_doctest(m, verbose) |
|
28891
d5e291f0ae39
Add doctest for example in the library reference.
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
176 |
|
d5e291f0ae39
Add doctest for example in the library reference.
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
177 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
178 def test_main(): |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
179 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
|
180 |
|
41383
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
181 if __name__ == "__main__": |
|
43250018285c
Make test_getopt use unittest.
Collin Winter <collinw@gmail.com>
parents:
32934
diff
changeset
|
182 test_main() |
