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

Method do_set

cmd2/cmd2.py:4677–4730  ·  view source on GitHub ↗

Set a settable parameter or show current settings of parameters.

(self, args: argparse.Namespace)

Source from the content-addressed store, hash-verified

4675 # Preserve quotes so users can pass in quoted empty strings and flags (e.g. -h) as the value
4676 @with_argparser(_build_set_parser, preserve_quotes=True)
4677 def do_set(self, args: argparse.Namespace) -> None:
4678 """Set a settable parameter or show current settings of parameters."""
4679 self.last_result = False
4680
4681 if not self.settables:
4682 self.pwarning("There are no settable parameters")
4683 return
4684
4685 if args.param:
4686 try:
4687 settable = self.settables[args.param]
4688 except KeyError:
4689 self.perror(f"Parameter '{args.param}' not supported (type 'set' for list of parameters).")
4690 return
4691
4692 if args.value:
4693 # Try to update the settable's value
4694 try:
4695 orig_value = settable.value
4696 settable.value = su.strip_quotes(args.value)
4697 except ValueError as ex:
4698 self.perror(f"Error setting {args.param}: {ex}")
4699 else:
4700 self.poutput(f"{args.param} - was: {orig_value!r}\nnow: {settable.value!r}")
4701 self.last_result = True
4702 return
4703
4704 # Show one settable
4705 to_show: list[str] = [args.param]
4706 else:
4707 # Show all settables
4708 to_show = list(self.settables.keys())
4709
4710 settable_table = Cmd2SimpleTable(
4711 Column("Name", no_wrap=True),
4712 Column("Value", overflow="fold"),
4713 Column("Description", overflow="fold"),
4714 )
4715
4716 # Build the table and populate self.last_result
4717 self.last_result = {} # dict[settable_name, settable_value]
4718
4719 for param in sorted(to_show, key=utils.DEFAULT_STR_SORT_KEY):
4720 settable = self.settables[param]
4721 settable_table.add_row(
4722 param,
4723 str(settable.value),
4724 Text.from_ansi(settable.description),
4725 )
4726 self.last_result[param] = settable.value
4727
4728 self.poutput()
4729 self.poutput(settable_table, soft_wrap=False)
4730 self.poutput()
4731
4732 @classmethod
4733 def _build_shell_parser(cls) -> Cmd2ArgumentParser:

Callers

nothing calls this directly

Calls 4

pwarningMethod · 0.95
perrorMethod · 0.95
poutputMethod · 0.95
Cmd2SimpleTableClass · 0.85

Tested by

no test coverage detected