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