1111from typing import Annotated , Optional , Sequence , Type , Union
1212from unittest .mock import patch
1313
14- from .cli_flags import CliFlags
1514
1615from ..cli import Command
1716from ..settings import CliSettings
3332from .form_dict import EnvClass , TagDict , dataclass_to_tagdict , MissingTagValue , dict_added_main
3433
3534try :
35+ from .cli_flags import CliFlags
3636 from tyro import cli
37- from tyro ._argparse import _SubParsersAction , ArgumentParser
38- from tyro ._argparse_formatter import TyroArgumentParser
39- from tyro ._singleton import MISSING_NONPROP
37+ try : # tyro >= 0.10
38+ from tyro import _experimental_options
39+ _experimental_options ["backend" ] = "argparse"
40+ from tyro ._backends ._argparse import _SubParsersAction , ArgumentParser
41+ from tyro ._backends ._argparse_formatter import TyroArgumentParser
42+ except ImportError :
43+ from tyro ._argparse import _SubParsersAction , ArgumentParser
44+ from tyro ._argparse_formatter import TyroArgumentParser
45+ from tyro ._parsers import ParserSpecification
46+
4047 from tyro .conf import OmitArgPrefixes , OmitSubcommandPrefixes , DisallowNone , FlagCreatePairsOff
4148
49+
4250 from .tyro_patches import (
4351 _crawling ,
4452 custom_error ,
4553 custom_init ,
4654 custom_parse_known_args ,
4755 failed_fields ,
48- patched_parse_known_args ,
56+ patched__parse_known_args ,
57+ patched__format_help ,
4958 subparser_call ,
50- argparse_init ,
59+ argparse_init
5160 )
5261except ImportError :
5362 from ..exceptions import DependencyRequired
@@ -183,6 +192,7 @@ def annot(type_form):
183192 helponly = False
184193 try :
185194 # Why redirect_stdout? Help-text shows the defaults, which also uses the subcommanded-config.
195+ # TODO maybe new tyro 0.10 will not output to stdout, get rid of the buffer
186196 with redirect_stdout (buffer ):
187197 try :
188198 # Standard way.
@@ -234,16 +244,16 @@ def annot(type_form):
234244 kwargs , None if helponly else m , args , type_form , env_classes , _custom_registry , annot , _req_fields
235245 )
236246
237- # Why setting m.env instead of putting into into a constructor of a new get_interface() call?
238- # 1. Getting the interface is a costly operation
239- # 2. There is this bug so that we need to use single interface:
240- # TODO
241- # As this works badly, lets make sure we use single interface now
242- # and will not need the second one.
243- # get_interface("gui")
244- # m = get_interface("gui")
245- # m.select([1,2,3])
246- m .env = env
247+ # Why setting m.env instead of putting into into a constructor of a new get_interface() call?
248+ # 1. Getting the interface is a costly operation
249+ # 2. There is this bug so that we need to use single interface:
250+ # TODO
251+ # As this works badly, lets make sure we use single interface now
252+ # and will not need the second one.
253+ # get_interface("gui")
254+ # m = get_interface("gui")
255+ # m.select([1,2,3])
256+ m .env = env
247257 except SystemExit as exception :
248258 # --- (C) The dialog missing section ---
249259 # Some fields are needed to be filled up.
@@ -339,8 +349,7 @@ def _apply_patches(cf: Optional[CliFlags], ask_for_missing, env_classes, kwargs)
339349 patches = []
340350
341351 patches .append (patch .object (_SubParsersAction , "__call__" , subparser_call ))
342- patches .append (patch .object (TyroArgumentParser , "_parse_known_args" , patched_parse_known_args ))
343-
352+ patches .append (patch .object (TyroArgumentParser , "_parse_known_args" , patched__parse_known_args ))
344353 kw = {
345354 k : v for k , v in kwargs .items () if k != "default"
346355 } # NOTE I might separate kwargs['default'] and do not do this filtering
@@ -359,6 +368,11 @@ def _apply_patches(cf: Optional[CliFlags], ask_for_missing, env_classes, kwargs)
359368 "__init__" ,
360369 custom_init (cf ),
361370 ),
371+ patch .object (
372+ TyroArgumentParser ,
373+ "format_help" ,
374+ patched__format_help (cf ),
375+ ),
362376 patch .object (
363377 TyroArgumentParser ,
364378 "parse_known_args" ,
@@ -486,7 +500,7 @@ def _fetch_currently_failed(requireds) -> TagDict:
486500 missing_req = {}
487501 for field in failed_fields .get ():
488502 # ex: `_subcommands._nested_subcommands (positional)`
489- fname = field .dest .replace (" (positional)" , "" ).replace ("-" , "_" ) # `_subcommands._nested_subcommands`
503+ fname = field .dest .replace (" (positional)" , "" ).replace ("-" , "_" ). replace ( "__tyro_dummy_inner__." , "" ). replace ( "__tyro_dummy_inner__" , "" ) # `_subcommands._nested_subcommands`
490504 fname_raw = fname .rsplit ("." , 1 )[- 1 ] # `_nested_subcommands`
491505
492506 if isinstance (field , _SubParsersAction ):
0 commit comments