簡體   English   中英

python中的簡單命令行應用程序-解析用戶輸入?

[英]Simple command line application in python - parse user input?

python的新功能-我想創建一個命令行應用程序,用戶將在其中鍵入輸入,然后我將解析它並執行一些命令-行如下:

try:
    while True:
        input = raw_input('> ')
        # parse here
except KeyboardInterrupt:
    pass

用戶應該鍵入init /path/to/dir類的命令。 我可以使用argparse解析那些嗎? 我的方式太粗糙了嗎?

您可以看一下cmd庫: http : //docs.python.org/library/cmd.html


如果要自己解析,則可以使用split來標記用戶輸入的內容,並基於標記執行命令,如下所示:

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

arparse是您所提出建議的完美解決方案。 這些文檔寫得很好,並顯示了許多如何簡單調用它的示例。 請記住,它默認情況下要讀取sys.argv,因此在調用parse_args時,您想給它一個args( https://docs.python.org/2.7/library/argparse.html?highlight=argparse#the -parse-args-method )。

唯一的減小是argparse期望項目為“參數”格式,這意味着以短划線開頭。

>>> import argparse
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-init', nargs=1)
>>> parser.parse_args('-init /path/to/something'.split())
Namespace(init="/path/to/something")

這取決於您要執行的操作,但是您可以讓腳本使用ipython(交互式python)。 例如:

    #!/bin/ipython -i
    def init(path_to_dir):
        print(path_to_dir)

用法:注視腳本后,

init(“ pathToFile.txt”)

您正在交互式python會話中運行,因此獲得了諸如制表符補全之類的功能,這些功能很難手動實現。 另一方面,您被python語法所困擾。 這取決於您的應用程序。

我所做的是:

# main
parser = Parser('blah')
try:
    while True:
        # http://stackoverflow.com/a/17352877/281545
        cmd = shlex.split(raw_input('> ').strip())
        logging.debug('command line: %s', cmd)
        try:
            parser.parse(cmd)
        except SystemExit: # DUH http://stackoverflow.com/q/16004901/281545
            pass
except KeyboardInterrupt:
    pass

解析器在哪里:

class Parser(argparse.ArgumentParser):
    def __init__(self, desc, add_h=True):
        super(Parser, self).__init__(description=desc, add_help=add_h,
                                     formatter_class=argparse.
                                    ArgumentDefaultsHelpFormatter)
        # https://docs.python.org/dev/library/argparse.html#sub-commands
        self.subparsers = subparsers = self.add_subparsers(
            help='sub-command help')
        # http://stackoverflow.com/a/8757447/281545
        subparsers._parser_class = argparse.ArgumentParser
        from  watcher.commands import CMDS
        for cmd in CMDS: cmd()(subparsers)

    def parse(self, args):
        return self.parse_args(args)

和一個命令( CMDS=[watch.Watch] ):

class Watch(Command):

    class _WatchAction(argparse.Action):
        def __call__(self, parser, namespace, values, option_string=None):
            # here is the actual logic of the command
            logging.debug('%r %r %r' % (namespace, values, option_string))
            setattr(namespace, self.dest, values)
            Sync.addObserver(path=values)

    CMD_NAME = 'watch'
    CMD_HELP = 'Watch a directory tree for changes'
    ARGUMENTS = {'path': Arg(hlp='Path to a directory to watch. May be '
                                  'relative or absolute', action=_WatchAction)}

哪里:

class Command(object):
    """A command given by the users - subclasses must define  the CMD_NAME,
    CMD_HELP and ARGUMENTS class fields"""

    def __call__(self, subparsers):
        parser_a = subparsers.add_parser(self.__class__.CMD_NAME,
                                         help=self.__class__.CMD_HELP)
        for dest, arg in self.__class__.ARGUMENTS.iteritems():
            parser_a.add_argument(dest=dest, help=arg.help, action=arg.action)
        return parser_a

class Arg(object):
    """Wrapper around cli arguments for a command"""

    def __init__(self, hlp=None, action='store'):
        self.help = hlp
        self.action = action

到目前為止,僅使用一個命令嘗試過,因此尚未經過測試。 我使用了注釋中的shlex和subparsers技巧。 我看了@ jh314建議的cmd模塊,但並沒有完全理解-但是我認為它是完成這項工作的工具-我對使用我的命令但使用cmd模塊的代碼的答案很感興趣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM