22import pydoc
33import shlex
44import sys
5- from argparse import Action , ArgumentParser
5+ import warnings
6+ from abc import ABCMeta
7+ from argparse import _StoreAction
68from asyncio import iscoroutine
79from collections import defaultdict
810from subprocess import run
1315 Coroutine ,
1416 Dict ,
1517 List ,
16- Literal ,
1718 Optional ,
1819 Sequence ,
1920 Set ,
2324 Union ,
2425 cast ,
2526)
26- import warnings
2727
2828from prompt_toolkit .completion import Completer , NestedCompleter
2929from prompt_toolkit .formatted_text import ANSI , is_formatted_text
4242
4343from .argument import Arg
4444from .command import auto_argument
45- from .completer import ArgparseCompleter , MultiPrefixCompleter
46- from .info import CommandInfo , CommandLike , build_cmd_info , set_info
45+ from .completer import MultiPrefixCompleter
46+ from .info import CommandInfo , CommandLike , build_cmd_info , set_info , get_cmd_ins
4747from .theme import DEFAULT as THEME
4848
4949
@@ -64,7 +64,7 @@ async def _ensure_coroutine(coro: Union[Coroutine[Any, Any, _T], _T]) -> _T:
6464 return coro
6565
6666
67- class BaseCmd (object ):
67+ class BaseCmd (object , metaclass = ABCMeta ):
6868 """Base class for command line interfaces in ptcmd.
6969
7070 This class provides the core functionality for building interactive command-line
@@ -492,36 +492,17 @@ def __init_subclass__(cls, **kwds: Any) -> None:
492492
493493
494494
495- class _TopicAction (Action ):
496- def __init__ (
497- self ,
498- option_strings : Sequence [str ],
499- dest : str ,
500- nargs : Optional [Literal [1 ]] = None ,
501- default : None = None ,
502- type : None = None ,
503- choices : None = None ,
504- required : bool = False ,
505- help : Optional [str ] = None ,
506- metavar : Union [str , Tuple [str ], None ] = None ,
507- cmd : Optional ["Cmd" ] = None ,
508- ) -> None :
509- self .option_strings = option_strings
510- self .dest = dest
511- self .nargs = nargs
512- self .const = None
513- self .default = default
514- self .type = type
515- self .required = required
516- self .help = help
517- self .metavar = metavar
518- self .cmd = cmd
519-
495+ class _TopicAction (_StoreAction ):
520496 @property
521497 def choices (self ) -> Optional [Sequence [str ]]:
522- if self .cmd is None : # pragma: no cover
498+ cmd = get_cmd_ins (self )
499+ if cmd is None : # pragma: no cover
523500 return
524- return self .cmd .get_visible_commands ()
501+ return cmd .get_visible_commands ()
502+
503+ @choices .setter
504+ def choices (self , _ : Any ) -> None :
505+ pass
525506
526507
527508class Cmd (BaseCmd ):
@@ -604,7 +585,12 @@ def __init__(
604585 self .nohelp = nohelp
605586
606587 @auto_argument
607- def do_help (self , topic : str = "" , * , verbose : Arg [bool , "-v" , "--verbose" ] = False ) -> None : # noqa: F821,B002
588+ def do_help (
589+ self ,
590+ topic : Arg [Optional [str ], {"action" : _TopicAction , "help" : "Command or topic for help" }] = None , # noqa: F821,F722,B002
591+ * ,
592+ verbose : Arg [bool , "-v" , "--verbose" , {"help" : "Show more detailed help" }] = False # noqa: F821,F722,B002
593+ ) -> None :
608594 """List available commands or provide detailed help for a specific command.
609595
610596 :param topic: Command or topic for which to get help, defaults to ""
@@ -622,13 +608,6 @@ def do_help(self, topic: str = "", *, verbose: Arg[bool, "-v", "--verbose"] = Fa
622608 return self .perror (f"Unknown command: { topic } " )
623609 return self .poutput (self ._format_help_text (self .command_info [topic ], verbose ))
624610
625- @do_help .completer_getter
626- def _help_completer (self ) -> Completer :
627- parser = ArgumentParser ("help" , description = self .do_help .__doc__ )
628- parser .add_argument ("topic" , nargs = "?" , help = "Command or topic for help" , action = _TopicAction , cmd = self )
629- parser .add_argument ("-v" , "--verbose" , action = "store_true" , help = "Show more detailed help" )
630- return ArgparseCompleter (parser )
631-
632611 def _help_menu (self , verbose : bool = False ) -> None :
633612 """Display the help menu showing available commands and help topics.
634613
0 commit comments