cmd module - python
Question: I am trying to build a python shell using cmd module.

    
    
    from cmd import Cmd
    import subprocess
    import commands
    import os 
    from subprocess import call
    
    class Pirate(Cmd): 
        intro = 'Welcome to shell\n'
        prompt = 'platform> '
        pass
    
    if __name__ == '__main__':
        Pirate().cmdloop()
    

I am trying to build a shell using python - cmd module. I am trying to build
these two functionalities.

Welcome to shell

platform> ls

platform> cd ..

like if I want to perform ls - list all files from that directory in my python
shell or cd .. - go back to prev directory

Can anyone help in this? I tried using subprocess library.. but didn't get it
working.

Appreciate your help ! Ref Doc: <https://docs.python.org/3/library/cmd.html>


Answer: I have a hard time trying to figure out why you would need such a thing, but
here's my attempt:

    
    
    import subprocess
    from cmd import Cmd
    
    class Pirate(Cmd): 
        intro = 'Welcome to shell\n'
        prompt = 'platform> '
    
        def default(self, line): # this method will catch all commands
            subprocess.call(line, shell=True)
    
    if __name__ == '__main__':
        Pirate().cmdloop()
    

The main point is to use `default` method to catch all commands passed as
input.

