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 '[email protected]*@`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
Issues
  • 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;[email protected]:argcomplete-1.7.0\x07[[email protected] argcomplete-1.7.0]$ '
    before (last 100 chars): u'\\#\\#\\ \x08#\r\r\nUnmatched \'"\'.\r\n\x1b]0;[email protected]:argcomplete-1.7.0\x07[[email protected] 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;[email protected]:argcomplete-1.7.0\x07[[email protected] argcomplete-1.7.0]$ '
    before (last 100 chars): u'\\#\\ \x08#\\#\r\r\nUnmatched \'"\'.\r\n\x1b]0;[email protected]:argcomplete-1.7.0\x07[[email protected] 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;[email protected]:argcomplete-1.7.0\x07[[email protected] argcomplete-1.7.0]$ "
    before (last 100 chars): u"\\#\\#\\ \x08#\r\r\nUnmatched '''.\r\n\x1b]0;[email protected]:argcomplete-1.7.0\x07[[email protected] 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;[email protected]:argcomplete-1.7.0\x07[[email protected] argcomplete-1.7.0]$ '
    before (last 100 chars): u'\\#\\#\\ \x08#\r\r\nUnmatched \'"\'.\r\n\x1b]0;[email protected]:argcomplete-1.7.0\x07[[email protected] 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;[email protected]:argcomplete-1.7.0\x07[[email protected] argcomplete-1.7.0]$ "
    before (last 100 chars): u"\\#\\#\\ \x08#\r\r\nUnmatched '''.\r\n\x1b]0;[email protected]:argcomplete-1.7.0\x07[[email protected] 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 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
  • 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
  • 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
  • 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
  • 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
  • Add tests for tcsh

    Add tests for tcsh

    opened by evanunderscore 14
  • Add new option to activate-global-python-argcomplete that returns location of bash completion script

    Add new option to activate-global-python-argcomplete that returns location of bash completion script

    It would be nice for activate-global-python-argcomplete to have an option that returns the location of the bash completion script. This would help in situations where we can't/don't want to use activate-global-python-argcomplete to make a copy of the completion script and want to source the script directly from the package.

    opened by xcl-gh 0
  • Migrate to pyproject.toml and flit

    Migrate to pyproject.toml and flit

    Once that is done, add build to the build dependencies in pyproject.toml.

    opened by kislyuk 0
  • Update patched versions of shlex and argparse

    Update patched versions of shlex and argparse

    For #361 and v2.0, we need to update the code in these modules from the original 2.7 version to the most recent released version in https://github.com/python/cpython/tree/main/Lib that passes our CI.

    opened by kislyuk 0
  • Support of path completion in fish #327

    Support of path completion in fish #327

    First version of absolute path completion for fish. #327 fix

    opened by volkov 4
  • argcomplete.egg-info/requires.txt | make issue with customized python

    argcomplete.egg-info/requires.txt | make issue with customized python

    Hi Team,

    We been using python module argcomplete - 1.12.3 that required for module"yq".

    The problem is we been using customized python [ that is for our environment pupose]

    Ex;

    python -- would point python 2.x python3 -- would point to python 3.x

    the problem is when we try to build and use the module argcomplete with python3 [python 3.8.1] we facing below issue

    [[email protected] argcomplete-1.12.3-py3.7.egg-info]$ yq Traceback (most recent call last): File "/opt/python3/x86_64/3.7.5/lib/python3.7/site-packages/pkg_resources/init.py", line 583, in _build_master ws.require(requires) File "/opt/python3/x86_64/3.7.5/lib/python3.7/site-packages/pkg_resources/init.py", line 900, in require needed = self.resolve(parse_requirements(requirements)) File "/opt/python3/x86_64/3.7.5/lib/python3.7/site-packages/pkg_resources/init.py", line 791, in resolve raise VersionConflict(dist, req).with_context(dependent_req) pkg_resources.ContextualVersionConflict: (importlib-metadata 0.0.0 (/opt/python3-importlib-metadata/noarch/4.5.0.p375-1/lib/python3.7/site-packages), Requirement.parse('importlib-metadata<5,>=0.23'), {'argcomplete'})

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "/opt/python3-yq/noarch/2.12.0.p375-1/bin/yq", line 6, in from pkg_resources import load_entry_point File "/opt/python3/x86_64/3.7.5/lib/python3.7/site-packages/pkg_resources/init.py", line 3250, in @_call_aside File "/opt/python3/x86_64/3.7.5/lib/python3.7/site-packages/pkg_resources/init.py", line 3234, in _call_aside f(*args, **kwargs) File "/opt/python3/x86_64/3.7.5/lib/python3.7/site-packages/pkg_resources/init.py", line 3263, in _initialize_master_working_set working_set = WorkingSet._build_master() File "/opt/python3/x86_64/3.7.5/lib/python3.7/site-packages/pkg_resources/init.py", line 585, in _build_master return cls._build_from_requirements(requires) File "/opt/python3/x86_64/3.7.5/lib/python3.7/site-packages/pkg_resources/init.py", line 598, in _build_from_requirements dists = ws.resolve(reqs, Environment()) File "/opt/python3/x86_64/3.7.5/lib/python3.7/site-packages/pkg_resources/init.py", line 786, in resolve raise DistributionNotFound(req, requirers) pkg_resources.DistributionNotFound: The 'importlib-metadata<5,>=0.23' distribution was not found and is required by argcomplete

    importlib_metadata already installed in our environment that pointed with PYTHONPATH env variable

    on further investigation we understand that this is because of argcomplete/requires.txt is in below python specific format

    [[email protected] argcomplete-1.12.3-py3.7.egg-info]$ cat requires.txt

    [:python_version == "2.7"] importlib-metadata<5,>=0.23

    [:python_version == "3.5"] importlib-metadata<5,>=0.23

    [:python_version == "3.6"] importlib-metadata<5,>=0.23

    [:python_version == "3.7"] importlib-metadata<5,>=0.23

    Since our version of python is mentioned as python3.

    when we manually changed to requires.txt to be [:python3_version == 3.7] and no issue.

    So please let us know why there is version specific information for argcomplete, how can we avoid this further.

    also we been using 500+ python modules in this same way and no issue so far.

    issue only observed on argcomplete only.

    would be great if anyone help/suggest here.

    Thank you!!!

    opened by AbinayaSandhiyaM 2
  • 1.12.3: sphinx 4.0.2 warnings

    1.12.3: sphinx 4.0.2 warnings

    + /usr/bin/python3 setup.py build_sphinx -b man --build-dir build/sphinx
    running build_sphinx
    Running Sphinx v4.0.2
    making output directory... done
    building [mo]: targets for 0 po files that are out of date
    building [man]: all manpages
    updating environment: [new config] 1 added, 0 changed, 0 removed
    reading sources... [100%] index
    /home/tkloczko/rpmbuild/BUILD/argcomplete-1.12.3/argcomplete/__init__.py:docstring of argcomplete.warn:3: WARNING: Duplicate explicit target name: "rlcompleter".
    README.rst:161: WARNING: Duplicate target name, cannot be used as a unique reference: "rlcompleter".
    README.rst:290: WARNING: image file not readable: docs/fish_help_string.png
    looking for now-outdated files... none found
    pickling environment... done
    checking consistency... done
    writing... python-argcomplete.3 { } done
    build succeeded, 3 warnings.
    
    opened by kloczek 0
  • How to complete user-defined git subcommands?

    How to complete user-defined git subcommands?

    git and some other tools support user-defined subcommands by having git-some-subcommand in PATH. Running git some-subcommand runs the custom subcommand via git binary. I can't figure out how to make argcomplete support completion on that subcommand alone. Obviously it works for git-some-subcommand directly, but how to enable it via git while preserving regular git completion?

    opened by jnikula 2
  • Global completion mode always set to `_minimal`

    Global completion mode always set to `_minimal`

    When I use the global completion mode, completion does not work. Pressing tab with no arguments it returns a file list of the current directory Pressing tab with a - or -- seems to return base python completion.

    But when I register a script directly, it works fine. I seem to have run into the issue mentioned in the docs.

    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.

    Global mode (doesn't work):

    $ complete | grep py
    ...
    complete -F _minimal ./test.py
    

    Explicit eval "$(register-python-argcomplete ./test.py)" (works):

    $ complete | grep py
    ...
    complete -o bashdefault -o default -o nospace -F _python_argcomplete ./test.py
    

    The advice (added in #198) says when this happens try removing the completion with complete -r ./test.py. But it's not clear how this solves the problem - Trying this again in the same or a new shell presents the same issue for me. Did I miss something?

    I also tried specifying the full path to ./test.py, which doesn't make a difference.

    test.py is a pretty minimal example.
    #!/usr/bin/env python3
    # PYTHON_ARGCOMPLETE_OK
    import argparse
    import argcomplete
    from pathlib import Path
    
    if __name__ == "__main__":
    	parser = argparse.ArgumentParser()
    	parser.add_argument("-d", "--directory", type=Path)
    	argcomplete.autocomplete(parser)
    
    	args = parser.parse_args()
    
    	print(args)
    
    opened by celynw 2
  • Show only one of multiple names/flags

    Show only one of multiple names/flags

    I am not sure if I missed it somewhere in the docs or issues, but I was wondering if it is possible to show only one (e.g. the first) of multiple names for an argument.

    Let's say I do:

    run.add_argument("-n", "--dryrun")
    

    and then I use argcomplete on this:

    $ my_tool [tab-tab]
    -n --dryrun
    

    I want only one of those two to show, not both. Is this somehow supported by argcomplete?

    p.s. great tool :trophy:

    opened by Maarten-vd-Sande 2
  • autocomplete in csh not working

    autocomplete in csh not working

    I tried to register my program with tcsh and it didn't work, after following the direction autocomplete works on zsh, bash but not tcsh in my Mac system

    (buildtest) [DOE-7086392:~/Documents/buildtest] siddiq90% eval `register-python-argcomplete --shell tcsh buildtest`
    (buildtest) [DOE-7086392:~/Documents/buildtest] siddiq90% buildtest <TAB>
    (buildtest) [DOE-7086392:~/Documents/buildtest] siddiq90% echo $SHELL
    /bin/csh
    (buildtest) [DOE-7086392:~/Documents/buildtest] siddiq90% $SHELL --version
    tcsh 6.21.00 (Astron) 2019-05-08 (x86_64-apple-darwin) options wide,nls,dl,bye,al,kan,sm,rh,color,filec
    

    The version of arcomplete is the following

    (buildtest) [DOE-7086392:~/Documents/buildtest] siddiq90% pip list | grep argcomplete
    argcomplete                   1.12.2
    (buildtest) [DOE-7086392:~/Documents/buildtest] siddiq90% python --version
    Python 3.7.3
    

    Any idea whats the issue?

    opened by shahzebsiddiqui 5
Releases(v1.12.3)
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 31.1k Nov 23, 2021
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 2.9k Nov 24, 2021
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. 34 Oct 6, 2021
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 11.6k Nov 24, 2021
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 7.4k Dec 2, 2021
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 2.6k Dec 2, 2021
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.4k Nov 30, 2021
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 254 Nov 20, 2021
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.3k Nov 22, 2021
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 6.7k Dec 1, 2021
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 61 Nov 24, 2021
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 6.2k Nov 27, 2021
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 Nov 25, 2021
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 4 Nov 21, 2021
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 7.4k Dec 2, 2021
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 554 Nov 17, 2021
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 776 Nov 26, 2021
A script that will warn you, by opening a new browser tab, when there are new content in your favourite websites.

web check A script that will warn you, by opening a new browser tab, when there are new content in your favourite websites. What it does The script wi

Jaime Álvarez 49 Nov 22, 2021
MultiPy lets you conveniently keep track of your python scripts for personal use or showcase by loading and grouping them into categories. It allows you to either run each script individually or together with just one click.

MultiPy About MultiPy is a graphical user interface built using Dear PyGui Python GUI Framework that lets you conveniently keep track of your python s

null 49 Oct 31, 2021
PyPDF2 is a pure-python PDF library capable of splitting, merging together, cropping, and transforming the pages of PDF files.

PyPDF2 is a pure-python PDF library capable of splitting, merging together, cropping, and transforming the pages of PDF files. It can also add custom data, viewing options, and passwords to PDF files. It can retrieve text and metadata from PDFs as well as merge entire files together.

Matthew Stamy 3.9k Nov 22, 2021
Tie together `drf-spectacular` and `djangorestframework-dataclasses` for easy-to-use apis and openapi schemas.

Speccify Tie together drf-spectacular and djangorestframework-dataclasses for easy-to-use apis and openapi schemas. Usage @dataclass class MyQ

Lyst 4 Jul 19, 2021
Secret santa is a fun and easy way to get together with your friends and/or family with a gift for them.

Secret Santa What is Secret Santa? Secret santa is a fun and easy way to get together with your friends and/or family with a gift for them. The idea i

null 2 Nov 7, 2021
Fusion 360 Add-in that creates a pair of toothed curves that can be used to split a body and create two pieces that slide and lock together.

Fusion-360-Add-In-PuzzleSpline Fusion 360 Add-in that creates a pair of toothed curves that can be used to split a body and create two pieces that sli

Michiel van Wessem 1 Nov 15, 2021
AutoGiphyMovie lets you search giphy for gifs, converts them to videos, attach a soundtrack and stitches it all together into a movie!

AutoGiphyMovie lets you search giphy for gifs, converts them to videos, attach a soundtrack and stitches it all together into a movie!

Satya Mohapatra 16 Sep 10, 2021
Histocartography is a framework bringing together AI and Digital Pathology

Documentation | Paper Welcome to the histocartography repository! histocartography is a python-based library designed to facilitate the development of

null 77 Nov 30, 2021
Source code for "Pack Together: Entity and Relation Extraction with Levitated Marker"

PL-Marker Source code for Pack Together: Entity and Relation Extraction with Levitated Marker. Quick links Overview Setup Install Dependencies Data Pr

THUNLP 29 Dec 2, 2021
MosaicML Composer contains a library of methods, and ways to compose them together for more efficient ML training

MosaicML Composer MosaicML Composer contains a library of methods, and ways to compose them together for more efficient ML training. We aim to ease th

MosaicML 142 Nov 23, 2021
Bringing together django, django rest framework, and htmx

This is Just an Idea There is no code, this README just represents an idea for a minimal library that, as of now, does not exist. django-htmx-rest A l

Jack DeVries 1 Oct 29, 2021
Explore different way to mix speech model(wav2vec2, hubert) and nlp model(BART,T5,GPT) together

SpeechMix Explore different way to mix speech model(wav2vec2, hubert) and nlp model(BART,T5,GPT) together. Introduction For the same input: from datas

Eric Lam 6 Nov 17, 2021