在Python中创建CLI(Shell?) [英] Creating a CLI (Shell?) in Python

查看:83
本文介绍了在Python中创建CLI(Shell?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,但我必须为学校实现Python语言的命令行解释器,但我却迷失了如何做到这一点。

I am very newbie in Python but I have to implement for school a command line interpreter in Python language, but I am kinda lost in how to do that.

我已经阅读了一些教程,并创建了一个名为functions.py的简单文件,其中包括了一些简单的函数,例如:

I have already read some tutorials and created a simple file called functions.py where i include some simple functions like this:

def delete(loc):
    if os.path.exists(loc) == True:
        os.remove(loc)
        print "Removed"
    else:
        print "File not exists"

现在。这是东西。为了使用此文件,我必须将其导入python命令解释器中,例如...

Now.. here is the thing.. in order to use this I must import it inside the python command interpreter, like...

import functions
functions.delete("file to delete")

我如何制作Shell / CLI,所以不必写所有这些内容就像这样写:

How can I make a Shell/CLI so instead of have to write all of this I can just write like:

delete file_name

谢谢!

推荐答案

或者如果您想要一个cmd, ll,您可以使用cmd lib。它提供python界面来制作命令行。
http://docs.python.org/library/cmd.html

Or if you want a cmd shell, you could use the cmd lib. It offers python interfaces to making command lines. http://docs.python.org/library/cmd.html

这篇关于在Python中创建CLI(Shell?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆