예제 #1
0
 def test_command_help(self):
     help = get_help(TEST_COMMAND)
     self.assertIsNotNone(help)
     self.assertTrue(help.get('short-summary'))
     examples = help.get('examples')
     self.assertNotEqual(0, len(examples))
     self.assertTrue(examples[0]['name'])
     self.assertTrue(examples[0]['text'])
예제 #2
0
def get_group_index(command_table):
    index = {'': [], '-': []}
    for command in command_table.values():
        parts = command.name.split()
        len_parts = len(parts)
        for i in range(1, len_parts):
            group = ' '.join(parts[0:i])
            if group not in index:
                index[group] = []
                parent = ' '.join(parts[0:i - 1])
                completion = {
                    'name': parts[i - 1],
                    'kind': 'group',
                    'detail': group
                }
                help = get_help(group)
                if help:
                    description = help.get('short-summary')
                    if description:
                        completion['documentation'] = description

                index[parent].append(completion)
                if NO_AZ_PREFIX_COMPLETION_ENABLED and i == 1:
                    add = completion.copy()
                    add['snippet'] = 'az ' + add['name']
                    index['-'].append(add)

                if TWO_SEGMENTS_COMPLETION_ENABLED and i > 1:
                    add = completion.copy()
                    add['name'] = ' '.join(parts[i - 2:i])
                    index[' '.join(parts[0:i - 2])].append(add)
                    if NO_AZ_PREFIX_COMPLETION_ENABLED and i == 2:
                        add = add.copy()
                        add['snippet'] = 'az ' + add['name']
                        index['-'].append(add)

        parent = ' '.join(parts[0:-1])
        completion = {
            'name': parts[-1],
            'kind': 'command',
            'detail': command.name
        }
        add_command_documentation(completion, command)

        index[parent].append(completion)

        if TWO_SEGMENTS_COMPLETION_ENABLED and len_parts > 1:
            add = completion.copy()
            add['name'] = ' '.join(parts[len_parts - 2:len_parts])
            index[' '.join(parts[0:len_parts - 2])].append(add)
            if NO_AZ_PREFIX_COMPLETION_ENABLED and len_parts == 2:
                add = add.copy()
                add['snippet'] = 'az ' + add['name']
                index['-'].append(add)
    return index
예제 #3
0
def add_command_documentation(completion, command):
    help = get_help(command.name)
    if help:
        short_summary = help.get('short-summary')
        if short_summary:
            completion['documentation'] = short_summary
            long_summary = help.get('long-summary')
            if long_summary:
                completion['documentation'] += '\n\n' + long_summary
            examples = help.get('examples')
            if examples:
                for example in examples:
                    completion['documentation'] += '\n\n' + example[
                        'name'].strip() + '\n' + example['text'].strip()
예제 #4
0
def get_short_summary(subcommand, fallback):
    help = get_help(subcommand)
    if help:
        return help.get('short-summary', fallback)
    return fallback
예제 #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
예제 #6
0
 def test_group_help(self):
     help = get_help(TEST_GROUP)
     self.assertIsNotNone(help)
     self.assertTrue(help.get('short-summary'))