Deal with extra features provided by cmd2, this is an outer wrapper around _cmdloop(). _cmdloop() provides the main loop. This provides the following extra features provided by cmd2: - intro banner - exit code :param intro: if provided this overrides self.intro and
(self, intro: RenderableType = "")
| 5738 | return Completions() |
| 5739 | |
| 5740 | def cmdloop(self, intro: RenderableType = "") -> int: |
| 5741 | """Deal with extra features provided by cmd2, this is an outer wrapper around _cmdloop(). |
| 5742 | |
| 5743 | _cmdloop() provides the main loop. This provides the following extra features provided by cmd2: |
| 5744 | - intro banner |
| 5745 | - exit code |
| 5746 | |
| 5747 | :param intro: if provided this overrides self.intro and serves as the intro banner printed once at start |
| 5748 | :return: exit code |
| 5749 | """ |
| 5750 | # cmdloop() expects to be run in the main thread to support extensive use of KeyboardInterrupts throughout the |
| 5751 | # other built-in functions. You are free to override cmdloop, but much of cmd2's features will be limited. |
| 5752 | if threading.current_thread() is not threading.main_thread(): |
| 5753 | raise RuntimeError("cmdloop must be run in the main thread") |
| 5754 | |
| 5755 | # Register signal handlers |
| 5756 | import signal |
| 5757 | |
| 5758 | original_sigint_handler = signal.getsignal(signal.SIGINT) |
| 5759 | signal.signal(signal.SIGINT, self.sigint_handler) |
| 5760 | |
| 5761 | if not sys.platform.startswith("win"): |
| 5762 | original_sighup_handler = signal.getsignal(signal.SIGHUP) |
| 5763 | signal.signal(signal.SIGHUP, self.termination_signal_handler) |
| 5764 | |
| 5765 | original_sigterm_handler = signal.getsignal(signal.SIGTERM) |
| 5766 | signal.signal(signal.SIGTERM, self.termination_signal_handler) |
| 5767 | |
| 5768 | # Always run the preloop first |
| 5769 | for func in self._preloop_hooks: |
| 5770 | func() |
| 5771 | self.preloop() |
| 5772 | |
| 5773 | # If an intro was supplied in the method call, allow it to override the default |
| 5774 | if intro: |
| 5775 | self.intro = intro |
| 5776 | |
| 5777 | # Print the intro, if there is one, right after the preloop |
| 5778 | if self.intro: |
| 5779 | self.poutput(self.intro) |
| 5780 | |
| 5781 | # And then call _cmdloop() to enter the main loop |
| 5782 | self._cmdloop() |
| 5783 | |
| 5784 | # Run the postloop() no matter what |
| 5785 | for func in self._postloop_hooks: |
| 5786 | func() |
| 5787 | self.postloop() |
| 5788 | |
| 5789 | # Restore original signal handlers |
| 5790 | signal.signal(signal.SIGINT, original_sigint_handler) |
| 5791 | |
| 5792 | if not sys.platform.startswith("win"): |
| 5793 | signal.signal(signal.SIGHUP, original_sighup_handler) |
| 5794 | signal.signal(signal.SIGTERM, original_sigterm_handler) |
| 5795 | |
| 5796 | return self.exit_code |
| 5797 |