Python and tab completion, better together.

Overview

argcomplete - Bash tab completion for argparse

Tab complete all the things!

Argcomplete provides easy, extensible command line tab completion of arguments for your Python script.

It makes two assumptions:

  • You're using bash as your shell (limited support for zsh, fish, and tcsh is available)
  • You're using argparse to manage your command line arguments/options

Argcomplete is particularly useful if your program has lots of options or subparsers, and if your program can dynamically suggest completions for your argument/option values (for example, if the user is browsing resources over the network).

Installation

pip install argcomplete
activate-global-python-argcomplete

See Activating global completion below for details about the second step (or if it reports an error).

Refresh your bash environment (start a new shell or source /etc/profile).

Synopsis

Python code (e.g. my-awesome-script):

#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argcomplete, argparse
parser = argparse.ArgumentParser()
...
argcomplete.autocomplete(parser)
args = parser.parse_args()
...

Shellcode (only necessary if global completion is not activated - see Global completion below), to be put in e.g. .bashrc:

eval "$(register-python-argcomplete my-awesome-script)"

argcomplete.autocomplete(parser)

This method is the entry point to the module. It must be called after ArgumentParser construction is complete, but before the ArgumentParser.parse_args() method is called. The method looks for an environment variable that the completion hook shellcode sets, and if it's there, collects completions, prints them to the output stream (fd 8 by default), and exits. Otherwise, it returns to the caller immediately.

Side effects

Argcomplete gets completions by running your program. It intercepts the execution flow at the moment argcomplete.autocomplete() is called. After sending completions, it exits using exit_method (os._exit by default). This means if your program has any side effects that happen before argcomplete is called, those side effects will happen every time the user presses <TAB> (although anything your program prints to stdout or stderr will be suppressed). For this reason it's best to construct the argument parser and call argcomplete.autocomplete() as early as possible in your execution flow.

Performance

If the program takes a long time to get to the point where argcomplete.autocomplete() is called, the tab completion process will feel sluggish, and the user may lose confidence in it. So it's also important to minimize the startup time of the program up to that point (for example, by deferring initialization or importing of large modules until after parsing options).

Specifying completers

You can specify custom completion functions for your options and arguments. Two styles are supported: callable and readline-style. Callable completers are simpler. They are called with the following keyword arguments:

  • prefix: The prefix text of the last word before the cursor on the command line. For dynamic completers, this can be used to reduce the work required to generate possible completions.
  • action: The argparse.Action instance that this completer was called for.
  • parser: The argparse.ArgumentParser instance that the action was taken by.
  • parsed_args: The result of argument parsing so far (the argparse.Namespace args object normally returned by ArgumentParser.parse_args()).

Completers should return their completions as a list of strings. An example completer for names of environment variables might look like this:

def EnvironCompleter(**kwargs):
    return os.environ

To specify a completer for an argument or option, set the completer attribute of its associated action. An easy way to do this at definition time is:

from argcomplete.completers import EnvironCompleter

parser = argparse.ArgumentParser()
parser.add_argument("--env-var1").completer = EnvironCompleter
parser.add_argument("--env-var2").completer = EnvironCompleter
argcomplete.autocomplete(parser)

If you specify the choices keyword for an argparse option or argument (and don't specify a completer), it will be used for completions.

A completer that is initialized with a set of all possible choices of values for its action might look like this:

class ChoicesCompleter(object):
    def __init__(self, choices):
        self.choices = choices

    def __call__(self, **kwargs):
        return self.choices

The following two ways to specify a static set of choices are equivalent for completion purposes:

from argcomplete.completers import ChoicesCompleter

parser.add_argument("--protocol", choices=('http', 'https', 'ssh', 'rsync', 'wss'))
parser.add_argument("--proto").completer=ChoicesCompleter(('http', 'https', 'ssh', 'rsync', 'wss'))

Note that if you use the choices=<completions> option, argparse will show all these choices in the --help output by default. To prevent this, set metavar (like parser.add_argument("--protocol", metavar="PROTOCOL", choices=('http', 'https', 'ssh', 'rsync', 'wss'))).

The following script uses parsed_args and Requests to query GitHub for publicly known members of an organization and complete their names, then prints the member description:

#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argcomplete, argparse, requests, pprint

def github_org_members(prefix, parsed_args, **kwargs):
    resource = "https://api.github.com/orgs/{org}/members".format(org=parsed_args.organization)
    return (member['login'] for member in requests.get(resource).json() if member['login'].startswith(prefix))

parser = argparse.ArgumentParser()
parser.add_argument("--organization", help="GitHub organization")
parser.add_argument("--member", help="GitHub member").completer = github_org_members

argcomplete.autocomplete(parser)
args = parser.parse_args()

pprint.pprint(requests.get("https://api.github.com/users/{m}".format(m=args.member)).json())

Try it like this:

./describe_github_user.py --organization heroku --member <TAB>

If you have a useful completer to add to the completer library, send a pull request!

Readline-style completers

The readline module defines a completer protocol in rlcompleter. Readline-style completers are also supported by argcomplete, so you can use the same completer object both in an interactive readline-powered shell and on the bash command line. For example, you can use the readline-style completer provided by IPython to get introspective completions like you would get in the IPython shell:

import IPython
parser.add_argument("--python-name").completer = IPython.core.completer.Completer()

argcomplete.CompletionFinder.rl_complete can also be used to plug in an argparse parser as a readline completer.

Printing warnings in completers

Normal stdout/stderr output is suspended when argcomplete runs. Sometimes, though, when the user presses <TAB>, it's appropriate to print information about why completions generation failed. To do this, use warn:

from argcomplete import warn

def AwesomeWebServiceCompleter(prefix, **kwargs):
    if login_failed:
        warn("Please log in to Awesome Web Service to use autocompletion")
    return completions

Using a custom completion validator

By default, argcomplete validates your completions by checking if they start with the prefix given to the completer. You can override this validation check by supplying the validator keyword to argcomplete.autocomplete():

def my_validator(current_input, keyword_to_check_against):
    # Pass through ALL options even if they don't all start with 'current_input'
    return True

argcomplete.autocomplete(parser, validator=my_validator)

Global completion

In global completion mode, you don't have to register each argcomplete-capable executable separately. Instead, bash will look for the string PYTHON_ARGCOMPLETE_OK in the first 1024 bytes of any executable that it's running completion for, and if it's found, follow the rest of the argcomplete protocol as described above.

Additionally, completion is activated for scripts run as python <script> and python -m <module>. This also works for alternate Python versions (e.g. python3 and pypy), as long as that version of Python has argcomplete installed.

Bash version compatibility

Global completion requires bash support for complete -D, which was introduced in bash 4.2. On OS X or older Linux systems, you will need to update bash to use this feature. Check the version of the running copy of bash with echo $BASH_VERSION. On OS X, install bash via Homebrew (brew install bash), add /usr/local/bin/bash to /etc/shells, and run chsh to change your shell.

Global completion is not currently compatible with zsh.

Note

If you use setuptools/distribute scripts or entry_points directives to package your module, argcomplete will follow the wrapper scripts to their destination and look for PYTHON_ARGCOMPLETE_OK in the destination code.

If you choose not to use global completion, or ship a bash completion module that depends on argcomplete, you must register your script explicitly using eval "$(register-python-argcomplete my-awesome-script)". Standard bash completion registration roules apply: namely, the script name is passed directly to complete, meaning it is only tab completed when invoked exactly as it was registered. In the above example, my-awesome-script must be on the path, and the user must be attempting to complete it by that name. The above line alone would not allow you to complete ./my-awesome-script, or /path/to/my-awesome-script.

Activating global completion

The script activate-global-python-argcomplete will try to install the file bash_completion.d/python-argcomplete (see on GitHub) into an appropriate location on your system (/etc/bash_completion.d/ or ~/.bash_completion.d/). If it fails, but you know the correct location of your bash completion scripts directory, you can specify it with --dest:

activate-global-python-argcomplete --dest=/path/to/bash_completion.d

Otherwise, you can redirect its shellcode output into a file:

activate-global-python-argcomplete --dest=- > file

The file's contents should then be sourced in e.g. ~/.bashrc.

Zsh Support

To activate completions for zsh you need to have bashcompinit enabled in zsh:

autoload -U bashcompinit
bashcompinit

Afterwards you can enable completion for your scripts with register-python-argcomplete:

eval "$(register-python-argcomplete my-awesome-script)"

Tcsh Support

To activate completions for tcsh use:

eval `register-python-argcomplete --shell tcsh my-awesome-script`

The python-argcomplete-tcsh script provides completions for tcsh. The following is an example of the tcsh completion syntax for my-awesome-script emitted by register-python-argcomplete:

complete my-awesome-script 'p@*@`python-argcomplete-tcsh my-awesome-script`@'

Fish Support

To activate completions for fish use:

register-python-argcomplete --shell fish my-awesome-script | source

or create new completion file, e.g:

register-python-argcomplete --shell fish my-awesome-script > ~/.config/fish/completions/my-awesome-script.fish

Completion Description For Fish

By default help string is added as completion description.

docs/fish_help_string.png

You can disable this feature by removing _ARGCOMPLETE_DFS variable, e.g:

register-python-argcomplete --shell fish my-awesome-script | grep -v _ARGCOMPLETE_DFS | .

Git Bash Support

Due to limitations of file descriptor inheritance on Windows, Git Bash not supported out of the box. You can opt in to using temporary files instead of file descriptors for for IPC by setting the environment variable ARGCOMPLETE_USE_TEMPFILES, e.g. by adding export ARGCOMPLETE_USE_TEMPFILES=1 to ~/.bashrc.

For full support, consider using Bash with the Windows Subsystem for Linux (WSL).

External argcomplete script

To register an argcomplete script for an arbitrary name, the --external-argcomplete-script argument of the register-python-argcomplete script can be used:

eval "$(register-python-argcomplete --external-argcomplete-script /path/to/script arbitrary-name)"

This allows, for example, to use the auto completion functionality of argcomplete for an application not written in Python. The command line interface of this program must be additionally implemented in a Python script with argparse and argcomplete and whenever the application is called the registered external argcomplete script is used for auto completion.

This option can also be used in combination with the other supported shells.

Python Support

Argcomplete requires Python 2.7 or 3.5+.

Common Problems

If global completion is not completing your script, bash may have registered a default completion function:

$ complete | grep my-awesome-script
complete -F _minimal my-awesome-script

You can fix this by restarting your shell, or by running complete -r my-awesome-script.

Debugging

Set the _ARC_DEBUG variable in your shell to enable verbose debug output every time argcomplete runs. This will disrupt the command line composition state of your terminal, but make it possible to see the internal state of the completer if it encounters problems.

Acknowledgments

Inspired and informed by the optcomplete module by Martin Blais.

Links

Bugs

Please report bugs, issues, feature requests, etc. on GitHub.

License

Licensed under the terms of the Apache License, Version 2.0.

https://codecov.io/github/kislyuk/argcomplete/coverage.svg?branch=master
Comments
  • Does argcomplete special case = (equals signs)?

    Does argcomplete special case = (equals signs)?

    Does argcomplete special case equals signs, or am I doing this wrong?

    For conda, I want to complete conda install packagename=version, like conda install python=3.3. I am using

    
    # To get tab completion from argcomplete
    class Completer(object):
        @memoize
        def get_items(self):
            return self._get_items()
    
        def __contains__(self, item):
            # This generally isn't all possibilities, and even if it is, we want
            # to give better error messages than argparse
            return True
    
        def __iter__(self):
            return iter(self.get_items())
    
    class Packages(Completer):
        def __init__(self, prefix, parsed_args, **kwargs):
            self.prefix = prefix
            self.parsed_args = parsed_args
    
            self.has_eq = '=' in prefix
    
        def _get_items(self):
            # TODO: Include .tar.bz2 files for local installs.
            from conda.api import get_index
            args = self.parsed_args
            index = get_index(channel_urls=args.channel or (), use_cache=True,
                prepend=not args.override_channels, unknown=args.unknown,
                offline=args.offline)
            if not self.has_eq:
                return [i.rsplit('-', 2)[0] for i in index]
            else:
                return [i.rsplit('-', 2)[0] + '=' + i.rsplit('-', 2)[1] for i in index]
    

    Sorry that the code is a little complicated. I have a lot of completers.

    The idea is that if the package doesn't contain a =, we just return a list of packages (because the common case is to not include any = at the command line, like conda install numpy). If it does, I want to complete to package=version. But this is giving

    $PYTHONPATH=. conda install numpy= # I press TAB here
    numpy\=1.5.1     numpy\=1.6.2     numpy\=1.7.0     numpy\=1.7.0b2   numpy\=1.7.0rc1  numpy\=1.7.1     numpy\=1.8.0     numpy\=1.8.1     numpy\=1.8.2     numpy\=1.9.0     numpy\=1.9.1
    $PYTHONPATH=. conda install numpy=numpy\=1.
    

    (the PYTHONPATH is to make sure I am using the git master of argcomplete to test if this was already fixed)

    How do I get the behavior I want?

    opened by asmeurer 38
  • tests are failing

    tests are failing

    ======================================================================
    ERROR: test_file_completion (test.test.TestArgcomplete)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 252, in test_file_completion
        os.makedirs(os.path.join("abcdefж", "klm"))
      File "/usr/lib64/python2.7/os.py", line 148, in makedirs
        if head and tail and not path.exists(head):
      File "/usr/lib64/python2.7/genericpath.py", line 26, in exists
        os.stat(path)
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u0436' in position 6: ordinal not in range(128)
    
    ======================================================================
    ERROR: test_filescompleter_filetype_integration (test.test.TestArgcomplete)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 267, in test_filescompleter_filetype_integration
        os.makedirs(os.path.join("abcdefж", "klm"))
      File "/usr/lib64/python2.7/os.py", line 148, in makedirs
        if head and tail and not path.exists(head):
      File "/usr/lib64/python2.7/genericpath.py", line 26, in exists
        os.stat(path)
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u0436' in position 6: ordinal not in range(128)
    
    ======================================================================
    ERROR: test_non_ascii (test.test.TestArgcomplete)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 354, in test_non_ascii
        self.assertEqual(set(self.run_completer(make_parser(), cmd)),
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 339, in make_parser
        parser.add_argument(_str("--книга"), choices=[
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/argcomplete/compat.py", line 18, in ensure_bytes
        x = x.encode(encoding)
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-6: ordinal not in range(128)
    
    ======================================================================
    ERROR: test_double_quoted_completion (test.test.TestTcsh)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 809, in test_double_quoted_completion
        self.assertEqual(self.sh.run_command('prog basic "f\t'), 'foo\r\n')
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 914, in run_command
        self.child.expect_exact('###', timeout=1)
      File "/usr/lib/python2.7/site-packages/pexpect/spawnbase.py", line 390, in expect_exact
        return exp.expect_loop(timeout)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 107, in expect_loop
        return self.timeout(e)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 70, in timeout
        raise TIMEOUT(msg)
    TIMEOUT: Timeout exceeded.
    <pexpect.pty_spawn.spawn object at 0x7f38a73602d0>
    command: /usr/bin/tcsh
    args: ['/usr/bin/tcsh']
    buffer (last 100 chars): u'\\#\\#\\ \x08#\r\r\nUnmatched \'"\'.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ '
    before (last 100 chars): u'\\#\\#\\ \x08#\r\r\nUnmatched \'"\'.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ '
    after: <class 'pexpect.exceptions.TIMEOUT'>
    match: None
    match_index: None
    exitstatus: None
    flag_eof: False
    pid: 32286
    child_fd: 23
    closed: False
    timeout: 30
    delimiter: <class 'pexpect.exceptions.EOF'>
    logfile: None
    logfile_read: None
    logfile_send: None
    maxread: 2000
    ignorecase: False
    searchwindowsize: None
    delaybeforesend: 0.05
    delayafterclose: 0.1
    delayafterterminate: 0.1
    searcher: searcher_string:
        0: "###"
    
    ======================================================================
    ERROR: test_quoted_exact (test.test.TestTcsh)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 826, in test_quoted_exact
        self.assertEqual(self.sh.run_command('prog basic "f\t--'), 'foo\r\n')
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 914, in run_command
        self.child.expect_exact('###', timeout=1)
      File "/usr/lib/python2.7/site-packages/pexpect/spawnbase.py", line 390, in expect_exact
        return exp.expect_loop(timeout)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 107, in expect_loop
        return self.timeout(e)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 70, in timeout
        raise TIMEOUT(msg)
    TIMEOUT: Timeout exceeded.
    <pexpect.pty_spawn.spawn object at 0x7f38a7366110>
    command: /usr/bin/tcsh
    args: ['/usr/bin/tcsh']
    buffer (last 100 chars): u'\\#\\ \x08#\\#\r\r\nUnmatched \'"\'.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ '
    before (last 100 chars): u'\\#\\ \x08#\\#\r\r\nUnmatched \'"\'.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ '
    after: <class 'pexpect.exceptions.TIMEOUT'>
    match: None
    match_index: None
    exitstatus: None
    flag_eof: False
    pid: 32423
    child_fd: 28
    closed: False
    timeout: 30
    delimiter: <class 'pexpect.exceptions.EOF'>
    logfile: None
    logfile_read: None
    logfile_send: None
    maxread: 2000
    ignorecase: False
    searchwindowsize: None
    delaybeforesend: 0.05
    delayafterclose: 0.1
    delayafterterminate: 0.1
    searcher: searcher_string:
        0: "###"
    
    ======================================================================
    ERROR: test_single_quoted_completion (test.test.TestTcsh)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 806, in test_single_quoted_completion
        self.assertEqual(self.sh.run_command("prog basic 'f\t"), 'foo\r\n')
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 914, in run_command
        self.child.expect_exact('###', timeout=1)
      File "/usr/lib/python2.7/site-packages/pexpect/spawnbase.py", line 390, in expect_exact
        return exp.expect_loop(timeout)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 107, in expect_loop
        return self.timeout(e)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 70, in timeout
        raise TIMEOUT(msg)
    TIMEOUT: Timeout exceeded.
    <pexpect.pty_spawn.spawn object at 0x7f38a7366810>
    command: /usr/bin/tcsh
    args: ['/usr/bin/tcsh']
    buffer (last 100 chars): u"\\#\\#\\ \x08#\r\r\nUnmatched '''.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ "
    before (last 100 chars): u"\\#\\#\\ \x08#\r\r\nUnmatched '''.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ "
    after: <class 'pexpect.exceptions.TIMEOUT'>
    match: None
    match_index: None
    exitstatus: None
    flag_eof: False
    pid: 32505
    child_fd: 31
    closed: False
    timeout: 30
    delimiter: <class 'pexpect.exceptions.EOF'>
    logfile: None
    logfile_read: None
    logfile_send: None
    maxread: 2000
    ignorecase: False
    searchwindowsize: None
    delaybeforesend: 0.05
    delayafterclose: 0.1
    delayafterterminate: 0.1
    searcher: searcher_string:
        0: "###"
    
    ======================================================================
    ERROR: test_single_quotes_in_double_quotes (test.test.TestTcsh)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 860, in test_single_quotes_in_double_quotes
        self.assertEqual(self.sh.run_command('prog quote "1\t'), "1'1\r\n")
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 914, in run_command
        self.child.expect_exact('###', timeout=1)
      File "/usr/lib/python2.7/site-packages/pexpect/spawnbase.py", line 390, in expect_exact
        return exp.expect_loop(timeout)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 107, in expect_loop
        return self.timeout(e)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 70, in timeout
        raise TIMEOUT(msg)
    TIMEOUT: Timeout exceeded.
    <pexpect.pty_spawn.spawn object at 0x7f38a7366a90>
    command: /usr/bin/tcsh
    args: ['/usr/bin/tcsh']
    buffer (last 100 chars): u'\\#\\#\\ \x08#\r\r\nUnmatched \'"\'.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ '
    before (last 100 chars): u'\\#\\#\\ \x08#\r\r\nUnmatched \'"\'.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ '
    after: <class 'pexpect.exceptions.TIMEOUT'>
    match: None
    match_index: None
    exitstatus: None
    flag_eof: False
    pid: 32532
    child_fd: 32
    closed: False
    timeout: 30
    delimiter: <class 'pexpect.exceptions.EOF'>
    logfile: None
    logfile_read: None
    logfile_send: None
    maxread: 2000
    ignorecase: False
    searchwindowsize: None
    delaybeforesend: 0.05
    delayafterclose: 0.1
    delayafterterminate: 0.1
    searcher: searcher_string:
        0: "###"
    
    ======================================================================
    ERROR: test_single_quotes_in_single_quotes (test.test.TestTcsh)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 865, in test_single_quotes_in_single_quotes
        self.assertEqual(self.sh.run_command("prog quote '1\t"), "1'1\r\n")
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 914, in run_command
        self.child.expect_exact('###', timeout=1)
      File "/usr/lib/python2.7/site-packages/pexpect/spawnbase.py", line 390, in expect_exact
        return exp.expect_loop(timeout)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 107, in expect_loop
        return self.timeout(e)
      File "/usr/lib/python2.7/site-packages/pexpect/expect.py", line 70, in timeout
        raise TIMEOUT(msg)
    TIMEOUT: Timeout exceeded.
    <pexpect.pty_spawn.spawn object at 0x7f38a7366cd0>
    command: /usr/bin/tcsh
    args: ['/usr/bin/tcsh']
    buffer (last 100 chars): u"\\#\\#\\ \x08#\r\r\nUnmatched '''.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ "
    before (last 100 chars): u"\\#\\#\\ \x08#\r\r\nUnmatched '''.\r\n\x1b]0;brain@x1carbon:argcomplete-1.7.0\x07[brain@x1carbon argcomplete-1.7.0]$ "
    after: <class 'pexpect.exceptions.TIMEOUT'>
    match: None
    match_index: None
    exitstatus: None
    flag_eof: False
    pid: 32559
    child_fd: 33
    closed: False
    timeout: 30
    delimiter: <class 'pexpect.exceptions.EOF'>
    logfile: None
    logfile_read: None
    logfile_send: None
    maxread: 2000
    ignorecase: False
    searchwindowsize: None
    delaybeforesend: 0.05
    delayafterclose: 0.1
    delayafterterminate: 0.1
    searcher: searcher_string:
        0: "###"
    
    ======================================================================
    FAIL: test_continuation (test.test.TestBash)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 821, in test_continuation
        self.assertEqual(self.sh.run_command('prog cont f\t--'), 'foo=--\r\n')
    AssertionError: u'f--\r\n' != u'foo=--\r\n'
    - f--
    + foo=--
    ?  +++
    
    
    ======================================================================
    FAIL: test_partial_completion (test.test.TestTcsh)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 803, in test_partial_completion
        self.assertEqual(self.sh.run_command('prog basic b\tr'), 'bar\r\n')
    AssertionError: u"usage: prog basic [-h] {foo,bar,baz}\r\nprog basic: error: argument arg: inval [truncated]... != u'bar\r\n'
    + bar
    - usage: prog basic [-h] {foo,bar,baz}
    - prog basic: error: argument arg: invalid choice: 'build/r' (choose from 'foo', 'bar', 'baz')
    
    
    ======================================================================
    FAIL: test_simple_completion (test.test.TestTcsh)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/brain/git/fedora/python-argcomplete/argcomplete-1.7.0/test/test.py", line 800, in test_simple_completion
        self.assertEqual(self.sh.run_command('prog basic f\t'), 'foo\r\n')
    AssertionError: u"usage: prog basic [-h] {foo,bar,baz}\r\nprog basic: error: argument arg: inval [truncated]... != u'foo\r\n'
    + foo
    - usage: prog basic [-h] {foo,bar,baz}
    - prog basic: error: argument arg: invalid choice: 'f' (choose from 'foo', 'bar', 'baz')
    
    
    ----------------------------------------------------------------------
    
    opened by ignatenkobrain 27
  • Disable duplicated flags

    Disable duplicated flags

    The autocompletion continue to offer flags that already in the command, so I added a check from parsed_args. Not sure if it's the best way, will be glad to get more ideas on it.

    opened by zacharya19 21
  • Add REPL subparsers support

    Add REPL subparsers support

    This commit add subparsers support for REPL with readline module. And multi level supparsers also supported.

    tested pass under python2.7 and python3.4

    opened by lisongmin 17
  • Not work with unicode as input

    Not work with unicode as input

    Example code as below:

    def MyCompleter(prefix, **kwargs):
        results = ['aaa', 'bbb']
        return (c for c in results if c.startswith(prefix))
    
    if __name__ == '__main__':
        import argparse
        import argcomplete
    
        parser = argparse.ArgumentParser()
        subparsers = parser.add_subparsers(dest='command')
        create_parser = subparsers.add_parser('new')
        create_parser.add_argument('title')
        create_parser.add_argument('category').completer = MyCompleter
        argcomplete.autocomplete(parser)
        args = parser.parse_args()
        print args
    

    It works as expect when all the inputs are normal characters (assume above code are wrapped in command demo):

    $ ./demo.py new 'hi'
    --help  -h      aaa     bbb  
    

    When the input contains unicode, the auto completer is not working:

    $ ./demo.py new '你好'
    

    So, is this a bug or something related to the shell? I'm using zsh and argcomplete (Version: 1.9.2).

    opened by cuyu 16
  • Not working when installed in develop mode on OSX

    Not working when installed in develop mode on OSX

    After running python setup.py develop, I don't get auto-completion because the script can't find the file correctly.

    The problem seems to be that this line resolves incorrectly causing an IOError. get_metadata is resolving to files within the .egg-info directory. For example if my working directory is /Users/dcosson/my-package and I ran python setup.py develop from there, it resolves to /Users/dcosson/my-package/my_package.egg-info/scripts/myscript

    I'm not too familiar with setuptools, so it's possible that I have misconfured something and the scripts should be getting copied into the egg-info folder.

    I'm on OSX 10.9 and python 2.7.5

    opened by dcosson 16
  • Add support for script completion on Windows via Git Bash

    Add support for script completion on Windows via Git Bash

    • How does this impact WSL users?
      • Is the OS environment variable set there?
      • Should we make this feature opt-in instead?
    • Global completion of python/py does not work
      • Should be fixable with a flag to tell the script not to launch via py.exe
    • Completion of pip-generated script wrappers does not work
      • Much harder; existing logic parses the wrapper to find the correct interpreter to then find the script to check for the argcomplete marker
      • Alternatively we could just guess an interpreter and hope for the best
    opened by evanunderscore 15
  • check_module: Don't crash, exit with error instead

    check_module: Don't crash, exit with error instead

    This should avoid https://bugzilla.redhat.com/show_bug.cgi?id=1689507

    I still don't know what makes this happen, or if a regular Exception is better here than AttributeError, but this is not the first time this was reported by Fedora automatic bug report.

    See for example https://bugzilla.redhat.com/show_bug.cgi?id=1601763

    I've asked the user what were they actually doing.

    https://retrace.fedoraproject.org/faf/reports/2267286/ gives hint that this happens quite often.

    opened by hroncok 14
  • Anyone got this working with bash from git for windows?

    Anyone got this working with bash from git for windows?

    After some debugging, it seems it has issues opening fd 8 on my computer : "Unable to open fd 8 for writing, quitting"

    $ git --version git version 2.10.0.windows.1

    $ bash --version GNU bash, version 4.3.46(2)-release (x86_64-pc-msys)

    opened by krist10an 14
  • Activating argcomplete globally in Debian breaks other types of completion

    Activating argcomplete globally in Debian breaks other types of completion

    When I install package python-argcomplete in Debian and then run activate-global-python-argcomplete as root, other types of completion stop working. I don't know if this issue occurs with packages for other distributions.

    For example, activating argcomplete globally prevents completion of package names for apt-get when used like sudo apt-get install <Tab>, or completion for mplayer when only multimedia files are suggested as completion options. Instead of those custom completion types a common file list completion is being offered. At the same time Python completion with argcomplete is working as intended.

    python-argcomplete package version is 0.3.3-1

    EDIT:

    The same occurs when installing the package with pip and then activating global completion.

    opened by Cookson 14
  • Support for python -m module

    Support for python -m module

    Is it possible for argcomplete to work for modules run as python -m module? This is necessary to do once you start organizing your code in a module (rather than just a single script) to make relative imports work correctly, but I don't really want to make a separate entry point script if I don't have to.

    I am using global completion and it isn't working, at least not out of the box.

    opened by asmeurer 13
  • Integration with traitlets

    Integration with traitlets

    As noted in the prior issue, I've been looking into integrating argcomplete into https://github.com/ipython/traitlets/pull/811.

    Just wanted to jot down a couple of things that were done there, which perhaps could be upstreamed to argcomplete:

    • Dynamically adding options: traitlets supports reading arbitrary arguments of the form --Class.trait=value. It does this lazily (since there can be a lot of classes, some classes may not known at init time, etc). To handle this, I overrode _get_options_completions() to add additional "--Class." completions, and once there was only one class that could be completed, then overrode _get_completions to dynamically add the corresponding "--Class.trait" arguments to the ArgumentParser instance. (I didn't want "--Class." itself to be directly completable, because argcomplete would then automatically add a space afterwards.) This was mostly fine but perhaps could be nice to have dedicated entry points to modify these behaviors.
    • Parsing input words and changing which word to start from: for traitlets subcommand handling, its again evaluated lazily from the contents of sys.argv and done independently from argparse. Since argcomplete doesn't pass along any argv, I copied over argcomplete's logic to parse comp_words and passed that along as argv. After traitlets was able to then determine what subcommand was being used, I had to "tell" argcomplete to skip over the subcommand token, which was done a bit hackily via incrementing $_ARGCOMPLETE. I know this is pretty custom to traitlets, but it might be nice to refactor out some of the comp_words parsing logic to helper methods so that can be used by other scripts which do some manipulation of sys.argv prior to calling argparse, and have a path for scripts to modify the current state of comp_words.
    opened by azjps 0
  • Allow debug_stream to be set optionally

    Allow debug_stream to be set optionally

    First, a thank you for the project; I've been looking into integrating argcomplete into ipython/traitlets#811 so that shell completion works "out-of-the-box" for IPython, Jupyter, and everything else in the ecosystem.

    Currently in CompletionFinder.__init__(), the following is done:

            global debug_stream
            try:
                debug_stream = os.fdopen(9, "w")
            except Exception:
                debug_stream = sys.stderr
            debug()
    

    Could this behavior be made optional? I think it could either be extracted out into a separate helper method that can be overridden, or perhaps be controlled by either an additional argument (similar to output_stream=) or environment variable. Feel free to let me know which (or both) is preferable, and I can open a PR.

    The problem is that this file descriptor could be used elsewhere, for example by pytest, leading to obscure failures. You can find a MRE in https://github.com/ipython/traitlets/pull/815/commits/2654e3feb9e152148a519c47a73200b9bd683133. I was able to get around this for now by mock-ing os.fdopen https://github.com/ipython/traitlets/pull/811/commits/0deadccc0adc0d9a91a15fe8790f2b7c5c8831a9, but I'd prefer not to have to introduce mocking if possible.

    I think this was also identified as one of two issues in #365 but was closed since the other half was resolved.

    opened by azjps 0
  • Support PowerShell

    Support PowerShell

    In https://github.com/kislyuk/argcomplete/pull/307, argcomplete adds the env ARGCOMPLETE_USE_TEMPFILES to redirect its output to file and supports Git Bash. We can use the same way to support powershell.

    I've used it in https://github.com/Azure/azure-cli/pull/24752, and it works.

    opened by bebound 0
  • How can I get rid of argcomplete?

    How can I get rid of argcomplete?

    Hello, I have a strange problem after upgrading to Linux Mint 21: my bash windows show argcomplete error messages about something which cannot be parsed (but not saying what it is).

    StrangeTerminalError

    I have already done multiple times

    pip3 uninstall argcomplete

    and by now the feedback is:

    pip3 uninstall argcomplete WARNING: Skipping argcomplete as it is not installed.

    Yet, when I do 'locate -iA argcomplete', I still see a lot of files in /usr/local/lib, /usr/local/bin, /home/USER/.local/lib and /etc with argcomplete in the file names. Do I have to delete them all manually? Would that be safe? I better ask before taking action.

    I never had this problem under Linux Mint 20.3. It only occurs now, but I have no clue why, and what has changed. I would be very grateful for assistance. Thank you in advance!

    opened by papioara 0
  • Drop python 3.6?

    Drop python 3.6?

    In regards to python 3.6 mentioned throughout the codebase (README, CI)

    • Released 5 years and 10 months ago (22 Dec 2016)
    • Support Ended 10 months ago (23 Dec 2021)

    Source: https://endoflife.date/python

    Proposal:

    • Remove python 3.6
    • Remove any python 2 / < 3.7 compat related code for cleanliness
    opened by tony 0
Releases(v2.0.0)
Owner
Andrey Kislyuk
Software engineer and bioinformatician
Andrey Kislyuk
Rich is a Python library for rich text and beautiful formatting in the terminal.

Rich 中文 readme • lengua española readme • Läs på svenska Rich is a Python library for rich text and beautiful formatting in the terminal. The Rich API

Will McGugan 41.4k Jan 2, 2023
A cross platform package to do curses-like operations, plus higher level APIs and widgets to create text UIs and ASCII art animations

ASCIIMATICS Asciimatics is a package to help people create full-screen text UIs (from interactive forms to ASCII animations) on any platform. It is li

null 3.2k Jan 9, 2023
A simple terminal Christmas tree made with Python

Python Christmas Tree A simple CLI Christmas tree made with Python Installation Just clone the repository and run $ python terminal_tree.py More opti

Francisco B. 64 Dec 27, 2022
Python composable command line interface toolkit

$ click_ Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It's the "Comm

The Pallets Projects 13.3k Dec 31, 2022
Library for building powerful interactive command line applications in Python

Python Prompt Toolkit prompt_toolkit is a library for building powerful interactive command line applications in Python. Read the documentation on rea

prompt-toolkit 8.1k Dec 30, 2022
Simple cross-platform colored terminal text in Python

Colorama Makes ANSI escape character sequences (for producing colored terminal text and cursor positioning) work under MS Windows. PyPI for releases |

Jonathan Hartley 3k Jan 1, 2023
emoji terminal output for Python

Emoji Emoji for Python. This project was inspired by kyokomi. Example The entire set of Emoji codes as defined by the unicode consortium is supported

Taehoon Kim 1.6k Jan 2, 2023
Python library that measures the width of unicode strings rendered to a terminal

Introduction This library is mainly for CLI programs that carefully produce output for Terminals, or make pretend to be an emulator. Problem Statement

Jeff Quast 305 Dec 25, 2022
A thin, practical wrapper around terminal capabilities in Python

Blessings Coding with Blessings looks like this... from blessings import Terminal t = Terminal() print(t.bold('Hi there!')) print(t.bold_red_on_brig

Erik Rose 1.4k Jan 7, 2023
Typer, build great CLIs. Easy to code. Based on Python type hints.

Typer, build great CLIs. Easy to code. Based on Python type hints. Documentation: https://typer.tiangolo.com Source Code: https://github.com/tiangolo/

Sebastián Ramírez 10.1k Jan 2, 2023
Python Command-line Application Tools

Clint: Python Command-line Interface Tools Clint is a module filled with a set of awesome tools for developing commandline applications. C ommand L in

Kenneth Reitz Archive 82 Dec 28, 2022
Textual is a TUI (Text User Interface) framework for Python using Rich as a renderer.

Textual is a TUI (Text User Interface) framework for Python using Rich as a renderer. The end goal is to be able to rapidly create rich termin

Will McGugan 17k Jan 2, 2023
Cement is an advanced Application Framework for Python, with a primary focus on CLI

Cement Framework Cement is an advanced Application Framework for Python, with a primary focus on Command Line Interfaces (CLI). Its goal is to introdu

Data Folk Labs, LLC 1.1k Dec 31, 2022
Corgy allows you to create a command line interface in Python, without worrying about boilerplate code

corgy Elegant command line parsing for Python. Corgy allows you to create a command line interface in Python, without worrying about boilerplate code.

Jayanth Koushik 7 Nov 17, 2022
prompt_toolkit is a library for building powerful interactive command line applications in Python.

Python Prompt Toolkit prompt_toolkit is a library for building powerful interactive command line applications in Python. Read the documentation on rea

prompt-toolkit 8.1k Jan 4, 2023
Terminalcmd - a Python library which can help you to make your own terminal program with high-intellegence instruments

Terminalcmd - a Python library which can help you to make your own terminal program with high-intellegence instruments, that will make your code clear and readable.

Dallas 0 Jun 19, 2022
A drop-in replacement for argparse that allows options to also be set via config files and/or environment variables.

ConfigArgParse Overview Applications with more than a handful of user-settable options are best configured through a combination of command line args,

null 634 Dec 22, 2022
Cleo allows you to create beautiful and testable command-line interfaces.

Cleo Create beautiful and testable command-line interfaces. Cleo is mostly a higher level wrapper for CliKit, so a lot of the components and utilities

Sébastien Eustace 984 Jan 2, 2023
A module for parsing and processing commands.

cmdtools A module for parsing and processing commands. Installation pip install --upgrade cmdtools-py install latest commit from GitHub pip install g

null 1 Aug 14, 2022