110
113
"""Test that we can pass a plain '-' as an argument."""
111
114
self.assertEqual((['-'], {}), parse_args(cmd_commit(), ['-']))
116
def parse(self, options, args):
117
parser = option.get_optparser(dict((o.name, o) for o in options))
118
return parser.parse_args(args)
113
120
def test_conversion(self):
114
121
options = [option.Option('hello')]
115
opts, args = parse(options, ['--no-hello', '--hello'])
122
opts, args = self.parse(options, ['--no-hello', '--hello'])
116
123
self.assertEqual(True, opts.hello)
117
opts, args = parse(options, [])
124
opts, args = self.parse(options, [])
118
125
self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
119
opts, args = parse(options, ['--hello', '--no-hello'])
126
opts, args = self.parse(options, ['--hello', '--no-hello'])
120
127
self.assertEqual(False, opts.hello)
121
128
options = [option.Option('number', type=int)]
122
opts, args = parse(options, ['--number', '6'])
129
opts, args = self.parse(options, ['--number', '6'])
123
130
self.assertEqual(6, opts.number)
124
self.assertRaises(errors.BzrCommandError, parse, options, ['--number'])
125
self.assertRaises(errors.BzrCommandError, parse, options,
131
self.assertRaises(errors.BzrCommandError, self.parse, options,
133
self.assertRaises(errors.BzrCommandError, self.parse, options,
136
def test_registry_conversion(self):
137
registry = bzrdir.BzrDirFormatRegistry()
138
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
139
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
140
registry.set_default('one')
141
options = [option.RegistryOption('format', '', registry, str)]
142
opts, args = self.parse(options, ['--format', 'one'])
143
self.assertEqual({'format':'one'}, opts)
144
opts, args = self.parse(options, ['--format', 'two'])
145
self.assertEqual({'format':'two'}, opts)
146
self.assertRaises(errors.BadOptionValue, self.parse, options,
147
['--format', 'three'])
148
self.assertRaises(errors.BzrCommandError, self.parse, options,
150
options = [option.RegistryOption('format', '', registry, str,
151
value_switches=True)]
152
opts, args = self.parse(options, ['--two'])
153
self.assertEqual({'format':'two'}, opts)
154
opts, args = self.parse(options, ['--two', '--one'])
155
self.assertEqual({'format':'one'}, opts)
156
opts, args = self.parse(options, ['--two', '--one',
158
self.assertEqual({'format':'two'}, opts)
160
def test_registry_converter(self):
161
options = [option.RegistryOption('format', '',
162
bzrdir.format_registry, builtins.get_format_type)]
163
opts, args = self.parse(options, ['--format', 'knit'])
164
self.assertIsInstance(opts.format.repository_format,
165
repository.RepositoryFormatKnit1)
168
registry = bzrdir.BzrDirFormatRegistry()
169
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
170
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
171
registry.set_default('one')
172
options = [option.RegistryOption('format', 'format help', registry,
173
str, value_switches=True)]
174
parser = option.get_optparser(dict((o.name, o) for o in options))
175
value = parser.format_option_help()
176
self.assertContainsRe(value, 'format.*format help')
177
self.assertContainsRe(value, 'one.*one help')
128
179
def test_iter_switches(self):
129
180
opt = option.Option('hello', help='fg')
130
181
self.assertEqual(list(opt.iter_switches()),
135
186
opt = option.Option('hello', help='fg', type=int, argname='gar')
136
187
self.assertEqual(list(opt.iter_switches()),
137
188
[('hello', None, 'GAR', 'fg')])
189
registry = bzrdir.BzrDirFormatRegistry()
190
registry.register_metadir('one', 'RepositoryFormat7', 'one help')
191
registry.register_metadir('two', 'RepositoryFormatKnit1', 'two help')
192
registry.set_default('one')
193
opt = option.RegistryOption('format', 'format help', registry,
194
value_switches=False)
195
self.assertEqual(list(opt.iter_switches()),
196
[('format', None, 'ARG', 'format help')])
197
opt = option.RegistryOption('format', 'format help', registry,
199
self.assertEqual(list(opt.iter_switches()),
200
[('format', None, 'ARG', 'format help'),
201
('default', None, None, 'one help'),
202
('one', None, None, 'one help'),
203
('two', None, None, 'two help'),
139
206
# >>> parse_args('log -r 500'.split())
140
207
# (['log'], {'revision': [<RevisionSpec_int 500>]})