[英]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模块来模拟这种行为(即在同一行中进行打印),但是没有必要这样做。
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”可以为您提供成千上万个结果,即使其中大多数将用于经典的命令行解析语法。
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
命令,您就可以使用了。
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. 您已经有了eval
和exec
模块,但请注意,这是一个非常危险的做法,很容易将代码注入到您的程序中,从而造成巨大的安全漏洞。 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.