Skip to content

Commit

Permalink
Helptext cleanup, version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Nov 20, 2022
1 parent fd13815 commit 31f8b27
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
13 changes: 11 additions & 2 deletions docs/source/examples/02_nesting/03_multiple_subcommands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Multiple unions over nested types are populated using a series of subcommands.
# Train script.
@tyro.conf.configure(tyro.conf.ConsolidateSubcommandArgs)
def train(
dataset: Union[Mnist, ImageNet] = Mnist(),
optimizer: Union[Adam, Sgd] = Adam(),
Expand Down Expand Up @@ -100,6 +101,14 @@ Multiple unions over nested types are populated using a series of subcommands.

.. raw:: html

<kbd>python 02_nesting/03_multiple_subcommands.py dataset:mnist optimizer:adam --optimizer.learning-rate 3e-4</kbd>
<kbd>python 02_nesting/03_multiple_subcommands.py dataset:mnist optimizer:adam --help</kbd>

.. program-output:: python ../../examples/02_nesting/03_multiple_subcommands.py dataset:mnist optimizer:adam --optimizer.learning-rate 3e-4
.. program-output:: python ../../examples/02_nesting/03_multiple_subcommands.py dataset:mnist optimizer:adam --help

------------

.. raw:: html

<kbd>python 02_nesting/03_multiple_subcommands.py dataset:mnist optimizer:adam --optimizer.learning-rate 3e-4 --dataset.binary</kbd>

.. program-output:: python ../../examples/02_nesting/03_multiple_subcommands.py dataset:mnist optimizer:adam --optimizer.learning-rate 3e-4 --dataset.binary
4 changes: 3 additions & 1 deletion examples/02_nesting/03_multiple_subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
`python ./03_multiple_subcommands.py`
`python ./03_multiple_subcommands.py --help`
`python ./03_multiple_subcommands.py dataset:mnist --help`
`python ./03_multiple_subcommands.py dataset:mnist optimizer:adam --optimizer.learning-rate 3e-4`
`python ./03_multiple_subcommands.py dataset:mnist optimizer:adam --help`
`python ./03_multiple_subcommands.py dataset:mnist optimizer:adam --optimizer.learning-rate 3e-4 --dataset.binary`
"""
from __future__ import annotations

Expand Down Expand Up @@ -47,6 +48,7 @@ class Sgd:
# Train script.


@tyro.conf.configure(tyro.conf.ConsolidateSubcommandArgs)
def train(
dataset: Union[Mnist, ImageNet] = Mnist(),
optimizer: Union[Adam, Sgd] = Adam(),
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "tyro"
version = "0.3.33"
version = "0.3.34"
description = "Strongly typed, zero-effort CLI interfaces"
authors = ["brentyi <[email protected]>"]
include = ["./tyro/**/*"]
Expand Down
18 changes: 11 additions & 7 deletions tyro/_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ def apply(
else:
self.apply_args(parser)

# Break some API boundaries to rename the "optional arguments" => "arguments".
parser._action_groups[1].title = "arguments"

return leaves

def apply_args(self, parser: argparse.ArgumentParser) -> None:
Expand All @@ -250,17 +253,18 @@ def apply_args(self, parser: argparse.ArgumentParser) -> None:
parser.description = self.description

# Make argument groups.
def format_group_name(nested_field_name: str) -> str:
return (nested_field_name + " arguments").strip()
def format_group_name(prefix: str) -> str:
return (prefix + " arguments").strip()

group_from_prefix: Dict[str, argparse._ArgumentGroup] = {
"": parser._action_groups[1],
**{
cast(str, group.title).partition(" ")[0]: group
for group in parser._action_groups[2:]
},
}

# Break some API boundaries to rename the optional group.
parser._action_groups[1].title = format_group_name("")
positional_group = parser.add_argument_group("positional arguments")
parser._action_groups = parser._action_groups[::-1]
positional_group = parser._action_groups[0]
assert positional_group.title == "positional arguments"

# Add each argument group. Note that groups with only suppressed arguments won't
# be added.
Expand Down

0 comments on commit 31f8b27

Please sign in to comment.