MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / LoadableBase

Class LoadableBase

tests/test_commandset.py:390–447  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

388
389
390class LoadableBase(cmd2.CommandSet):
391 def __init__(self, dummy) -> None:
392 super().__init__()
393 self._dummy = dummy # prevents autoload
394 self._cut_called = False
395
396 cut_parser = cmd2.Cmd2ArgumentParser()
397 cut_subparsers = cut_parser.add_subparsers(title="item", help="item to cut")
398
399 def namespace_provider(self) -> argparse.Namespace:
400 ns = argparse.Namespace()
401 ns.cut_called = self._cut_called
402 return ns
403
404 @cmd2.with_argparser(cut_parser)
405 def do_cut(self, ns: argparse.Namespace) -> None:
406 """Cut something"""
407 handler = ns.cmd2_subcmd_handler
408 if handler is not None:
409 # Call whatever subcommand function was selected
410 handler(ns)
411 self._cut_called = True
412 else:
413 # No subcommand was provided, so call help
414 self._cmd.pwarning("This command does nothing without sub-parsers registered")
415 self._cmd.do_help("cut")
416
417 stir_parser = cmd2.Cmd2ArgumentParser()
418 stir_subparsers = stir_parser.add_subparsers(title="item", help="what to stir")
419
420 @cmd2.with_argparser(stir_parser, ns_provider=namespace_provider)
421 def do_stir(self, ns: argparse.Namespace) -> None:
422 """Stir something"""
423 if not ns.cut_called:
424 self._cmd.poutput("Need to cut before stirring")
425 return
426
427 handler = ns.cmd2_subcmd_handler
428 if handler is not None:
429 # Call whatever subcommand function was selected
430 handler(ns)
431 else:
432 # No subcommand was provided, so call help
433 self._cmd.pwarning("This command does nothing without sub-parsers registered")
434 self._cmd.do_help("stir")
435
436 stir_pasta_parser = cmd2.Cmd2ArgumentParser()
437 stir_pasta_parser.add_argument("--option", "-o")
438 stir_pasta_parser.add_subparsers(title="style", help="Stir style")
439
440 @cmd2.as_subcommand_to("stir", "pasta", stir_pasta_parser)
441 def stir_pasta(self, ns: argparse.Namespace) -> None:
442 handler = ns.cmd2_subcmd_handler
443 if handler is not None:
444 # Call whatever subcommand function was selected
445 handler(ns)
446 else:
447 self._cmd.poutput("Stir pasta haphazardly")

Callers 2

test_subcommandsFunction · 0.85
test_nested_subcommandsFunction · 0.85

Calls

no outgoing calls

Tested by 2

test_subcommandsFunction · 0.68
test_nested_subcommandsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…