| 742 | |
| 743 | |
| 744 | class Command(CliCommand): |
| 745 | def __init__(self): |
| 746 | super(Command, self).__init__() |
| 747 | self.extra_parameters = '' |
| 748 | |
| 749 | def execute(self, params, **kwargs): # type: (KeeperParams, Any) -> Any |
| 750 | raise NotImplementedError() |
| 751 | |
| 752 | def execute_args(self, params, args, **kwargs): |
| 753 | # type: (Command, KeeperParams, str, ...) -> Any |
| 754 | |
| 755 | global parameter_pattern |
| 756 | try: |
| 757 | d = {} |
| 758 | d.update(kwargs) |
| 759 | self.extra_parameters = '' |
| 760 | parser = self._get_parser_safe() |
| 761 | envvars = params.environment_variables |
| 762 | args = '' if args is None else args |
| 763 | if parser: |
| 764 | # Update prog to match alias name (e.g., 'find-password' instead of 'clipboard-copy') |
| 765 | alias_cmd = d.get('command') |
| 766 | if alias_cmd and alias_cmd != parser.prog: |
| 767 | parser.prog = alias_cmd |
| 768 | args = expand_cmd_args(args, envvars) |
| 769 | args = normalize_output_param(args) |
| 770 | if self.support_extra_parameters(): |
| 771 | opts, extra_args = parser.parse_known_args(shlex.split(args)) |
| 772 | if extra_args: |
| 773 | self.extra_parameters = ' '.join(extra_args) |
| 774 | else: |
| 775 | opts = parser.parse_args(shlex.split(args)) |
| 776 | d.update(opts.__dict__) |
| 777 | |
| 778 | return self.execute(params, **d) |
| 779 | except ParseError as e: |
| 780 | logging.error(e) |
| 781 | |
| 782 | def support_extra_parameters(self): # type: () -> bool |
| 783 | return False |
| 784 | |
| 785 | def get_parser(self): # type: () -> Optional[argparse.ArgumentParser] |
| 786 | return None |
| 787 | |
| 788 | def _ensure_parser(func): |
| 789 | def _wrapper(self): |
| 790 | parser = func(self) |
| 791 | if parser: |
| 792 | if parser.exit != suppress_exit: |
| 793 | parser.exit = suppress_exit |
| 794 | if parser.error != raise_parse_exception: |
| 795 | parser.error = raise_parse_exception |
| 796 | return parser |
| 797 | return _wrapper |
| 798 | |
| 799 | @_ensure_parser |
| 800 | def _get_parser_safe(self): |
| 801 | return self.get_parser() |
nothing calls this directly
no outgoing calls
no test coverage detected