且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python中的简单命令行应用程序-解析用户输入?

更新时间:2023-08-25 23:05:04

您可以查看cmd库: http://docs.python.org/library/cmd.html

You can take a look at the cmd lib: http://docs.python.org/library/cmd.html

如果要自己解析,可以使用split标记用户输入,并基于标记执行命令,如下所示:

If you want to parse by yourself, you can use split to tokenize the user input, and execute your commands based on the tokens, sort of like this:

try:
    while True:
        input = raw_input('> ')
        tokens = input.split()
        command = tokens[0]
        args = tokens[1:]
        if command == 'init':
            # perform init command
        elif command == 'blah':
            # perform other command


except KeyboardInterrupt:
    pass