13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
# TODO: For things like --diff-prefix, we want a way to customize the display
18
18
# of the option argument.
152
152
def __init__(self, name, help='', type=None, argname=None,
153
short_name=None, param_name=None, custom_callback=None):
153
short_name=None, param_name=None, custom_callback=None,
154
155
"""Make a new command option.
156
157
:param name: regular name of the command, used in the double-dash
157
form and also as the parameter to the command's run()
158
form and also as the parameter to the command's run()
158
159
method (unless param_name is specified).
160
161
:param help: help message displayed in command help
162
:param type: function called to parse the option argument, or
163
:param type: function called to parse the option argument, or
163
164
None (default) if this option doesn't take an argument.
165
166
:param argname: name of option argument, if any
173
174
:param custom_callback: a callback routine to be called after normal
174
175
processing. The signature of the callback routine is
175
176
(option, name, new_value, parser).
177
:param hidden: If True, the option should be hidden in help and
186
189
self.argname = argname
187
190
if param_name is None:
188
self._param_name = self.name
191
self._param_name = self.name.replace('-', '_')
190
193
self._param_name = param_name
191
194
self.custom_callback = custom_callback
193
197
def short_name(self):
194
198
if self._short_name:
208
212
option_strings = ['--%s' % self.name]
209
213
if short_name is not None:
210
214
option_strings.append('-%s' % short_name)
216
help = optparse.SUPPRESS_HELP
211
219
optargfn = self.type
212
220
if optargfn is None:
213
parser.add_option(action='callback',
214
callback=self._optparse_bool_callback,
221
parser.add_option(action='callback',
222
callback=self._optparse_bool_callback,
215
223
callback_args=(True,),
218
226
negation_strings = ['--%s' % self.get_negation_name()]
219
parser.add_option(action='callback',
220
callback=self._optparse_bool_callback,
227
parser.add_option(action='callback',
228
callback=self._optparse_bool_callback,
221
229
callback_args=(False,),
222
230
help=optparse.SUPPRESS_HELP, *negation_strings)
224
parser.add_option(action='callback',
225
callback=self._optparse_callback,
232
parser.add_option(action='callback',
233
callback=self._optparse_callback,
226
234
type='string', metavar=self.argname.upper(),
228
default=OptionParser.DEFAULT_VALUE,
236
default=OptionParser.DEFAULT_VALUE,
231
239
def _optparse_bool_callback(self, option, opt_str, value, parser, bool_v):
250
258
yield self.name, self.short_name(), argname, self.help
252
260
def is_hidden(self, name):
256
264
class ListOption(Option):
307
315
return self.converter(value)
309
def __init__(self, name, help, registry, converter=None,
310
value_switches=False, title=None, enum_switch=True):
317
def __init__(self, name, help, registry=None, converter=None,
318
value_switches=False, title=None, enum_switch=True,
321
330
'--knit' can be used interchangeably.
322
331
:param enum_switch: If true, a switch is provided with the option name,
323
332
which takes a value.
333
:param lazy_registry: A tuple of (module name, attribute name) for a
334
registry to be lazily loaded.
325
336
Option.__init__(self, name, help, type=self.convert)
326
self.registry = registry
337
self._registry = registry
339
if lazy_registry is None:
340
raise AssertionError(
341
'One of registry or lazy_registry must be given.')
342
self._lazy_registry = _mod_registry._LazyObjectGetter(
344
if registry is not None and lazy_registry is not None:
345
raise AssertionError(
346
'registry and lazy_registry are mutually exclusive')
328
348
self.converter = converter
329
349
self.value_switches = value_switches
332
352
if self.title is None:
333
353
self.title = name
357
if self._registry is None:
358
self._registry = self._lazy_registry.get_obj()
359
return self._registry
336
362
def from_kwargs(name_, help=None, title=None, value_switches=False,
337
363
enum_switch=True, **kwargs):
341
367
RegistryOption constructor. Any other keyword arguments are treated
342
368
as values for the option, and they value is treated as the help.
344
reg = registry.Registry()
370
reg = _mod_registry.Registry()
345
371
for name, switch_help in kwargs.iteritems():
346
372
name = name.replace('_', '-')
347
373
reg.register(name, name, help=switch_help)
374
if not value_switches:
375
help = help + ' "' + name + '": ' + switch_help
376
if not help.endswith("."):
348
378
return RegistryOption(name_, help, reg, title=title,
349
379
value_switches=value_switches, enum_switch=enum_switch)
430
460
Option.OPTIONS[name] = Option(name, **kwargs)
433
def _global_registry_option(name, help, registry, **kwargs):
463
def _global_registry_option(name, help, registry=None, **kwargs):
434
464
Option.OPTIONS[name] = RegistryOption(name, help, registry, **kwargs)
437
class MergeTypeRegistry(registry.Registry):
442
467
# This is the verbosity level detected during command line parsing.
443
468
# Note that the final value is dependent on the order in which the
444
469
# various flags (verbose, quiet, no-verbose, no-quiet) are given.
467
492
_verbosity_level = -1
495
class MergeTypeRegistry(_mod_registry.Registry):
470
500
_merge_type_registry = MergeTypeRegistry()
471
501
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
472
502
"Native diff3-style merge")
480
510
# Declare the standard options
481
511
_standard_option('help', short_name='h',
482
512
help='Show help message.')
513
_standard_option('usage',
514
help='Show usage message and options.')
483
515
_standard_option('verbose', short_name='v',
484
516
help='Display more information.',
485
517
custom_callback=_verbosity_level_callback)
515
547
help='Select changes introduced by the specified revision. See also "help revisionspec".')
516
548
_global_option('show-ids',
517
549
help='Show internal object ids.')
518
_global_option('timezone',
550
_global_option('timezone',
520
help='display timezone as local, original, or utc')
552
help='Display timezone as local, original, or utc.')
521
553
_global_option('unbound')
522
554
_global_option('version')
523
555
_global_option('email')
524
556
_global_option('update')
525
557
_global_registry_option('log-format', "Use specified log format.",
526
log.log_formatter_registry, value_switches=True,
558
lazy_registry=('bzrlib.log', 'log_formatter_registry'),
559
value_switches=True, title='Log format')
528
560
_global_option('long', help='Use detailed log format. Same as --log-format long',
530
562
_global_option('short', help='Use moderately short log format. Same as --log-format short')
542
574
_global_option('dry-run',
543
575
help="Show what would be done, but don't actually do anything.")
544
576
_global_option('name-from-revision', help='The path name in the old tree.')
578
diff_writer_registry = _mod_registry.Registry()
579
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')
580
diff_writer_registry.default_key = 'plain'