Last active
December 24, 2021 11:06
-
-
Save ivangeorgiev/8cba5b37bdd83c63c31353fcc0516ac5 to your computer and use it in GitHub Desktop.
Python interactive shell with cmd module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cmd | |
| class BasicShell(cmd.Cmd): | |
| """Basic shell | |
| To create interactive shell, subclass the cmd.Cmd class. | |
| To start the shell, call the `cmdloop()` method, implemented in the base class. | |
| """ | |
| def do_greet(self, argline): | |
| """Greet the user. | |
| Commands are implemented as methods. Method name is prefixed with "do_". | |
| The command method documentation (the docstring) is used by `Cmd` to provide help to the user. | |
| """ | |
| if argline: | |
| print(f"Hello, {argline}!") | |
| else: | |
| print("Hello, stranger!") | |
| def do_bye(self, argline): | |
| """Exit the shell. | |
| To indicate that the session should finish, the command returns True. | |
| """ | |
| print("Bye!") | |
| return True | |
| if __name__ == "__main__": | |
| BasicShell().cmdloop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cmd | |
| import os | |
| class BasicShell(cmd.Cmd): | |
| """Interactive shell example.""" | |
| intro = "Welcome to BasicShell! For help type `?` or `help`.\n" | |
| prompt = "> " | |
| def do_greet(self, argline): | |
| """Greet the user.""" | |
| if argline: | |
| print(f"Hello, {argline}!") | |
| else: | |
| print("Hello, stranger!") | |
| def do_bye(self, argline): | |
| """Exit the shell.""" | |
| print("Bye!") | |
| return True | |
| def do_EOF(self, argline): | |
| """Exit the shell.""" | |
| return self.do_bye(argline) | |
| def do_eval(self, argline): | |
| """Evaluate given expression and print the result.""" | |
| try: | |
| print(eval(argline)) | |
| except Exception as error: | |
| print(f"ERROR EVALUATING {argline}: {error}") | |
| def do_shell(self, argline): | |
| """Execute a shell command and print the output.""" | |
| output = os.popen(argline).read() | |
| print(output) | |
| def emptyline(self): | |
| """Re-execute the last command""" | |
| print(f"REPEAT: {self.lastcmd}") | |
| super().emptyline() | |
| if __name__ == "__main__": | |
| BasicShell().cmdloop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cmd | |
| class BasicShell(cmd.Cmd): | |
| """Interactive shell example.""" | |
| prompt = "> " | |
| def do_greet(self, argline): | |
| """Greet the user.""" | |
| if argline: | |
| print(f"Hello, {argline}!") | |
| else: | |
| print("Hello, stranger!") | |
| def do_bye(self, argline): | |
| """Exit the shell.""" | |
| print("Bye!") | |
| return True | |
| def do_eval(self, argline): | |
| """Evaluate given expression and print the result.""" | |
| try: | |
| print(eval(argline)) | |
| except Exception as error: | |
| print(f"ERROR EVALUATING {argline}: {error}") | |
| if __name__ == "__main__": | |
| BasicShell().cmdloop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cmd | |
| class BasicShell(cmd.Cmd): | |
| prompt = "> " | |
| def do_greet(self, argline): | |
| """Greet the user. | |
| Commands are implemented as methods. Method name is prefixed with "do_". | |
| The command method documentation (the docstring) is used by `Cmd` to provide help to the user. | |
| """ | |
| if argline: | |
| print(f"Hello, {argline}!") | |
| else: | |
| print("Hello, stranger!") | |
| def do_bye(self, argline): | |
| """Exit the shell. | |
| To indicate that the session should finish, the command returns True. | |
| """ | |
| print("Bye!") | |
| return True | |
| if __name__ == "__main__": | |
| BasicShell().cmdloop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cmd | |
| class BasicShell(cmd.Cmd): | |
| """Interactive shell example.""" | |
| intro = "Welcome to BasicShell! For help type `?` or `help`.\n" | |
| prompt = "> " | |
| def do_greet(self, argline): | |
| """Greet the user.""" | |
| if argline: | |
| print(f"Hello, {argline}!") | |
| else: | |
| print("Hello, stranger!") | |
| def do_bye(self, argline): | |
| """Exit the shell.""" | |
| print("Bye!") | |
| return True | |
| def do_eval(self, argline): | |
| """Evaluate given expression and print the result.""" | |
| try: | |
| print(eval(argline)) | |
| except Exception as error: | |
| print(f"ERROR EVALUATING {argline}: {error}") | |
| def emptyline(self): | |
| """Re-execute the last command""" | |
| print(f"REPEAT: {self.lastcmd}") | |
| super().emptyline() | |
| if __name__ == "__main__": | |
| BasicShell().cmdloop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cmd | |
| import os | |
| class BasicShell(cmd.Cmd): | |
| """Interactive shell example.""" | |
| intro = "Welcome to BasicShell! For help type `?` or `help`.\n" | |
| prompt = "> " | |
| def do_greet(self, argline): | |
| """Greet the user.""" | |
| if argline: | |
| print(f"Hello, {argline}!") | |
| else: | |
| print("Hello, stranger!") | |
| def do_bye(self, argline): | |
| """Exit the shell.""" | |
| print("Bye!") | |
| return True | |
| def do_eval(self, argline): | |
| """Evaluate given expression and print the result.""" | |
| try: | |
| print(eval(argline)) | |
| except Exception as error: | |
| print(f"ERROR EVALUATING {argline}: {error}") | |
| def do_shell(self, argline): | |
| """Execute a shell command and print the output.""" | |
| output = os.popen(argline).read() | |
| print(output) | |
| def emptyline(self): | |
| """Re-execute the last command""" | |
| print(f"REPEAT: {self.lastcmd}") | |
| super().emptyline() | |
| if __name__ == "__main__": | |
| BasicShell().cmdloop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cmd | |
| class BasicShell(cmd.Cmd): | |
| """Interactive shell example.""" | |
| intro = "Welcome to BasicShell! For help type `?` or `help`.\n" | |
| prompt = "> " | |
| def do_greet(self, argline): | |
| """Greet the user.""" | |
| if argline: | |
| print(f"Hello, {argline}!") | |
| else: | |
| print("Hello, stranger!") | |
| def do_bye(self, argline): | |
| """Exit the shell.""" | |
| print("Bye!") | |
| return True | |
| def do_eval(self, argline): | |
| """Evaluate given expression and print the result.""" | |
| try: | |
| print(eval(argline)) | |
| except Exception as error: | |
| print(f"ERROR EVALUATING {argline}: {error}") | |
| if __name__ == "__main__": | |
| BasicShell().cmdloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment