-
-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathargcomplete_app.py
More file actions
executable file
·154 lines (117 loc) · 5.24 KB
/
argcomplete_app.py
File metadata and controls
executable file
·154 lines (117 loc) · 5.24 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
"""A simple example to test CLI completion with `traitlets.Application`
Follow the installation instructions in
https://github.com/kislyuk/argcomplete#installation
to install argcomplete. For example for bash, you can set up global completion
via something like::
$ activate-global-python-argcomplete --dest=~/.bash_completion.d
and ``source ~/.bash_completion.d`` in your ``~/.bashrc``. To use the
`global-python-argcomplete`, your `traitlets.Application`-based script should
have the string ``PYTHON_ARGCOMPLETE_OK`` in the first few lines of the script.
Afterwards, try tab completing options to this script::
# Option completion, show flags, aliases and --Class.
$ examples/argcomplete_app.py --[TAB]
--Application. --JsonPrinter. --env-vars --s
--ArgcompleteApp. --e --help --skip-if-missing
--EnvironPrinter. --env-var --json-indent --style
$ examples/argcomplete_app.py --A[TAB]
--Application. --ArgcompleteApp.
# Complete class config traits
"""TODO: Add description."""
$ examples/argcomplete_app.py --EnvironPrinter.[TAB]
--EnvironPrinter.no_complete --EnvironPrinter.style
--EnvironPrinter.skip_if_missing --EnvironPrinter.vars
# Using argcomplete's provided EnvironCompleter
$ examples/argcomplete_app.py --EnvironPrinter.vars=[TAB]
APPDATA LS_COLORS
COMP_LINE NAME
COMP_POINT OLDPWD
COMP_TYPE PATH
...
$ examples/argcomplete_app.py --EnvironPrinter.vars USER [TAB]
APPDATA LS_COLORS
COMP_LINE NAME
COMP_POINT OLDPWD
COMP_TYPE PATH
...
# Alias for --EnvironPrinter.vars
$ examples/argcomplete_app.py --env-vars P[TAB]
PATH PWD PYTHONPATH
# Custom completer example
$ examples/argcomplete_app.py --env-vars PWD --json-indent [TAB]
2 4 8
# Enum completer example
$ examples/argcomplete_app.py --style [TAB]
ndjson posix verbose
# Bool completer example
$ examples/argcomplete_app.py --Application.show_config_json [TAB]
0 1 false true
If completions are not showing, you can set the environment variable ``_ARC_DEBUG=1``
to assist in debugging argcomplete. This was last checked with ``argcomplete==1.12.3``.
"""
from __future__ import annotations
import json
import os
try:
from argcomplete.completers import EnvironCompleter, SuppressCompleter
except ImportError:
EnvironCompleter = SuppressCompleter = None
from traitlets import Bool, Enum, Int, List, Unicode
from traitlets.config.application import Application
from traitlets.config.configurable import Configurable
def _indent_completions(**kwargs):
"""Example of a custom completer, which could be dynamic"""
return ["2", "4", "8"]
class JsonPrinter(Configurable):
indent = Int(None, allow_none=True).tag(config=True, argcompleter=_indent_completions)
def print(self, obj):
print(json.dumps(obj, indent=self.indent))
class EnvironPrinter(Configurable):
"""A class that has configurable, typed attributes."""
vars = List(trait=Unicode(), help="Environment variable").tag(
# NOTE: currently multiplicity is ignored by the traitlets CLI.
# Refer to issue GH#690 for discussion
config=True,
multiplicity="+",
argcompleter=EnvironCompleter,
)
no_complete = Unicode().tag(config=True, argcompleter=SuppressCompleter)
style = Enum(values=["posix", "ndjson", "verbose"], default_value="posix").tag(config=True)
skip_if_missing = Bool(False, help="Skip variable if not set").tag(config=True)
def print(self):
for env_var in self.vars:
if env_var not in os.environ:
if self.skip_if_missing:
continue
raise KeyError(f"Environment variable not set: {env_var}")
value = os.environ[env_var]
if self.style == "posix":
print(f"{env_var}={value}")
elif self.style == "verbose":
print(f">> key: {env_var} value:\n{value}\n")
elif self.style == "ndjson":
JsonPrinter(parent=self).print({"key": env_var, "value": value})
def bool_flag(trait, value=True):
return ({trait.this_class.__name__: {trait.name: value}}, trait.help)
class ArgcompleteApp(Application):
name = Unicode("argcomplete-example-app")
description = Unicode("prints requested environment variables")
classes = [JsonPrinter, EnvironPrinter]
config_file = Unicode("", help="Load this config file").tag(config=True)
aliases = {
("e", "env-var", "env-vars"): "EnvironPrinter.vars",
("s", "style"): "EnvironPrinter.style",
("json-indent"): "JsonPrinter.indent",
}
flags = {
"skip-if-missing": bool_flag(EnvironPrinter.skip_if_missing),
}
def initialize(self, argv=None):
self.parse_command_line(argv)
if self.config_file:
self.load_config_file(self.config_file)
def start(self):
EnvironPrinter(parent=self).print()
if __name__ == "__main__":
ArgcompleteApp.launch_instance()