28
28
from textwrap import dedent
29
29
from typing import Tuple
30
30
31
+ try :
32
+ import shtab
33
+ except ImportError :
34
+ from . import _shtab as shtab
31
35
import appdirs
32
36
from prompt_toolkit .formatted_text import HTML
33
37
from prompt_toolkit .shortcuts import print_formatted_text
41
45
42
46
43
47
__all__ = ["create_parser" , "get_config_and_history_file" , "run" ]
48
+ # https://github.com/iterative/shtab/blob/master/examples/customcomplete.py#L11-L22
49
+ PY_FILE = {
50
+ "bash" : "_shtab_greeter_compgen_py_file" ,
51
+ "zsh" : "_files -g '*.py'" ,
52
+ "tcsh" : "f:*.py" ,
53
+ }
54
+ PREAMBLE = {
55
+ "bash" : """\
56
+ # $1=COMP_WORDS[1]
57
+ _shtab_greeter_compgen_py_file() {
58
+ compgen -d -- $1 # recurse into subdirs
59
+ compgen -f -X '!*?.py' -- $1
60
+ }
61
+ """ ,
62
+ "zsh" : """\
63
+ _script_args() {
64
+ _arguments -S -s '(-)1:script_args:_files -g "*.py"' '*: :_files'
65
+ }
66
+ """ ,
67
+ }
68
+ SCRIPT_ARGS = {"zsh" : "_script_args" }
44
69
45
70
46
71
class _Parser (argparse .ArgumentParser ):
@@ -58,7 +83,8 @@ def print_help(self):
58
83
59
84
60
85
def create_parser () -> _Parser :
61
- parser = _Parser (description = "ptpython: Interactive Python shell." )
86
+ parser = _Parser ("ptpython" , description = "ptpython: Interactive Python shell." )
87
+ shtab .add_argument_to (parser , preamble = PREAMBLE )
62
88
parser .add_argument ("--vi" , action = "store_true" , help = "Enable Vi key bindings" )
63
89
parser .add_argument (
64
90
"-i" ,
@@ -70,23 +96,27 @@ def create_parser() -> _Parser:
70
96
"--light-bg" ,
71
97
action = "store_true" ,
72
98
help = "Run on a light background (use dark colors for text)." ,
73
- ),
99
+ )
74
100
parser .add_argument (
75
101
"--dark-bg" ,
76
102
action = "store_true" ,
77
103
help = "Run on a dark background (use light colors for text)." ,
78
- ),
104
+ )
79
105
parser .add_argument (
80
106
"--config-file" , type = str , help = "Location of configuration file."
81
- )
82
- parser .add_argument ("--history-file" , type = str , help = "Location of history file." )
107
+ ).complete = PY_FILE
108
+ parser .add_argument (
109
+ "--history-file" , type = str , help = "Location of history file."
110
+ ).complete = shtab .FILE
83
111
parser .add_argument (
84
112
"-V" ,
85
113
"--version" ,
86
114
action = "version" ,
87
115
version = metadata .version ("ptpython" ), # type: ignore
88
116
)
89
- parser .add_argument ("args" , nargs = "*" , help = "Script and arguments" )
117
+ parser .add_argument (
118
+ "args" , nargs = argparse .REMAINDER , help = "Script and arguments"
119
+ ).complete = SCRIPT_ARGS
90
120
return parser
91
121
92
122
0 commit comments