-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
register-python-argcomplete
executable file
·84 lines (66 loc) · 2.32 KB
/
register-python-argcomplete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
# Copyright 2012-2013, Andrey Kislyuk and argcomplete contributors.
# Licensed under the Apache License. See https://github.com/kislyuk/argcomplete for more info.
'''
Register a Python executable for use with the argcomplete module.
To perform the registration, source the output of this script in your bash shell (quote the output to avoid interpolation).
Example:
$ eval "$(register-python-argcomplete my-favorite-script.py)"
For Tcsh
$ eval `register-python-argcomplete --shell tcsh my-favorite-script.py`
'''
import sys
import argparse
shellcode = '''
_python_argcomplete() {
local IFS='\013'
COMPREPLY=( $(IFS="$IFS" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
COMP_TYPE="$COMP_TYPE" \
_ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" \
_ARGCOMPLETE=1 \
"$1" 8>&1 9>&2 1>/dev/null 2>/dev/null) )
if [[ $? != 0 ]]; then
unset COMPREPLY
elif [[ "$COMPREPLY" =~ [=/:]$ ]]; then
compopt -o nospace
fi
}
complete %(complete_opts)s -F _python_argcomplete "%(executable)s"
'''
tcshcode = '''\
complete "%(executable)s" 'p@*@`python-argcomplete-tcsh "%(executable)s"`@'
'''
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'--no-defaults',
dest='use_defaults', action='store_false', default=True,
help='When no matches are generated, do not fallback to readline\'s default completion')
parser.add_argument(
'--complete-arguments',
nargs=argparse.REMAINDER,
help='arguments to call complete with; use of this option discards default options')
parser.add_argument(
'-s', '--shell',
choices=('bash', 'tcsh'), default='bash',
help='output code for the specified shell')
parser.add_argument("executable")
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
if args.complete_arguments is None:
complete_options = '-o default' if args.use_defaults else '-o nospace'
else:
complete_options = " ".join(args.complete_arguments)
if args.shell == 'bash':
code = shellcode
else:
code = tcshcode
sys.stdout.write(code % dict(
complete_opts=complete_options,
executable=args.executable
))