Example #1
0
def get_argument_name_completions(command_table, query):
    command_name = query['subcommand']
    command = command_table[command_name]
    arguments = query['arguments']
    unused = {
        name: argument
        for name, argument in get_arguments(command).items() if not [
            option for option in argument.options_list if option in arguments
        ] and argument.type.settings.get('help') != '==SUPPRESS=='
    }
    defaults = get_defaults(unused)
    return [{
        'name':
        option,
        'kind':
        'argument_name',
        'required':
        is_required(argument),
        'default':
        not not defaults.get(name),
        'detail':
        'required'
        if is_required(argument) and not defaults.get(name) else None,
        'documentation':
        argument.type.settings.get('help'),
        'sortText':
        ('10_' if is_required(argument) and not defaults.get(name) else '20_')
        + option
    } for name, argument in unused.items() for option in argument.options_list]
Example #2
0
 def test_argument_defaults(self):
     command = self.command_table.get(TEST_COMMAND)
     self.assertIsNotNone(command)
     defaults = get_defaults(get_arguments(command))
     self.assertIsNotNone(defaults)
     self.assertTrue(defaults.get(TEST_ARGUMENT_WITH_DEFAULT))
     self.assertFalse(defaults.get(TEST_ARGUMENT_WITHOUT_DEFAULT))
Example #3
0
 def test_argument_choices(self):
     command = self.command_table.get(TEST_COMMAND_WITH_CHOICES)
     self.assertIsNotNone(command)
     argument = get_arguments(command)[TEST_ARGUMENT_WITH_CHOICES]
     self.assertIsNotNone(argument)
     self.assertIsNotNone(argument.choices)
     self.assertIsNone(argument.completer)
     self.assertNotEqual(0, len(argument.choices))
Example #4
0
 def test_argument_completer(self):
     command = self.command_table.get(TEST_COMMAND_WITH_COMPLETER)
     self.assertIsNotNone(command)
     argument = get_arguments(command)[TEST_ARGUMENT_WITH_COMPLETER]
     self.assertIsNotNone(argument)
     self.assertIsNone(argument.choices)
     self.assertIsNotNone(argument.completer)
     values = run_argument_value_completer(command, argument, {})
     self.assertTrue(isinstance(values, collections.Sequence))
Example #5
0
def get_hover_text(group_index, command_table, command):
    subcommand = command['subcommand']
    if 'argument' in command and subcommand in command_table:
        argument_name = command['argument']
        argument = next(
            (argument
             for argument in get_arguments(command_table[subcommand]).values()
             if argument_name in argument.options_list), None)
        if argument:
            req = is_required(argument)
            return {
                'paragraphs': [
                    '`' + ' '.join(argument.options_list) + '`' +
                    ('*' if req else '') + ': ' +
                    argument.type.settings.get('help') +
                    ('\n\n*Required' if req else '')
                ]
            }
        argument = next((argument for argument in GLOBAL_ARGUMENTS.values()
                         if argument_name in argument['options']), None)
        if argument:
            return {
                'paragraphs': [
                    '`' + ' '.join(argument['options']) + '`: ' +
                    argument['help']
                ]
            }
        return

    help = get_help(subcommand)
    if help:
        short_summary = help.get('short-summary')
        if short_summary:
            paragraphs = [
                '{1}\n\n`{0}`\n\n{2}'.format(subcommand, short_summary,
                                             help.get('long-summary',
                                                      '')).strip()
            ]
            if subcommand in command_table:
                list = sorted(
                    [
                        argument for argument in get_arguments(
                            command_table[subcommand]).values()
                        if argument.type.settings.get('help') != '==SUPPRESS=='
                    ],
                    key=lambda e: str(not is_required(e)) + e.options_list[0])
                if list:
                    paragraphs.append('Arguments\n' + '\n'.join([
                        '- `' + ' '.join(argument.options_list) + '`' +
                        ('*' if is_required(argument) else '') + ': ' +
                        (argument.type.settings.get('help') or '')
                        for argument in list
                    ]) + ('\n\n*Required' if is_required(list[0]) else ''))
                paragraphs.append('Global Arguments\n' + '\n'.join([
                    '- `' + ' '.join(argument['options']) + '`: ' +
                    argument['help'] for argument in GLOBAL_ARGUMENTS.values()
                ]))
            elif subcommand in group_index:
                list = sorted(group_index[subcommand], key=lambda e: e['name'])
                groups = [
                    element for element in list if element['kind'] == 'group'
                ]
                if groups:
                    paragraphs.append('Subgroups\n' + '\n'.join([
                        '- `' + element['name'] + '`: ' +
                        get_short_summary(element.get('detail'), '-')
                        for element in groups
                    ]))
                commands = [
                    element for element in list if element['kind'] == 'command'
                ]
                if commands:
                    paragraphs.append('Commands\n' + '\n'.join([
                        '- `' + element['name'] + '`: ' +
                        get_short_summary(element.get('detail'), '-')
                        for element in commands
                    ]))
            examples = help.get('examples')
            if examples:
                paragraphs.append('Examples\n\n' + '\n\n'.join([
                    '{0}\n```azcli\n{1}\n```'.format(example['name'].strip(),
                                                     example['text'].strip())
                    for example in examples
                ]))
            return {'paragraphs': paragraphs}
        return
Example #6
0
def get_argument(command, argument_name):
    for name, argument in get_arguments(command).items():
        if argument_name in argument.options_list:
            return name, argument
    return None, None
Example #7
0
 def test_is_linux_optional(self):
     command = self.command_table.get('appservice plan create')
     self.assertIsNotNone(command)
     self.assertFalse(is_required(get_arguments(command).get('is_linux')))
Example #8
0
 def test_required_argument(self):
     command = self.command_table.get(TEST_COMMAND)
     self.assertIsNotNone(command)
     self.assertTrue(is_required(get_arguments(command).get(TEST_ARGUMENT)))
     self.assertFalse(
         is_required(get_arguments(command).get(TEST_OPTIONAL_ARGUMENT)))
Example #9
0
 def test_argument_help(self):
     command = self.command_table.get(TEST_COMMAND)
     self.assertIsNotNone(command)
     argument = get_arguments(command).get(TEST_ARGUMENT)
     self.assertIsNotNone(argument)
     self.assertTrue(argument.type.settings.get('help'))
Example #10
0
 def test_argument(self):
     command = self.command_table.get(TEST_COMMAND)
     self.assertIsNotNone(command)
     argument = get_arguments(command).get(TEST_ARGUMENT)
     self.assertIsNotNone(argument)
     self.assertSequenceEqual(TEST_ARGUMENT_OPTIONS, argument.options_list)