如何制作可调用命令 python 的列表?

发布于 2024-11-03 21:26:02 字数 126 浏览 1 评论 0原文

我想知道你如何制作用户可以输入的命令列表。例如,用户输入“who”即可获取 MUD 中的成员列表。这一切都是在 python 中使用 if、elif 和 else 完成的吗?

顺便说一句,我正在使用 Python 3.1。

I was wondering how you go about making a list of commands the user can enter. Like for example, user types in "who" to get a list of who is on in a MUD. Is it all done using if, elif and else in pythons?

I am using Python 3.1 btw.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愁杀 2024-11-10 21:26:02

没有。发送字典。

def who(*args, **kwargs):
   ...

commands = {
  'who': who,
   ...
}

...
if command in commands:
  commands[command](*args, **kwargs)
else:
  print('Bad command or file name')

Nope. Dispatch dictionary.

def who(*args, **kwargs):
   ...

commands = {
  'who': who,
   ...
}

...
if command in commands:
  commands[command](*args, **kwargs)
else:
  print('Bad command or file name')
云淡月浅 2024-11-10 21:26:02

cmd 模块就是为此而设计的。只需子类 cmd.Cmd(),包括一组以 do_XXX 开头的方法 - 因此 do_who() 将提供 who< /代码> 命令。

The cmd module is made for this. Just subclass cmd.Cmd(), including a set of methods that begin do_XXX - so do_who() would provide the who command.

久光 2024-11-10 21:26:02

查找 input 函数以获取用户的命令行输入:

输入([提示])

如果提示参数存在,则将其写入标准输出
没有尾随换行符。这
然后函数从输入中读取一行,
将其转换为字符串(剥离
尾随换行符),并返回该值。

这是一个小例子:

s = input("Enter a command: ")
if s == 'who':
  # do stuff
elif s == 'help':
  # show help
else
  # do other stuff

Look for the input function to get command-line input from the user:

input([prompt])

If the prompt argument is present, it is written to standard output
without a trailing newline. The
function then reads a line from input,
converts it to a string (stripping a
trailing newline), and returns that.

Here's a small example:

s = input("Enter a command: ")
if s == 'who':
  # do stuff
elif s == 'help':
  # show help
else
  # do other stuff
念﹏祤嫣 2024-11-10 21:26:02

您可以导入 sys 模块,然后使用该模块的 sys.stdin.readline() 部分。这将接受来自命令行或终端的输入,这意味着您也必须从其中之一运行它。

import sys
sys.stdout.write("$ ")
command = sys.stdin.readline()
command = command.strip() #Remove the new line character
if command = argument:
    do code
elif command = other argument:
    do more code
else:
    Thats not a command than

使用这样的好处是,在linux系统下,程序仍然能够运行。例如,我使用它接受命令来检查我的服务器/客户端程序以查找 3 个立方体的总和。我可以告诉它退出,检查找到的多维数据集,或者查看客户端和线程列表。

You are able to import the sys module and than use the sys.stdin.readline() part of that module. That will accept input from a command line or terminal, which means you have to run it from one of those too.

import sys
sys.stdout.write("$ ")
command = sys.stdin.readline()
command = command.strip() #Remove the new line character
if command = argument:
    do code
elif command = other argument:
    do more code
else:
    Thats not a command than

The benefits of using this is that, under a linux system, the program is still able to run. For example, I use it to accept commands to check on my server/client program to find the sum of 3 cubes. I can tell it to quit, check the found cubes, or look at the list of clients and threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文