def test_option_callback_list(self):
     """Test callbacks work for list options."""
     cb_calls = []
     def cb(option, name, value, parser):
         # Note that the value is a reference so copy to keep it
         cb_calls.append((option,name,value[:],parser))
     options = [option.ListOption('hello', type=str, custom_callback=cb)]
     opts, args = self.parse(options, ['--hello=world', '--hello=mars',
         '--hello=-'])
     self.assertEqual(3, len(cb_calls))
     opt,name,value,parser = cb_calls[0]
     self.assertEqual('hello', name)
     self.assertEqual(['world'], value)
     opt,name,value,parser = cb_calls[1]
     self.assertEqual('hello', name)
     self.assertEqual(['world', 'mars'], value)
     opt,name,value,parser = cb_calls[2]
     self.assertEqual('hello', name)
     self.assertEqual([], value)
示例#2
0
class cmd_bash_completion(commands.Command):
    __doc__ = """Generate a shell function for bash command line completion.

    This command generates a shell function which can be used by bash to
    automatically complete the currently typed command when the user presses
    the completion key (usually tab).
    
    Commonly used like this:
        eval "`bzr bash-completion`"
    """

    takes_options = [
        option.Option("function-name",
                      short_name="f",
                      type=str,
                      argname="name",
                      help="Name of the generated function (default: _bzr)"),
        option.Option(
            "function-only",
            short_name="o",
            type=None,
            help="Generate only the shell function, don't enable it"),
        option.Option("debug",
                      type=None,
                      hidden=True,
                      help="Enable shell code useful for debugging"),
        option.ListOption(
            "plugin",
            type=str,
            argname="name",
            # param_name="selected_plugins", # doesn't work, bug #387117
            help="Enable completions for the selected plugin" +
            " (default: all plugins)"),
    ]

    def run(self, **kwargs):
        if 'plugin' in kwargs:
            # work around bug #387117 which prevents us from using param_name
            if len(kwargs['plugin']) > 0:
                kwargs['selected_plugins'] = kwargs['plugin']
            del kwargs['plugin']
        bash_completion_function(sys.stdout, **kwargs)
 def test_list_option_param_name(self):
     """Test list options can have their param_name set."""
     options = [option.ListOption('hello', type=str, param_name='greeting')]
     opts, args = self.parse(
         options, ['--hello=world', '--hello=sailor'])
     self.assertEqual(['world', 'sailor'], opts.greeting)
 def test_list_option_can_be_reset(self):
     """Passing an option of '-' to a list option should reset the list."""
     options = [option.ListOption('hello', type=str)]
     opts, args = self.parse(
         options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
     self.assertEqual(['c'], opts.hello)
 def test_list_option_with_int_type_can_be_reset(self):
     options = [option.ListOption('hello', type=int)]
     opts, args = self.parse(options, ['--hello=2', '--hello=3',
                                       '--hello=-', '--hello=5'])
     self.assertEqual([5], opts.hello)
 def test_list_option_with_int_type(self):
     options = [option.ListOption('hello', type=int)]
     opts, args = self.parse(options, ['--hello=2', '--hello=3'])
     self.assertEqual([2, 3], opts.hello)
 def test_list_option_no_arguments(self):
     options = [option.ListOption('hello', type=str)]
     opts, args = self.parse(options, [])
     self.assertEqual([], opts.hello)
 def test_list_option_with_dash(self):
     options = [option.ListOption('with-dash', type=str)]
     opts, args = self.parse(options, ['--with-dash=world',
                                       '--with-dash=sailor'])
     self.assertEqual(['world', 'sailor'], opts.with_dash)
 def test_list_option(self):
     options = [option.ListOption('hello', type=str)]
     opts, args = self.parse(options, ['--hello=world', '--hello=sailor'])
     self.assertEqual(['world', 'sailor'], opts.hello)