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

Method _alias_create

cmd2/cmd2.py:3791–3823  ·  view source on GitHub ↗

Create or overwrite an alias.

(self, args: argparse.Namespace)

Source from the content-addressed store, hash-verified

3789
3790 @as_subcommand_to("alias", "create", _build_alias_create_parser, help="create or overwrite an alias")
3791 def _alias_create(self, args: argparse.Namespace) -> None:
3792 """Create or overwrite an alias."""
3793 self.last_result = False
3794
3795 # Validate the alias name
3796 valid, errmsg = self.statement_parser.is_valid_command(args.name)
3797 if not valid:
3798 self.perror(f"Invalid alias name: {errmsg}")
3799 return
3800
3801 if args.name in self.get_all_commands():
3802 self.perror("Alias cannot have the same name as a command")
3803 return
3804
3805 if args.name in self.macros:
3806 self.perror("Alias cannot have the same name as a macro")
3807 return
3808
3809 # Unquote redirection and terminator tokens
3810 tokens_to_unquote = (*constants.REDIRECTION_TOKENS, *self.statement_parser.terminators)
3811 utils.unquote_specific_tokens(args.command_args, tokens_to_unquote)
3812
3813 # Build the alias value string
3814 value = args.command
3815 if args.command_args:
3816 value += " " + " ".join(args.command_args)
3817
3818 # Set the alias
3819 result = "overwritten" if args.name in self.aliases else "created"
3820 self.poutput(f"Alias '{args.name}' {result}")
3821
3822 self.aliases[args.name] = value
3823 self.last_result = True
3824
3825 # alias -> delete
3826 @classmethod

Callers

nothing calls this directly

Calls 4

perrorMethod · 0.95
get_all_commandsMethod · 0.95
poutputMethod · 0.95
is_valid_commandMethod · 0.80

Tested by

no test coverage detected