简体   繁体   中英

How to create a shell program that accepts commands and prints a “$” prompt to indicate that a user can input a command using a while loop?

I'm a beginner Python coder and I'm very unsure on how to create a simple shell program that accepts commands (ex. printrecipes, printinventory, load etc.)

The input should look like:

$ loadrecipes

$ printmoney()

20

For this shell, I'm trying to use a while loop so it continues through the program without crashing even if they input a command that is acceptable.

def handle_commands():
    keep_going=True
    command=input("$" + " ")
    while keep_going:
        if command == '$ quit':
            keep_going = False
            break
        elif command == "$ loadrecipefile(recipe_file)"
            j
        elif command == "$ printrecipes":
            printrecipes()
        elif command == "$ printiinventory":
            printiinventory()
        elif command == "$ printmoney":
            printmoney()
        elif command == "$ buyingredient":

I have no idea what to go from here. The commands are that loadrecipes(recipe_file) takes in one argument, all print commands don't take an argument, buyingredient(ingredient_name, number:int) takes in 2 arguments (the ingredient name and how many of those ingredients).

So, for each command I have created a function in correspondence. Such as for printiinventory() I have:

def printiinventory():
  print(iinventory['apple'],iinventory['beets'],iinventory['carrots'])

so if the command is:

$ printiinventory

0 4 3

it should come out to be like this

So your flow should look like this:

while True:
    command = input("$ ")
    if command is ...
    elif ...:

Very similar to what you have, with the difference that you don't need to expect $ into the user's input. Input function prints the argument passed and returns SOLELY the user's input, not the rest of the content in the same line. So you should check for commands like command == "printrecipes" , etc.

Explanation:

This piece of code:

x = input(str)

Is equivalent to:

print(str); x = input(str)

with the only difference that print() creates a new line, so the input will be taken from the line just below the printed content.

You could emulate this behaviour (the printing in the same line, that is) with the IO low-level Python modules, but there is no need when you can do just that.

Edit

In order to parse the commands, you can opt for the classical command line interface syntax, that separates command name and argument with spaces, or you could make your own parser. In case you go for the first, you could use Python's built-in argparse module. In case you'd rather use the second (which is more of a headache, especially if you are a starter), you have to write your own parser from scratch. Is not that big of a deal if you know regex, but I'm afraid that's a different question you should ask in the site. I would recommend you to take a look at some tutorials. Just googling: "make my own command parser python" gives you thousands of results, even though most of them will go for classic command line parsing syntax.

Edit 2

I've noticed you use some sort of flag to check if you need to keep going inside the loop. That is useless in the piece of code you use; just use break command and you're good to go.

Edit 3

Taking a close look at the OP's comments, I see you are trying to write Python code to be executed by a Python script. You can for sure do that; you've got the eval and exec modules, BUT note that this is a very risky practice, code can very easily be injected into your program, causing huge security holes. It is highly discouraged to do that. You have to separate command parsing from task executing. The user cannot ever have direct access to the control flow of the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM