0

I am trying to build Python interactive console app that should work on Windows and Linux. I want it to be able to autocomplete commands using tab. It seams like cmd is a good tool for this purpose but it uses readline, which is available only under Linux. Reading related questions I found out that there is a Windows alternative to readline - pyreadline. But its documentation says that it is only tested under Windows, which means it is not suitable for Linux. And I cannot really figure out how to make cmd work with pyreadline anyway.

An example of what I am trying to accomplish: Commands: test, read, write. When user writes t and presses tab the command should be completed to test.

The app should also be compatible with Python 2.7 and Python 3.

Please let me know is you have any thoughts on this!

sky
  • 1
  • 1

1 Answers1

0

You might find some ideas in this thread: How to code autocompletion in python?

Another approach is to get the user's input with something like Enter command: If they enter 't', show an input with Enter command [test]: where values in brackets are defaults. Not as slick, but functional if you have a limited number of commands.

To issue different commands based on the user's OS, use platform.system(). Here's an example for clearing the terminal screen.

import os
import platform

def clear_screen():

    os_type = platform.system()

    if((os_type == 'Linux') or (os_type == 'Darwin')):  
        # 'Darwin' includes Apple Macs
        return os.system('clear')

    elif(os_type == 'Windows'):
        return os.system('cls')

    else:
        print('Sorry, your operating system is not supported.')
landrykid
  • 39
  • 1
  • 6