简体   繁体   English

如何创建一个接受命令并显示“ $”提示以指示用户可以使用while循环输入命令的shell程序?

[英]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.) 我是Python的初学者,我不确定如何创建一个可以接受命令(例如,printrecipes,printinventory,load等)的简单Shell程序。

The input should look like: 输入应如下所示:

$ loadrecipes $ loadrecipes

$ printmoney() $ printmoney()

20 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. 对于此shell,我尝试使用while循环,以便即使他们输入了可接受的命令,它也可以继续执行程序而不会崩溃。

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). 命令是loadrecipes(recipe_file)接受一个参数,所有打印命令都不接受参数,buyingredient(ingredient_name,number:int)接受2个参数(成分名称和这些成分中的多少)。

So, for each command I have created a function in correspondence. 因此,对于每个命令,我都创建了一个对应的函数。 Such as for printiinventory() I have: 例如对于printiinventory()我有:

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

so if the command is: 因此,如果命令是:

$ printiinventory $ printiinventory

0 4 3 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. 因此,您应该检查命令,例如command == "printrecipes"等。

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. 唯一的不同是print()创建了新行,因此输入将取自打印内容正下方的行。

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. 您可以使用IO低级Python模块来模拟这种行为(即在同一行中进行打印),但是没有必要这样做。

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. 万一您第一次使用,可以使用Python的内置argparse模块。 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. 如果您知道regex没什么大不了的,但是恐怕您应该在网站上问另一个问题。 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. 只是谷歌搜索:“使用我自己的命令解析器python”可以为您提供成千上万个结果,即使其中大多数将用于经典的命令行解析语法。

Edit 2 编辑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. 只需使用break命令,您就可以使用了。

Edit 3 编辑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. 通过仔细查看OP的注释,我看到您正在尝试编写要由Python脚本执行的Python代码。 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. 您已经有了evalexec模块,但请注意,这是一个非常危险的做法,很容易将代码注入到您的程序中,从而造成巨大的安全漏洞。 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. 用户永远无法直接访问程序的控制流。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写一个程序,从用户那里接受一个正整数并打印该整数的前四个倍数。 使用while循环 - Write a program that accepts a positive integer from the user and prints the first four multiples of that integer. Use a while loop 如何在Windows中创建3个命令提示符实例,并使用python将命令并行传递给每个提示符 - How to create 3 instances of command prompt in Windows and pass commands to each prompt in parallel using python 如何使用Python在命令提示符下传递命令 - How to pass commands in command prompt using Python While 循环使用用户输入和命令运行 - While loop functioning with user input and commands 在 Python 中,如何使用 input + 'filename' 创建一个 while 循环 - In Python, How can I create a while loop using input + 'filename' 如何在使用循环时创建列表用户输入? - How can I create a list user inputs while using a loop? Python - While 循环接受用户输入并在终止后打印列表 - Python - While loop to take user input and once terminated prints the list 如何使用用户输入的两个连续的相同字符串停止while循环,然后打印出其余的字符串 - how to stop a while loop with two consecutive same strings form user input and then prints out the rest of the strings 如何在不中断程序的情况下停止用户输入的while循环? - How to stop a while loop with a user input without interrupting the program? While循环不断提示用户提供正确的输入类型 - While loop to continuously prompt user to provide correct input type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM