dataframe-toolkit 0.29.0__tar.gz → 0.30.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. dataframe_toolkit-0.29.0/README.md → dataframe_toolkit-0.30.0/PKG-INFO +55 -0
  2. dataframe_toolkit-0.29.0/PKG-INFO → dataframe_toolkit-0.30.0/README.md +24 -30
  3. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/pyproject.toml +2 -1
  4. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/pyproject.toml.orig +2 -1
  5. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/cli.py +14 -4
  6. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/__init__.py +1 -0
  7. dataframe_toolkit-0.30.0/src/dftk/commands/completion_cmd.py +158 -0
  8. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/skill_cmd.py +6 -0
  9. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/skills/dftk/SKILL.md +2 -1
  10. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/__init__.py +0 -0
  11. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/annotate_cmd.py +0 -0
  12. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/append_cmd.py +0 -0
  13. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/base.py +0 -0
  14. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/binx_cmd.py +0 -0
  15. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/box_cmd.py +0 -0
  16. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/chi_cmd.py +0 -0
  17. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/clean_cmd.py +0 -0
  18. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/cleannames_cmd.py +0 -0
  19. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/concat_cmd.py +0 -0
  20. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/corr_cmd.py +0 -0
  21. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/dataset_cmd.py +0 -0
  22. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/describe_cmd.py +0 -0
  23. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/eval_cmd.py +0 -0
  24. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/explode_cmd.py +0 -0
  25. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/fit_cmd.py +0 -0
  26. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/forest_cmd.py +0 -0
  27. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/func_cmd.py +0 -0
  28. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/heatmap_cmd.py +0 -0
  29. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/help.py +0 -0
  30. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/hist_cmd.py +0 -0
  31. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/implode_cmd.py +0 -0
  32. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/info_cmd.py +0 -0
  33. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/interp_cmd.py +0 -0
  34. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/line_cmd.py +0 -0
  35. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/locus_cmd.py +0 -0
  36. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/manhattan_cmd.py +0 -0
  37. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/melt_cmd.py +0 -0
  38. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/merge_cmd.py +0 -0
  39. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/pairs_cmd.py +0 -0
  40. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/phewas_cmd.py +0 -0
  41. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/pivot_cmd.py +0 -0
  42. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/print_cmd.py +0 -0
  43. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/qq_cmd.py +0 -0
  44. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/query_cmd.py +0 -0
  45. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/randvar_cmd.py +0 -0
  46. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/sample_cmd.py +0 -0
  47. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/scale_cmd.py +0 -0
  48. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/scat_cmd.py +0 -0
  49. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/segid_cmd.py +0 -0
  50. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/split_cmd.py +0 -0
  51. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/stat_cmd.py +0 -0
  52. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/test_cmd.py +0 -0
  53. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/transpose_cmd.py +0 -0
  54. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/commands/wstat_cmd.py +0 -0
  55. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/__init__.py +0 -0
  56. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/copula.py +0 -0
  57. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/docformats.py +0 -0
  58. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/evalfuncs.py +0 -0
  59. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/gwas.py +0 -0
  60. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/io.py +0 -0
  61. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/plot.py +0 -0
  62. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/seed.py +0 -0
  63. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/seq.py +0 -0
  64. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/common/stats.py +0 -0
  65. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/io.py +0 -0
  66. {dataframe_toolkit-0.29.0 → dataframe_toolkit-0.30.0}/src/dftk/skills/dftk/references/snakemake.md +0 -0
@@ -1,3 +1,34 @@
1
+ Metadata-Version: 2.3
2
+ Name: dataframe-toolkit
3
+ Version: 0.30.0
4
+ Summary: A command-line toolkit for DataFrame analysis and manipulation
5
+ Keywords: dataframe,statistics,data analysis,CLI,TSV,pandas
6
+ Author: Gunnar Pálsson
7
+ Author-email: Gunnar Pálsson <hemingur@gmail.com>
8
+ License: MIT
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
13
+ Classifier: Topic :: Utilities
14
+ Classifier: Programming Language :: Python :: 3
15
+ Requires-Dist: pandas>=2.1
16
+ Requires-Dist: numpy>=1.24
17
+ Requires-Dist: pyarrow>=12.0
18
+ Requires-Dist: scipy>=1.10
19
+ Requires-Dist: statsmodels>=0.14
20
+ Requires-Dist: patsy>=0.5.6
21
+ Requires-Dist: duckdb>=0.8
22
+ Requires-Dist: matplotlib>=3.7
23
+ Requires-Dist: pillow>=13.0
24
+ Requires-Dist: seaborn>=0.12
25
+ Requires-Dist: tqdm>=4.65
26
+ Requires-Dist: shtab>=1.7
27
+ Requires-Python: >=3.12
28
+ Project-URL: Homepage, https://github.com/hemingur/dataframe-toolkit
29
+ Project-URL: Repository, https://github.com/hemingur/dataframe-toolkit
30
+ Description-Content-Type: text/markdown
31
+
1
32
  # dftk — DataFrame analysis and manipulation toolkit
2
33
 
3
34
  `dftk` is a command-line toolkit for exploratory data analysis on TSV files.
@@ -69,6 +100,29 @@ It prints where it installed the skill. Run it again after upgrading dftk
69
100
  so the skill matches the installed version; it replaces only the earlier
70
101
  dftk skill. The source is in `src/dftk/skills/dftk/`.
71
102
 
103
+ ### Tab completion
104
+
105
+ ```bash
106
+ dftk completion install # for your shell ($SHELL): bash or zsh
107
+ dftk completion install --shell zsh
108
+ ```
109
+
110
+ Tab then completes commands, options, the values an option allows
111
+ (`--format`, `--fit`, `--catwidth`, …) and file names. The script is plain
112
+ shell generated from dftk's own parsers, so pressing Tab is instant.
113
+
114
+ - bash: installed to `~/.local/share/bash-completion/completions/dftk`, which
115
+ bash-completion loads by itself (most Linux systems load it in `~/.bashrc`;
116
+ on macOS, `brew install bash-completion@2`). Open a new shell.
117
+ - zsh: installed to `~/.zfunc/_dftk`. Add `fpath+=~/.zfunc` to `~/.zshrc`
118
+ before `compinit` (with Oh My Zsh, before the line that sources
119
+ `oh-my-zsh.sh`), then open a new shell; if it doesn't complete,
120
+ `rm ~/.zcompdump*` once.
121
+
122
+ Run it again after upgrading dftk so new options complete; `dftk skill install`
123
+ also refreshes an installed completion script. `dftk completion print`
124
+ writes the script to stdout instead.
125
+
72
126
  ## Quick example
73
127
 
74
128
  ```bash
@@ -156,6 +210,7 @@ publication-quality output.
156
210
  | `annotate` | Read and write provenance metadata (genome, source, …) in parquet files |
157
211
  | `print` | Read any dftk input (TSV, parquet, stdin) and write TSV, JSON, Markdown or LaTeX |
158
212
  | `clean` | Remove leftover temp parquet pipe files from interrupted pipelines |
213
+ | `completion` | Tab completion for bash and zsh: `dftk completion install` |
159
214
  | `help` | List all subcommands or show full help for one: `dftk help stat` |
160
215
 
161
216
  ## Common patterns
@@ -1,33 +1,3 @@
1
- Metadata-Version: 2.3
2
- Name: dataframe-toolkit
3
- Version: 0.29.0
4
- Summary: A command-line toolkit for DataFrame analysis and manipulation
5
- Keywords: dataframe,statistics,data analysis,CLI,TSV,pandas
6
- Author: Gunnar Pálsson
7
- Author-email: Gunnar Pálsson <hemingur@gmail.com>
8
- License: MIT
9
- Classifier: Development Status :: 3 - Alpha
10
- Classifier: Intended Audience :: Science/Research
11
- Classifier: Intended Audience :: Developers
12
- Classifier: Topic :: Scientific/Engineering :: Information Analysis
13
- Classifier: Topic :: Utilities
14
- Classifier: Programming Language :: Python :: 3
15
- Requires-Dist: pandas>=2.1
16
- Requires-Dist: numpy>=1.24
17
- Requires-Dist: pyarrow>=12.0
18
- Requires-Dist: scipy>=1.10
19
- Requires-Dist: statsmodels>=0.14
20
- Requires-Dist: patsy>=0.5.6
21
- Requires-Dist: duckdb>=0.8
22
- Requires-Dist: matplotlib>=3.7
23
- Requires-Dist: pillow>=13.0
24
- Requires-Dist: seaborn>=0.12
25
- Requires-Dist: tqdm>=4.65
26
- Requires-Python: >=3.12
27
- Project-URL: Homepage, https://github.com/hemingur/dataframe-toolkit
28
- Project-URL: Repository, https://github.com/hemingur/dataframe-toolkit
29
- Description-Content-Type: text/markdown
30
-
31
1
  # dftk — DataFrame analysis and manipulation toolkit
32
2
 
33
3
  `dftk` is a command-line toolkit for exploratory data analysis on TSV files.
@@ -99,6 +69,29 @@ It prints where it installed the skill. Run it again after upgrading dftk
99
69
  so the skill matches the installed version; it replaces only the earlier
100
70
  dftk skill. The source is in `src/dftk/skills/dftk/`.
101
71
 
72
+ ### Tab completion
73
+
74
+ ```bash
75
+ dftk completion install # for your shell ($SHELL): bash or zsh
76
+ dftk completion install --shell zsh
77
+ ```
78
+
79
+ Tab then completes commands, options, the values an option allows
80
+ (`--format`, `--fit`, `--catwidth`, …) and file names. The script is plain
81
+ shell generated from dftk's own parsers, so pressing Tab is instant.
82
+
83
+ - bash: installed to `~/.local/share/bash-completion/completions/dftk`, which
84
+ bash-completion loads by itself (most Linux systems load it in `~/.bashrc`;
85
+ on macOS, `brew install bash-completion@2`). Open a new shell.
86
+ - zsh: installed to `~/.zfunc/_dftk`. Add `fpath+=~/.zfunc` to `~/.zshrc`
87
+ before `compinit` (with Oh My Zsh, before the line that sources
88
+ `oh-my-zsh.sh`), then open a new shell; if it doesn't complete,
89
+ `rm ~/.zcompdump*` once.
90
+
91
+ Run it again after upgrading dftk so new options complete; `dftk skill install`
92
+ also refreshes an installed completion script. `dftk completion print`
93
+ writes the script to stdout instead.
94
+
102
95
  ## Quick example
103
96
 
104
97
  ```bash
@@ -186,6 +179,7 @@ publication-quality output.
186
179
  | `annotate` | Read and write provenance metadata (genome, source, …) in parquet files |
187
180
  | `print` | Read any dftk input (TSV, parquet, stdin) and write TSV, JSON, Markdown or LaTeX |
188
181
  | `clean` | Remove leftover temp parquet pipe files from interrupted pipelines |
182
+ | `completion` | Tab completion for bash and zsh: `dftk completion install` |
189
183
  | `help` | List all subcommands or show full help for one: `dftk help stat` |
190
184
 
191
185
  ## Common patterns
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "dataframe-toolkit"
3
- version = "0.29.0"
3
+ version = "0.30.0"
4
4
  description = "A command-line toolkit for DataFrame analysis and manipulation"
5
5
  readme = "README.md"
6
6
  keywords = [
@@ -32,6 +32,7 @@ dependencies = [
32
32
  "pillow>=13.0",
33
33
  "seaborn>=0.12",
34
34
  "tqdm>=4.65",
35
+ "shtab>=1.7",
35
36
  ]
36
37
 
37
38
  [[project.authors]]
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "dataframe-toolkit"
3
- version = "0.29.0"
3
+ version = "0.30.0"
4
4
  description = "A command-line toolkit for DataFrame analysis and manipulation"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -29,6 +29,7 @@ dependencies = [
29
29
  "pillow>=13.0", # not used directly (matplotlib needs it); floor for security fixes
30
30
  "seaborn>=0.12",
31
31
  "tqdm>=4.65",
32
+ "shtab>=1.7", # dftk completion: bash/zsh scripts from the parser
32
33
  ]
33
34
 
34
35
  [project.urls]
@@ -35,11 +35,11 @@ def _commands_for(argv: list[str]) -> list[BaseCommand]:
35
35
  return [load_command(argv[0])]
36
36
  # `dftk --help`, no command, or an unknown name: load everything so the
37
37
  # command listing and argparse's "invalid choice" message are complete.
38
- grouped = [cmd for _, cmds in load_command_groups() for cmd in cmds]
39
- return grouped + [load_command("help")]
38
+ return all_commands()
40
39
 
41
40
 
42
- def main() -> None:
41
+ def build_parser(commands: list[BaseCommand]) -> argparse.ArgumentParser:
42
+ """The dftk parser with a subparser for each of *commands*."""
43
43
  parser = argparse.ArgumentParser(
44
44
  prog="dftk",
45
45
  description="DataFrame analysis and manipulation toolkit.",
@@ -59,11 +59,21 @@ def main() -> None:
59
59
  subparsers = parser.add_subparsers(dest="command", metavar="COMMAND")
60
60
  subparsers.required = True
61
61
 
62
- for cmd in _commands_for(sys.argv[1:]):
62
+ for cmd in commands:
63
63
  cmd_parser = subparsers.add_parser(cmd.name, help=cmd.help)
64
64
  cmd.add_arguments(cmd_parser)
65
65
  cmd_parser.set_defaults(command_instance=cmd)
66
+ return parser
66
67
 
68
+
69
+ def all_commands() -> list[BaseCommand]:
70
+ """Every command, in the order `dftk --help` lists them."""
71
+ grouped = [cmd for _, cmds in load_command_groups() for cmd in cmds]
72
+ return grouped + [load_command("help")]
73
+
74
+
75
+ def main() -> None:
76
+ parser = build_parser(_commands_for(sys.argv[1:]))
67
77
  args = parser.parse_args()
68
78
 
69
79
  try:
@@ -83,6 +83,7 @@ COMMAND_GROUPS: list[tuple[str, list[tuple[str, str]]]] = [
83
83
  ("print", "print_cmd:PrintCommand"),
84
84
  ("clean", "clean_cmd:CleanCommand"),
85
85
  ("skill", "skill_cmd:SkillCommand"),
86
+ ("completion", "completion_cmd:CompletionCommand"),
86
87
  ],
87
88
  ),
88
89
  ]
@@ -0,0 +1,158 @@
1
+ """
2
+ dftk.commands.completion_cmd — dftk completion subcommand.
3
+
4
+ Writes a Tab-completion script for bash or zsh, generated with shtab from the
5
+ real dftk parser: commands, options and their allowed values, and file names
6
+ where a command takes a file. The script is plain shell, so pressing Tab
7
+ never starts Python; it is regenerated after an upgrade.
8
+ """
9
+
10
+ import argparse
11
+ import os
12
+ from pathlib import Path
13
+
14
+ from dftk.commands.base import BaseCommand
15
+
16
+ SHELLS = ("bash", "zsh")
17
+
18
+ _EPILOG = """\
19
+ Tab completes commands, options, the values an option allows (--format,
20
+ --fit, --catwidth, ...) and file names. Pressing Tab never starts Python:
21
+ the script lists everything, so it is instant.
22
+
23
+ dftk completion install install for your shell ($SHELL)
24
+ dftk completion install --shell zsh
25
+ dftk completion print > FILE write the script yourself
26
+
27
+ bash: installs to ~/.local/share/bash-completion/completions/dftk, which
28
+ bash-completion loads the first time you press Tab after "dftk" (Ubuntu
29
+ and most Linux systems load bash-completion in ~/.bashrc; on macOS install
30
+ it with Homebrew). Open a new shell to start using it.
31
+
32
+ zsh: installs to ~/.zfunc/_dftk. Add this to ~/.zshrc, before compinit or,
33
+ with Oh My Zsh, before the line that sources oh-my-zsh.sh:
34
+
35
+ fpath+=~/.zfunc
36
+
37
+ then open a new shell (if it does not complete, rm ~/.zcompdump* once).
38
+
39
+ Run "dftk completion install" again after upgrading dftk, so new commands
40
+ and options complete; "dftk skill install" also refreshes an installed
41
+ completion script.
42
+ """
43
+
44
+
45
+ def default_shell() -> str:
46
+ """The user's shell from $SHELL, if dftk supports it, else bash."""
47
+ shell = Path(os.environ.get("SHELL", "")).name
48
+ return shell if shell in SHELLS else "bash"
49
+
50
+
51
+ def install_path(shell: str) -> Path:
52
+ """Where `dftk completion install` puts the script for *shell*."""
53
+ home = Path.home()
54
+ if shell == "zsh":
55
+ return home / ".zfunc" / "_dftk"
56
+ user_dir = os.environ.get("BASH_COMPLETION_USER_DIR")
57
+ if user_dir:
58
+ return Path(user_dir) / "completions" / "dftk"
59
+ data = os.environ.get("XDG_DATA_HOME") or str(home / ".local" / "share")
60
+ return Path(data) / "bash-completion" / "completions" / "dftk"
61
+
62
+
63
+ def _subparsers(parser: argparse.ArgumentParser) -> dict:
64
+ for action in parser._actions:
65
+ if isinstance(action, argparse._SubParsersAction):
66
+ return action.choices
67
+ return {}
68
+
69
+
70
+ def _mark_completions(parser: argparse.ArgumentParser) -> None:
71
+ """Tell shtab which arguments are files or directories, and complete
72
+ `dftk help COMMAND` with the command names."""
73
+ import shtab
74
+
75
+ commands = _subparsers(parser)
76
+ for name, sub in commands.items():
77
+ for action in sub._actions:
78
+ if action.nargs == 0 or action.choices:
79
+ continue
80
+ metavar = str(action.metavar or "")
81
+ if name == "help" and not action.option_strings:
82
+ action.choices = sorted(commands)
83
+ elif not action.option_strings and action.dest in (
84
+ "DATAFILE",
85
+ "DATAFILES",
86
+ "PARQUET",
87
+ ):
88
+ action.complete = shtab.FILE
89
+ elif metavar in ("FILE", "PATH"):
90
+ action.complete = shtab.FILE
91
+ elif metavar == "DIR":
92
+ action.complete = shtab.DIRECTORY
93
+
94
+
95
+ def completion_script(shell: str) -> str:
96
+ """The completion script for *shell*, from every dftk command's parser."""
97
+ import shtab
98
+
99
+ from dftk.cli import all_commands, build_parser
100
+
101
+ parser = build_parser(all_commands())
102
+ _mark_completions(parser)
103
+ return shtab.complete(parser, shell=shell)
104
+
105
+
106
+ def install(shell: str) -> Path:
107
+ path = install_path(shell)
108
+ script = completion_script(shell)
109
+ path.parent.mkdir(parents=True, exist_ok=True)
110
+ path.write_text(script)
111
+ return path
112
+
113
+
114
+ def refresh_installed() -> list[Path]:
115
+ """Rewrite the completion scripts already installed (after an upgrade)."""
116
+ return [install(shell) for shell in SHELLS if install_path(shell).is_file()]
117
+
118
+
119
+ class CompletionCommand(BaseCommand):
120
+ @property
121
+ def name(self) -> str:
122
+ return "completion"
123
+
124
+ @property
125
+ def help(self) -> str:
126
+ return "Tab completion for bash and zsh"
127
+
128
+ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
129
+ parser.formatter_class = argparse.RawDescriptionHelpFormatter
130
+ parser.epilog = _EPILOG
131
+ parser.add_argument(
132
+ "action",
133
+ choices=["install", "print"],
134
+ help="install: write the script where the shell finds it; "
135
+ "print: write it to stdout.",
136
+ )
137
+ parser.add_argument(
138
+ "--shell",
139
+ choices=SHELLS,
140
+ default=None,
141
+ help="Shell to complete in (default: from $SHELL, else bash).",
142
+ )
143
+
144
+ def execute(self, args: argparse.Namespace) -> None:
145
+ shell = args.shell or default_shell()
146
+ if args.action == "print":
147
+ print(completion_script(shell), end="")
148
+ return
149
+ path = install(shell)
150
+ print(f"Installed {shell} completion for dftk in {path}")
151
+ if shell == "zsh":
152
+ print(
153
+ "Add 'fpath+=~/.zfunc' to ~/.zshrc before compinit (with Oh My "
154
+ "Zsh: before it sources oh-my-zsh.sh), then open a new shell."
155
+ )
156
+ else:
157
+ print("Open a new shell to use it.")
158
+ print('Run "dftk completion install" again after upgrading dftk.')
@@ -102,6 +102,12 @@ def _skill(args: argparse.Namespace) -> None:
102
102
  replaced = _install(folder)
103
103
  verb = "Updated" if replaced else "Installed"
104
104
  print(f"{verb} the dftk skill (dftk {_dftk_version()}) in {folder / 'dftk'}")
105
+ # an installed Tab-completion script is refreshed too, so one command
106
+ # after an upgrade brings both up to date
107
+ from dftk.commands.completion_cmd import refresh_installed
108
+
109
+ for path in refresh_installed():
110
+ print(f"Updated the dftk completion script in {path}")
105
111
  print('Run "dftk skill install" again after upgrading dftk.')
106
112
 
107
113
 
@@ -190,7 +190,8 @@ negative intercept).
190
190
 
191
191
  Other: `dataset NAME` loads a bundled example table, `annotate FILE.parquet`
192
192
  reads and writes provenance metadata, `clean` removes leftover temp pipe
193
- files, `skill install` installs this skill for Claude Code and Codex.
193
+ files, `skill install` installs this skill for Claude Code and Codex,
194
+ `completion install` sets up Tab completion in bash or zsh.
194
195
 
195
196
  ## Examples
196
197