Skip to content

Commit 478c6cd

Browse files
committed
New completions command generates shell completion
1 parent 22ec175 commit 478c6cd

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

stagpy/args.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import importlib.resources as imlr
66
import typing
77
from inspect import isfunction
8+
from pathlib import Path
89
from types import MappingProxyType
910

1011
import matplotlib.pyplot as plt
@@ -83,7 +84,24 @@ def parse_args(arglist: Optional[List[str]] = None) -> Callable[[], None]:
8384
Returns:
8485
the function implementing the sub command to be executed.
8586
"""
86-
climan = CLIManager(conf, **SUB_CMDS)
87+
88+
def compl_cmd() -> None:
89+
if conf.completions.zsh:
90+
filepath = Path("_stagpy.sh")
91+
print(f"writing zsh completion file {filepath}")
92+
climan.zsh_complete(filepath, "stagpy", sourceable=True)
93+
elif conf.completions.bash:
94+
filepath = Path("stagpy.sh")
95+
print(f"writing bash completion file {filepath}")
96+
climan.bash_complete(filepath, "stagpy")
97+
else:
98+
print("please choose a shell, `--help` for available options")
99+
100+
climan = CLIManager(
101+
conf,
102+
**SUB_CMDS,
103+
completions=Subcmd("generate completion scripts", func=compl_cmd),
104+
)
87105

88106
bash_script = CONFIG_DIR / "bash" / "stagpy.sh"
89107
bash_script.parent.mkdir(parents=True, exist_ok=True)

stagpy/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ class ConfSection(Section):
233233
create: bool = command_flag("create config file")
234234

235235

236+
@dataclass
237+
class Completions(Section):
238+
"""Shell completion scripts."""
239+
240+
bash: bool = command_flag("generate bash completion")
241+
zsh: bool = command_flag("generate zsh completion")
242+
243+
236244
@dataclass
237245
class Config(ConfigBase):
238246
"""StagPy configuration."""
@@ -249,3 +257,4 @@ class Config(ConfigBase):
249257
info: Info
250258
var: Var
251259
config: ConfSection
260+
completions: Completions

0 commit comments

Comments
 (0)