git-project 0.1.0__tar.gz → 0.1.1__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 (51) hide show
  1. {git_project-0.1.0 → git_project-0.1.1}/PKG-INFO +23 -11
  2. {git_project-0.1.0 → git_project-0.1.1}/_version.py +2 -2
  3. {git_project-0.1.0 → git_project-0.1.1}/docs/changelog.rst +10 -1
  4. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/__init__.py +13 -3
  5. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/configobj.py +29 -0
  6. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/git.py +4 -0
  7. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/parsermanager.py +110 -36
  8. {git_project-0.1.0 → git_project-0.1.1}/tests/test_confobj.py +31 -0
  9. {git_project-0.1.0 → git_project-0.1.1}/tests/test_parsermanager.py +162 -0
  10. {git_project-0.1.0 → git_project-0.1.1}/.gitignore +0 -0
  11. {git_project-0.1.0 → git_project-0.1.1}/CONTRIBUTORS +0 -0
  12. {git_project-0.1.0 → git_project-0.1.1}/COPYING +0 -0
  13. {git_project-0.1.0 → git_project-0.1.1}/MANIFEST.in +0 -0
  14. {git_project-0.1.0 → git_project-0.1.1}/changelog.d/README.rst +0 -0
  15. {git_project-0.1.0 → git_project-0.1.1}/changelog.py +0 -0
  16. {git_project-0.1.0 → git_project-0.1.1}/docs/Makefile +0 -0
  17. {git_project-0.1.0 → git_project-0.1.1}/docs/api.rst +0 -0
  18. {git_project-0.1.0 → git_project-0.1.1}/docs/authors.rst +0 -0
  19. {git_project-0.1.0 → git_project-0.1.1}/docs/conf.py +0 -0
  20. {git_project-0.1.0 → git_project-0.1.1}/docs/contributing.rst +0 -0
  21. {git_project-0.1.0 → git_project-0.1.1}/docs/index.rst +0 -0
  22. {git_project-0.1.0 → git_project-0.1.1}/docs/intro.rst +0 -0
  23. {git_project-0.1.0 → git_project-0.1.1}/docs/license.rst +0 -0
  24. {git_project-0.1.0 → git_project-0.1.1}/docs/make.bat +0 -0
  25. {git_project-0.1.0 → git_project-0.1.1}/docs/plugins.rst +0 -0
  26. {git_project-0.1.0 → git_project-0.1.1}/pyproject.toml +0 -0
  27. {git_project-0.1.0 → git_project-0.1.1}/requirements.txt +0 -0
  28. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/commandline.py +0 -0
  29. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/exception.py +0 -0
  30. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/gitproject.py +0 -0
  31. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/main.py +0 -0
  32. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/plugin.py +0 -0
  33. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/pluginmanager.py +0 -0
  34. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/project.py +0 -0
  35. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/py.typed +0 -0
  36. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/runnable.py +0 -0
  37. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/scopedobj.py +0 -0
  38. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/shell.py +0 -0
  39. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/substitutable.py +0 -0
  40. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/test_support/__init__.py +0 -0
  41. {git_project-0.1.0 → git_project-0.1.1}/src/git_project/test_support/common.py +0 -0
  42. {git_project-0.1.0 → git_project-0.1.1}/tests/conftest.py +0 -0
  43. {git_project-0.1.0 → git_project-0.1.1}/tests/test_changelog.py +0 -0
  44. {git_project-0.1.0 → git_project-0.1.1}/tests/test_git.py +0 -0
  45. {git_project-0.1.0 → git_project-0.1.1}/tests/test_gitproject.py +0 -0
  46. {git_project-0.1.0 → git_project-0.1.1}/tests/test_main.py +0 -0
  47. {git_project-0.1.0 → git_project-0.1.1}/tests/test_project.py +0 -0
  48. {git_project-0.1.0 → git_project-0.1.1}/tests/test_runnable.py +0 -0
  49. {git_project-0.1.0 → git_project-0.1.1}/tests/test_scopedobj.py +0 -0
  50. {git_project-0.1.0 → git_project-0.1.1}/tests/test_shell.py +0 -0
  51. {git_project-0.1.0 → git_project-0.1.1}/tests/test_substitutable.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: git-project
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: The extensible stupid project manager
5
5
  Author-email: "David A. Greene" <dag@obbligato.org>
6
6
  License-Expression: AGPL-3.0-or-later
@@ -171,9 +171,19 @@ full manual.
171
171
  ``--menu`` lists a command's subcommands, arguments and options in a form meant
172
172
  for tools. ``--menu=json`` prints them as one JSON object and adds each
173
173
  subcommand's aliases and each argument's ``nargs`` and ``choices``. An option's
174
- ``name`` is its long flag when it has one. The ``version`` field changes only
175
- when a key is removed or changes meaning, so a reader should ignore keys it
176
- does not know.
174
+ ``name`` is its long flag when it has one. ``--menu=json-tree`` prints the same
175
+ object with each subcommand's own object nested under its ``menu`` key, so one
176
+ call returns every level. The ``version`` field changes only when a key is
177
+ removed or changes meaning, so a reader should ignore keys it does not know.
178
+
179
+ ``nargs`` is null when the argument takes argparse's default, one value or none
180
+ for a switch. Otherwise it is a count such as ``"2"``, or ``"?"``, ``"*"``,
181
+ ``"+"`` or ``"..."``, which takes every remaining word as written. Three keys help a tool offer values. ``complete``
182
+ names the kind of value: ``branch``, ``committish``, ``directory``, ``file``,
183
+ ``remote`` or ``worktree``, a git-project worktree name. A null or unknown
184
+ ``complete`` means free text. ``candidates`` lists values to suggest, which
185
+ unlike ``choices`` the command does not enforce. ``repeatable`` is true when
186
+ each use of an option adds to its value rather than replacing it.
177
187
 
178
188
  License
179
189
  =======
@@ -218,14 +228,16 @@ Authors
218
228
  .. _David Greene: https://github.com/greened
219
229
 
220
230
 
221
- `0.1.0`_ - 2026-10-15
231
+ `0.1.1`_ - 2026-10-19
222
232
  ---------------------
223
- Changed
224
- .......
225
- - ``--menu=json`` names an option by its long flag when it has one, and lists its other flags as aliases, whatever order they were declared in.
226
- - Versions follow semantic versioning from 0.1.0. Before 1.0.0 a breaking change bumps the minor version, and a feature or a fix bumps the patch. From 1.0.0 on, a breaking change bumps the major version, a feature the minor and a fix the patch. ``--menu=json`` version 1 is part of that contract.
227
-
228
- .. _Unreleased: https://github.com/greened/git-project/compare/v0.1.0...HEAD
233
+ Added
234
+ .....
235
+ - ``ConfigObject.iter_idents`` lists the idents of the objects recorded under a subsection, so a plugin can offer them as ``candidates``.
236
+ - ``--menu=json`` gives each argument ``complete``, ``candidates`` and ``repeatable`` keys, set from new ``complete=`` and ``candidates=`` keywords to ``add_argument`` and from the argparse action.
237
+ - ``--menu=json-tree`` prints the whole command tree in one call, with each subcommand's menu nested under its ``menu`` key.
238
+
239
+ .. _Unreleased: https://github.com/greened/git-project/compare/v0.1.1...HEAD
240
+ .. _0.1.1: https://github.com/greened/git-project/compare/v0.1.0...v0.1.1
229
241
  .. _0.1.0: https://github.com/greened/git-project/compare/v0.0.44...v0.1.0
230
242
  .. _0.0.44: https://github.com/greened/git-project/compare/v0.0.43...v0.0.44
231
243
  .. _0.0.43: https://github.com/greened/git-project/compare/v0.0.42...v0.0.43
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
18
18
  commit_id: str | None
19
19
  __commit_id__: str | None
20
20
 
21
- __version__ = version = '0.1.0'
22
- __version_tuple__ = version_tuple = (0, 1, 0)
21
+ __version__ = version = '0.1.1'
22
+ __version_tuple__ = version_tuple = (0, 1, 1)
23
23
 
24
24
  __commit_id__ = commit_id = None
@@ -31,6 +31,14 @@ ChangeLog
31
31
  `Unreleased`_
32
32
  -------------
33
33
 
34
+ `0.1.1`_ - 2026-10-19
35
+ ---------------------
36
+ Added
37
+ .....
38
+ - ``ConfigObject.iter_idents`` lists the idents of the objects recorded under a subsection, so a plugin can offer them as ``candidates``.
39
+ - ``--menu=json`` gives each argument ``complete``, ``candidates`` and ``repeatable`` keys, set from new ``complete=`` and ``candidates=`` keywords to ``add_argument`` and from the argparse action.
40
+ - ``--menu=json-tree`` prints the whole command tree in one call, with each subcommand's menu nested under its ``menu`` key.
41
+
34
42
  `0.1.0`_ - 2026-10-15
35
43
  ---------------------
36
44
  Changed
@@ -260,7 +268,8 @@ Fixed
260
268
  ``git_project``. No declared environment supplied Sphinx either, so a
261
269
  ``docs`` environment with Sphinx is now declared.
262
270
 
263
- .. _Unreleased: https://github.com/greened/git-project/compare/v0.1.0...HEAD
271
+ .. _Unreleased: https://github.com/greened/git-project/compare/v0.1.1...HEAD
272
+ .. _0.1.1: https://github.com/greened/git-project/compare/v0.1.0...v0.1.1
264
273
  .. _0.1.0: https://github.com/greened/git-project/compare/v0.0.44...v0.1.0
265
274
  .. _0.0.44: https://github.com/greened/git-project/compare/v0.0.43...v0.0.44
266
275
  .. _0.0.43: https://github.com/greened/git-project/compare/v0.0.42...v0.0.43
@@ -163,9 +163,19 @@ full manual.
163
163
  ``--menu`` lists a command's subcommands, arguments and options in a form meant
164
164
  for tools. ``--menu=json`` prints them as one JSON object and adds each
165
165
  subcommand's aliases and each argument's ``nargs`` and ``choices``. An option's
166
- ``name`` is its long flag when it has one. The ``version`` field changes only
167
- when a key is removed or changes meaning, so a reader should ignore keys it
168
- does not know.
166
+ ``name`` is its long flag when it has one. ``--menu=json-tree`` prints the same
167
+ object with each subcommand's own object nested under its ``menu`` key, so one
168
+ call returns every level. The ``version`` field changes only when a key is
169
+ removed or changes meaning, so a reader should ignore keys it does not know.
170
+
171
+ ``nargs`` is null when the argument takes argparse's default, one value or none
172
+ for a switch. Otherwise it is a count such as ``"2"``, or ``"?"``, ``"*"``,
173
+ ``"+"`` or ``"..."``, which takes every remaining word as written. Three keys help a tool offer values. ``complete``
174
+ names the kind of value: ``branch``, ``committish``, ``directory``, ``file``,
175
+ ``remote`` or ``worktree``, a git-project worktree name. A null or unknown
176
+ ``complete`` means free text. ``candidates`` lists values to suggest, which
177
+ unlike ``choices`` the command does not enforce. ``repeatable`` is true when
178
+ each use of an option adds to its value rather than replacing it.
169
179
 
170
180
  License
171
181
  =======
@@ -165,6 +165,35 @@ class ConfigObject:
165
165
  section = git.config.get_section(gitsection)
166
166
  return True if section else False
167
167
 
168
+ @classmethod
169
+ def iter_idents(cls, git, project_section, subsection):
170
+ """Yield the ident of each ConfigObject that exists under subsection.
171
+
172
+ git: An object to query the repository and make config changes.
173
+
174
+ project_section: git config section of the active project.
175
+
176
+ subsection: The subsection whose idents to list.
177
+
178
+ """
179
+ if not git.has_repo():
180
+ return
181
+
182
+ prefix = (
183
+ ConfigObject._get_full_section(
184
+ ConfigObject._map_project_section(project_section),
185
+ subsection,
186
+ None,
187
+ )
188
+ + "."
189
+ )
190
+
191
+ for name in git.config.iter_section_names():
192
+ ident = name[len(prefix) :]
193
+ # An empty ident names the subsection itself, not an object.
194
+ if name.startswith(prefix) and ident:
195
+ yield ident
196
+
168
197
  @staticmethod
169
198
  def _map_project_section(project_section):
170
199
  """Return the git config section name for project_section. get, exists and
@@ -382,6 +382,10 @@ class Git:
382
382
  """Get the named section from the config."""
383
383
  return self._sections.get(section, None)
384
384
 
385
+ def iter_section_names(self):
386
+ """Yield the name of each section in the config."""
387
+ yield from list(self._sections)
388
+
385
389
  def rm_section(self, section_name):
386
390
  """Remove the named section from the config."""
387
391
  section = self.get_section(section_name)
@@ -94,7 +94,17 @@ class ParserManager:
94
94
 
95
95
  class Argument:
96
96
  def __init__(
97
- self, name, aliases, has_value, desc, nargs=None, choices=None
97
+ self,
98
+ name,
99
+ aliases,
100
+ has_value,
101
+ desc,
102
+ nargs=None,
103
+ choices=None,
104
+ complete=None,
105
+ candidates=None,
106
+ repeatable=False,
107
+ parser=None,
98
108
  ):
99
109
  self.name = name
100
110
  self.aliases = aliases
@@ -102,6 +112,11 @@ class ParserManager:
102
112
  self.description = desc
103
113
  self.nargs = nargs
104
114
  self.choices = choices
115
+ self.complete = complete
116
+ self.candidates = candidates
117
+ self.repeatable = repeatable
118
+ # The child Parser of a command.
119
+ self.parser = parser
105
120
 
106
121
  def menu_nargs(self):
107
122
  return None if self.nargs is None else str(self.nargs)
@@ -111,6 +126,11 @@ class ParserManager:
111
126
  return None
112
127
  return list(map(str, self.choices))
113
128
 
129
+ def menu_candidates(self):
130
+ if self.candidates is None:
131
+ return None
132
+ return list(map(str, self.candidates))
133
+
114
134
  def menu_names(self):
115
135
  # Keys on the long flag regardless of declaration order.
116
136
  names = [self.name, *self.aliases]
@@ -146,38 +166,8 @@ class ParserManager:
146
166
 
147
167
  def call(self, parser, namespace, values, option_string=None):
148
168
  parser = self.parser
149
- if values == "json":
150
- menu = {
151
- "version": 1,
152
- "commands": [
153
- {
154
- "name": command.name,
155
- "aliases": list(command.aliases),
156
- "description": command.description,
157
- }
158
- for command in parser.command_arguments
159
- ],
160
- "positionals": [
161
- {
162
- "name": positional.name,
163
- "nargs": positional.menu_nargs(),
164
- "choices": positional.menu_choices(),
165
- "description": positional.description,
166
- }
167
- for positional in parser.positional_arguments
168
- ],
169
- "optionals": [
170
- {
171
- "name": optional.menu_names()[0],
172
- "aliases": optional.menu_names()[1],
173
- "has_value": optional.has_value,
174
- "nargs": optional.menu_nargs(),
175
- "choices": optional.menu_choices(),
176
- "description": optional.description,
177
- }
178
- for optional in parser.optional_arguments
179
- ],
180
- }
169
+ if values in ("json", "json-tree"):
170
+ menu = parser._menu_object(tree=values == "json-tree")
181
171
  print(json.dumps(menu))
182
172
  sys.exit(0)
183
173
 
@@ -206,11 +196,67 @@ class ParserManager:
206
196
  "--menu",
207
197
  nargs="?",
208
198
  const="text",
209
- choices=["text", "json"],
199
+ choices=["text", "json", "json-tree"],
210
200
  action=action_class,
211
- help="Print the menu for tools, as text (the default) or JSON",
201
+ help=(
202
+ "Print the menu for tools, as text (the default), JSON "
203
+ "or JSON with every subcommand's menu nested"
204
+ ),
212
205
  )
213
206
 
207
+ def _menu_object(self, tree):
208
+ """Return this parser's --menu=json object. With tree, each
209
+ command also carries its child parser's object under "menu".
210
+
211
+ """
212
+
213
+ def command_object(command):
214
+ obj = {
215
+ "name": command.name,
216
+ "aliases": list(command.aliases),
217
+ "description": command.description,
218
+ }
219
+ if tree:
220
+ obj["menu"] = command.parser._menu_object(tree)
221
+ return obj
222
+
223
+ def hints(argument):
224
+ return {
225
+ "complete": argument.complete,
226
+ "candidates": argument.menu_candidates(),
227
+ "repeatable": argument.repeatable,
228
+ }
229
+
230
+ return {
231
+ "version": 1,
232
+ "commands": [
233
+ command_object(command)
234
+ for command in self.command_arguments
235
+ ],
236
+ "positionals": [
237
+ {
238
+ "name": positional.name,
239
+ "nargs": positional.menu_nargs(),
240
+ "choices": positional.menu_choices(),
241
+ **hints(positional),
242
+ "description": positional.description,
243
+ }
244
+ for positional in self.positional_arguments
245
+ ],
246
+ "optionals": [
247
+ {
248
+ "name": optional.menu_names()[0],
249
+ "aliases": optional.menu_names()[1],
250
+ "has_value": optional.has_value,
251
+ "nargs": optional.menu_nargs(),
252
+ "choices": optional.menu_choices(),
253
+ **hints(optional),
254
+ "description": optional.description,
255
+ }
256
+ for optional in self.optional_arguments
257
+ ],
258
+ }
259
+
214
260
  def _add_subparser(self, subparser):
215
261
  """Add a subparser as a child of this parser.
216
262
 
@@ -256,12 +302,33 @@ class ParserManager:
256
302
 
257
303
  args: Pass-through to the underlying argparser parser.
258
304
 
259
- kwargs: Pass-through to the underlying argparser parser.
305
+ kwargs: Pass-through to the underlying argparser parser, except
306
+ for two keywords that only the JSON menus read.
307
+
308
+ complete: What kind of value the argument takes, for a tool's
309
+ completion: "branch", "committish", "directory", "file",
310
+ "remote" or "worktree".
311
+
312
+ candidates: Values to suggest. Unlike choices, argparse does not
313
+ enforce them.
260
314
 
261
315
  """
316
+ complete = kwargs.pop("complete", None)
317
+ candidates = kwargs.pop("candidates", None)
262
318
 
263
319
  action = self.parser.add_argument(name, *args, **kwargs)
264
320
 
321
+ # Each use of these actions adds to the value. argparse resolves
322
+ # an action string to its class, and extend subclasses append.
323
+ repeatable = isinstance(
324
+ action,
325
+ (
326
+ argparse._AppendAction,
327
+ argparse._AppendConstAction,
328
+ argparse._CountAction,
329
+ ),
330
+ )
331
+
265
332
  if name[0] in self.parser.prefix_chars:
266
333
  # argparse resolves an action by string or class. Actions
267
334
  # taking no value set nargs to 0.
@@ -273,6 +340,9 @@ class ParserManager:
273
340
  desc=kwargs.get("help", ""),
274
341
  nargs=kwargs.get("nargs"),
275
342
  choices=kwargs.get("choices"),
343
+ complete=complete,
344
+ candidates=candidates,
345
+ repeatable=repeatable,
276
346
  )
277
347
  )
278
348
  else:
@@ -284,6 +354,9 @@ class ParserManager:
284
354
  desc=kwargs.get("help", ""),
285
355
  nargs=kwargs.get("nargs"),
286
356
  choices=kwargs.get("choices"),
357
+ complete=complete,
358
+ candidates=candidates,
359
+ repeatable=repeatable,
287
360
  )
288
361
  )
289
362
 
@@ -390,6 +463,7 @@ class ParserManager:
390
463
  aliases=list(kwargs.get("aliases", [])),
391
464
  has_value=False,
392
465
  desc=kwargs.get("help", ""),
466
+ parser=parser,
393
467
  )
394
468
  )
395
469
 
@@ -367,3 +367,34 @@ def test_confobj_mapped_section_reloads(reset_directory, git):
367
367
 
368
368
  again = Reader.get(git, "fizz_bin.x", "thing", "one")
369
369
  assert again.color == "blue"
370
+
371
+
372
+ def test_confobj_iter_idents(reset_directory, git):
373
+ class Thing(git_project.ConfigObject):
374
+ pass
375
+
376
+ for subsection, ident in [
377
+ ("thing", "one"),
378
+ ("thing", "two.x"),
379
+ # A longer subsection that starts with the same name.
380
+ ("thingpath", "three"),
381
+ ]:
382
+ Thing.get(git, "fizz_bin", subsection, ident).color = "blue"
383
+
384
+ # An empty ident is legal config but names no object.
385
+ git._repo.config["fizz-bin.thing..color"] = "green"
386
+ git.reload_config()
387
+
388
+ Thing.get(git, "fizz_bin", "thing", "Mixed").color = "red"
389
+ Thing.get(git, "fizz_bin", "thing", "gone").color = "red"
390
+ Thing.get(git, "fizz_bin", "thing", "gone").rm()
391
+
392
+ idents = git_project.ConfigObject.iter_idents(git, "fizz_bin", "thing")
393
+ assert sorted(idents) == ["Mixed", "one", "two.x"]
394
+ assert all(
395
+ git_project.ConfigObject.exists(git, "fizz_bin", "thing", ident)
396
+ for ident in ["Mixed", "one", "two.x"]
397
+ )
398
+ assert not list(
399
+ git_project.ConfigObject.iter_idents(git, "fizz_bin", "other")
400
+ )
@@ -92,18 +92,27 @@ def test_menu_json(reset_directory, parser_manager, capsys):
92
92
  "name": "target",
93
93
  "nargs": None,
94
94
  "choices": None,
95
+ "complete": None,
96
+ "candidates": None,
97
+ "repeatable": False,
95
98
  "description": "The target",
96
99
  },
97
100
  {
98
101
  "name": "where",
99
102
  "nargs": "?",
100
103
  "choices": None,
104
+ "complete": None,
105
+ "candidates": None,
106
+ "repeatable": False,
101
107
  "description": "Maybe a place",
102
108
  },
103
109
  {
104
110
  "name": "mode",
105
111
  "nargs": None,
106
112
  "choices": ["fast", "slow"],
113
+ "complete": None,
114
+ "candidates": None,
115
+ "repeatable": False,
107
116
  "description": "A mode",
108
117
  },
109
118
  ],
@@ -114,6 +123,9 @@ def test_menu_json(reset_directory, parser_manager, capsys):
114
123
  "has_value": False,
115
124
  "nargs": None,
116
125
  "choices": None,
126
+ "complete": None,
127
+ "candidates": None,
128
+ "repeatable": False,
117
129
  "description": "Force it",
118
130
  },
119
131
  {
@@ -122,6 +134,9 @@ def test_menu_json(reset_directory, parser_manager, capsys):
122
134
  "has_value": True,
123
135
  "nargs": None,
124
136
  "choices": None,
137
+ "complete": None,
138
+ "candidates": None,
139
+ "repeatable": False,
125
140
  "description": "Short flag first",
126
141
  },
127
142
  {
@@ -130,6 +145,9 @@ def test_menu_json(reset_directory, parser_manager, capsys):
130
145
  "has_value": True,
131
146
  "nargs": "2",
132
147
  "choices": None,
148
+ "complete": None,
149
+ "candidates": None,
150
+ "repeatable": False,
133
151
  "description": "Two values",
134
152
  },
135
153
  ],
@@ -165,6 +183,8 @@ def test_menu_json_custom_flag_action_has_no_value(
165
183
 
166
184
  [flag] = [opt for opt in menu["optionals"] if opt["name"] == "--flag"]
167
185
  assert flag["has_value"] is False
186
+ # Shipped in version 1, so it stays.
187
+ assert flag["nargs"] == "0"
168
188
 
169
189
 
170
190
  def test_menu_json_separate_value(reset_directory, parser_manager, capsys):
@@ -195,6 +215,148 @@ def test_menu_text_unchanged(reset_directory, parser_manager, capsys, args):
195
215
  )
196
216
 
197
217
 
218
+ def add_menu_tree(parser_manager):
219
+ command = parser_manager.find_subparser("command")
220
+ first = parser_manager.add_parser(
221
+ command, "first", "first", aliases=["f"], help="First"
222
+ )
223
+ second = parser_manager.add_parser(command, "second", "second")
224
+ # The same command name under two parents, told apart by the link.
225
+ for parent in (first, second):
226
+ sub = parser_manager.add_subparser(
227
+ parent, f"{parent.key}-command", dest=f"{parent.key}_command"
228
+ )
229
+ child = parser_manager.add_parser(
230
+ sub, "add", f"{parent.key}-add", help=f"{parent.key} add"
231
+ )
232
+ child.add_argument(f"--{parent.key}-only", help="Only here")
233
+
234
+
235
+ def test_menu_json_tree(reset_directory, parser_manager, capsys):
236
+ add_menu_tree(parser_manager)
237
+
238
+ menu = json.loads(run_menu(parser_manager, capsys, ["--menu=json-tree"]))
239
+
240
+ assert menu["version"] == 1
241
+ first, second = menu["commands"]
242
+ assert first["name"] == "first"
243
+ assert first["aliases"] == ["f"]
244
+ for parent in (first, second):
245
+ [add] = parent["menu"]["commands"]
246
+ assert add["description"] == f"{parent['name']} add"
247
+ # A leaf has an empty command list, not a missing one.
248
+ assert add["menu"]["commands"] == []
249
+ assert [opt["name"] for opt in add["menu"]["optionals"]] == [
250
+ f"--{parent['name']}-only"
251
+ ]
252
+
253
+
254
+ def test_menu_json_tree_on_subcommand(reset_directory, parser_manager, capsys):
255
+ add_menu_tree(parser_manager)
256
+
257
+ menu = json.loads(
258
+ run_menu(parser_manager, capsys, ["second", "--menu=json-tree"])
259
+ )
260
+
261
+ [add] = menu["commands"]
262
+ assert add["description"] == "second add"
263
+ assert add["menu"]["commands"] == []
264
+
265
+
266
+ def test_menu_json_has_no_nested_menu(reset_directory, parser_manager, capsys):
267
+ add_menu_tree(parser_manager)
268
+
269
+ menu = json.loads(run_menu(parser_manager, capsys, ["--menu=json"]))
270
+
271
+ assert all("menu" not in command for command in menu["commands"])
272
+
273
+
274
+ def add_hinted_arguments(parser_manager):
275
+ command = parser_manager.find_subparser("command")
276
+ main = parser_manager.add_parser(command, "hinted", "hinted")
277
+ main.add_argument(
278
+ "name",
279
+ complete="worktree",
280
+ candidates=["one", 2],
281
+ help="A worktree",
282
+ )
283
+ main.add_argument("--branch", complete="branch", help="A branch")
284
+
285
+
286
+ @pytest.mark.parametrize("values", ["json", "json-tree"])
287
+ def test_menu_json_hints(reset_directory, parser_manager, capsys, values):
288
+ add_hinted_arguments(parser_manager)
289
+
290
+ menu = json.loads(
291
+ run_menu(parser_manager, capsys, ["hinted", f"--menu={values}"])
292
+ )
293
+
294
+ [name] = menu["positionals"]
295
+ assert name["complete"] == "worktree"
296
+ assert name["candidates"] == ["one", "2"]
297
+ [branch] = [opt for opt in menu["optionals"] if opt["name"] == "--branch"]
298
+ assert branch["complete"] == "branch"
299
+ assert branch["candidates"] is None
300
+
301
+
302
+ def test_menu_hints_are_not_argparse_arguments(
303
+ reset_directory, parser_manager, capsys
304
+ ):
305
+ add_hinted_arguments(parser_manager)
306
+
307
+ # candidates suggests values but does not restrict them.
308
+ clargs = parser_manager.parse_args(["hinted", "three", "--branch", "b"])
309
+ assert clargs.name == "three"
310
+ assert not hasattr(clargs, "complete")
311
+ assert not hasattr(clargs, "candidates")
312
+
313
+ usage = run_menu(parser_manager, capsys, ["hinted", "-h"])
314
+ assert "worktree" not in usage.replace("A worktree", "")
315
+ assert "one" not in usage
316
+
317
+
318
+ class _CollectAction(argparse._AppendAction):
319
+ pass
320
+
321
+
322
+ @pytest.mark.parametrize(
323
+ ("kwargs", "repeatable"),
324
+ [
325
+ ({"action": "append"}, True),
326
+ ({"action": argparse._AppendAction}, True),
327
+ ({"action": argparse._AppendConstAction, "const": 1}, True),
328
+ ({"action": argparse._ExtendAction, "nargs": "+"}, True),
329
+ ({"action": argparse._CountAction}, True),
330
+ ({"action": _CollectAction}, True),
331
+ ({"action": "append_const", "const": 1}, True),
332
+ ({"action": "extend", "nargs": "+"}, True),
333
+ ({"action": "count"}, True),
334
+ ({"action": "store_true"}, False),
335
+ ({}, False),
336
+ ],
337
+ )
338
+ def test_menu_json_repeatable(
339
+ reset_directory, parser_manager, capsys, kwargs, repeatable
340
+ ):
341
+ main = parser_manager.find_parser("__main__")
342
+ main.add_argument("--opt", help="An option", **kwargs)
343
+
344
+ menu = json.loads(run_menu(parser_manager, capsys, ["--menu=json"]))
345
+
346
+ [opt] = [opt for opt in menu["optionals"] if opt["name"] == "--opt"]
347
+ assert opt["repeatable"] is repeatable
348
+
349
+
350
+ def test_menu_json_nargs(reset_directory, parser_manager, capsys):
351
+ main = parser_manager.find_parser("__main__")
352
+ main.add_argument("pair", nargs=2, help="Two values")
353
+ main.add_argument("rest", nargs=argparse.REMAINDER, help="The rest")
354
+
355
+ menu = json.loads(run_menu(parser_manager, capsys, ["--menu=json"]))
356
+
357
+ assert [pos["nargs"] for pos in menu["positionals"]] == ["2", "..."]
358
+
359
+
198
360
  def test_menu_rejects_other_values(reset_directory, parser_manager):
199
361
  with pytest.raises(SystemExit) as exit_info:
200
362
  parser_manager.parse_args(["--menu", "worktree"])
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes