0

We have a CLI tool which uses Typer module in python.

However, I want to create a custom command prompt - like the mysql prompt for example.

mysql>

Currently, the commands are invoked like:

C:\Users\ABC> python wowcli.py command1 paramA paramB
C:\Users\ABC> python wowcli.py command2 paramC paramD

I am trying to achieve something like:

C:\Users\ABC> python wowcli.py
wowcli> command1 paramA paramB
wowcli> command2 paramC paramD

Is there any python library that we can install or otherwise? Please suggest.

2
  • Does rich satisfy your need? rich.readthedocs.io/en/stable/introduction.html Commented May 15, 2022 at 4:37
  • @YaakovBressler Thanks for the suggestion. I am not sure if rick have the functionality I am looking for. I have now edited the question to add context. Please have a check. Commented May 15, 2022 at 5:07

3 Answers 3

1

You are looking to build a custom REPL, read evaluate print loop.

Your question is very broad but you can get started here https://dev.to/amal/building-the-python-repl-3468

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not absolutely sure what you mean here. Have a look at input. You can "parse" these arguments with str.split.

Comments

0

I Made A Script That Can Dynamically Add Commands To The App, Bind A Function To It And Give It All The Args Of The Command As Its First Parameter,

import time
import os

class Data:
    init = 0
    cmd = ""
    ss = "$"
    commands = {}
    default = []
    temp = {"0":[]}
    startup = [False, "", ""]
    error = True
    bucket = []
    alrt = ""
    wait = 0

class File:
    def read(self, filename):
        with open(filename) as f:
            return f.read()

    def write(self, filename, contents):
        with open(filename, "w") as f:
            f.write(contents)
        return 1

class Shell:
    def clear(self):
        os.system("cls")

    def run(self, command):
        if Shell != "":
            try:
                Data.commands[command[0]]
            except:
                print("Invalid Command")

    def start(self):
        if Data.alrt != "":
            print(Data.alrt)
            Data.alrt = ""

        time.sleep(Data.wait)
        Data.wait = 0

        cmd = input(Data.ss + " ")
        self.run(cmd.split(" "))
        self.start()

    def symbol(self, symbol: str):
        Data.ss = symbol

    def new(self, command, function):
        Data.commands.update({command: [function]})

    def delete(self, command):
        del Data.commands[command]

    def getcommands(self):
        return Data.commands

    def getcommand(self, command):
        return Data.commands[command]

    def reset(self):
        Data.commands = Data.default

    def show(self, parameters):
        print(" ".join(parameters[1:]))

    def startup(self, message, dialog):
        Data.startup[0] = True
        Data.startup[1] = message
        Data.startup[2] = dialog

    def alert(self, message):
        Data.alrt = message

    def wait(self, timeAmount):
        Data.wait = timeAmount

Data.default = Data.commands

It Is Under Development And The Startup Feature Does Not Work Yet But Everything Else Does. Hope This Helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.