Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for user autocompletion functions #34

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions click_completion/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

from __future__ import print_function, absolute_import

import copy
import os
import re
import shlex
import subprocess

import click
from click import Option, Argument, MultiCommand, echo
from click._bashcomplete import get_user_autocompletions, start_of_option, WORDBREAK, is_incomplete_option, is_incomplete_argument
from enum import Enum

from click_completion.lib import resolve_ctx, split_args, single_quote, double_quote, get_auto_shell
Expand Down Expand Up @@ -83,19 +85,43 @@ def get_choices(cli, prog_name, args, incomplete):
A list of completion results. The first element of each tuple is actually the argument to complete, the second
element is an help string for this argument.
"""
# Copy section from click._bashcomplete.get_choices()
all_args = copy.deepcopy(args)
# In newer versions of bash long opts with '='s are partitioned, but it's easier to parse
# without the '='
if start_of_option(incomplete) and WORDBREAK in incomplete:
partition_incomplete = incomplete.partition(WORDBREAK)
all_args.append(partition_incomplete[0])
incomplete = partition_incomplete[2]
elif incomplete == WORDBREAK:
incomplete = ''
# End of copy section from click._bashcomplete.get_choices()

ctx = resolve_ctx(cli, prog_name, args)
if ctx is None:
return

# Copy section from click._bashcomplete.get_choices()
# completion for option values from user supplied values
for param in ctx.command.params:
if is_incomplete_option(all_args, param):
return get_user_autocompletions(ctx, all_args, incomplete, param)
# completion for argument values from user supplied values
for param in ctx.command.params:
if is_incomplete_argument(ctx.params, param):
return get_user_autocompletions(ctx, all_args, incomplete, param)
# End of section from click._bashcomplete.get_choices()

optctx = None
if args:
if all_args:
options = [param
for param in ctx.command.get_params(ctx)
if isinstance(param, Option)]
arguments = [param
for param in ctx.command.get_params(ctx)
if isinstance(param, Argument)]
for param in options:
if not param.is_flag and args[-1] in param.opts + param.secondary_opts:
if not param.is_flag and all_args[-1] in param.opts + param.secondary_opts:
optctx = param
if optctx is None:
for param in arguments:
Expand Down Expand Up @@ -130,8 +156,7 @@ def get_choices(cli, prog_name, args, incomplete):
if match(name, incomplete):
choices.append((name, ctx.command.get_command_short_help(ctx, name)))

for item, help in choices:
yield (item, help)
return choices


def do_bash_complete(cli, prog_name):
Expand Down