def process_suggestions()

in modular_cli/modular_cli_autocomplete/modular_cli_complete.py [0:0]


def process_suggestions(request, meta):
    suggestions = {}
    completed_request = False
    is_group = False

    for token in request:
        if meta.get(token):
            meta = meta.get(token).get('body')
            completed_request = True
            is_group = True
        elif token == request[-1] and not token.startswith('--'):
            completed_request = False
            is_group = False
        elif token.startswith('--') and 'parameters' in meta:
            completed_request = True
            is_group = False
            break

    if 'parameters' in meta:
        is_group = False

    if is_group:
        for item in meta:
            suggestions[item] = None
        format_response(suggestions)

    if completed_request:
        if meta.get('parameters', None):
            parameters_list = meta.get('parameters')
            index_of_first_param = 0
            for index, token in enumerate(request):
                if '--' in token:
                    index_of_first_param = index
                    break

            no_params_request = request[:index_of_first_param]
            params_request = [param for param in
                              list(set(request) - set(no_params_request))
                              if re.match(r'^--[a-z]', param)]

            for parameter in parameters_list:
                param = parameter.get('name')
                param_description = parameter.get('description', None)
                suggestions[
                    f'--{param}'] = param_description if param_description \
                    else NO_DESCRIPTION

            updated_suggestions = {}
            for specified_param in params_request:
                if specified_param not in suggestions.keys():
                    for suggested_param in suggestions.keys():
                        if suggested_param.startswith(specified_param) \
                                and not specified_param == suggested_param:
                            updated_suggestions[
                                suggested_param] = suggestions.get(
                                suggested_param)
                else:
                    del suggestions[specified_param]

            if updated_suggestions:
                suggestions = updated_suggestions
        else:
            suggestions = []

        format_response(suggestions)

    process_start_suggestion(request[-1], meta)