Main command handling loop
(self, debug: int = 0)
| 3812 | return CLICompleter() |
| 3813 | |
| 3814 | def loop(self, debug: int = 0) -> None: |
| 3815 | """ |
| 3816 | Main command handling loop |
| 3817 | """ |
| 3818 | from prompt_toolkit import PromptSession |
| 3819 | session = PromptSession(completer=self._completer()) |
| 3820 | |
| 3821 | while True: |
| 3822 | try: |
| 3823 | cmd = session.prompt(self.ps1()).strip() |
| 3824 | except KeyboardInterrupt: |
| 3825 | continue |
| 3826 | except EOFError: |
| 3827 | self.close() |
| 3828 | break |
| 3829 | args = cmd.split(" ")[1:] |
| 3830 | cmd = cmd.split(" ")[0].strip().lower() |
| 3831 | if not cmd: |
| 3832 | continue |
| 3833 | if cmd in ["help", "h", "?"]: |
| 3834 | self.help(" ".join(args)) |
| 3835 | continue |
| 3836 | if cmd in "exit": |
| 3837 | break |
| 3838 | if cmd not in self.commands: |
| 3839 | print("Unknown command. Type help or ?") |
| 3840 | else: |
| 3841 | # check the number of arguments |
| 3842 | func = self.commands[cmd] |
| 3843 | args, kwargs, outkwargs = self._parseallargs(func, cmd, args) |
| 3844 | if func._spaces: # type: ignore |
| 3845 | args = [" ".join(args)] |
| 3846 | # if globsupport is set, we might need to do several calls |
| 3847 | if func._globsupport and "*" in args[0]: # type: ignore |
| 3848 | if args[0].count("*") > 1: |
| 3849 | print("More than 1 glob star (*) is currently unsupported.") |
| 3850 | continue |
| 3851 | before, after = args[0].split("*", 1) |
| 3852 | reg = re.compile(re.escape(before) + r".*" + after) |
| 3853 | calls = [ |
| 3854 | [x] for x in |
| 3855 | self.commands_complete[cmd](self, before) |
| 3856 | if reg.match(x) |
| 3857 | ] |
| 3858 | else: |
| 3859 | calls = [args] |
| 3860 | else: |
| 3861 | calls = [args] |
| 3862 | # now iterate if required, call the function and print its output |
| 3863 | res = None |
| 3864 | for args in calls: |
| 3865 | try: |
| 3866 | res = func(self, *args, **kwargs) |
| 3867 | except TypeError: |
| 3868 | print("Bad number of arguments !") |
| 3869 | self.help(cmd=cmd) |
| 3870 | continue |
| 3871 | except Exception as ex: |
no test coverage detected