0

我正在使用cmd库来设计 python 交互工具(shell)。我的要求是提供变量并针对变量存储值。在cmdorcmd2图书馆有可能吗?

>> get_values **A_VARIABLE 100 B_VARIABLE 200**

期望是:A_VARIABLE应该将 100 存储为值,并且也类似 B_VARIABLE=200。在代码中,我应该能够访问这些变量。

#!/usr/bin/python
import cmd
import os

class Console(cmd.Cmd):

  def do_get_values(self, args):
      args_list = args.split()
      #Empty List
      print "args:", args_list

  def help_get_values(self):
      print '\n'.join(['Get Values',
                      'Usage: get_values 1 3 4 5'])

  def emptyline(self):
    pass

  def do_exit(self, arg):
        """Exits from the console (usage: exit)"""
        return -1

  def do_EOF(self, args):
        """Exit on system end of file character (Usage : CTRL+D)"""
        return self.do_exit(args)

if __name__== "__main__":

    prompt = Console()
    prompt.prompt = "\n>>"
    prompt.cmdloop("Welcome !")

我的程序的输出:

python dummy_console.py
Welcome !

>>help

Documented commands (type help <topic>):
========================================
EOF  exit  get_values  help


>>get_values 1 2 3
args: ['1', '2', '3']

>>get_values NUM_1 1 NUM_2 2
args: ['NUM_1', '1', 'NUM_2', '2']

>>get_values ANUM 1 BNUM 2
args: ['ANUM', '1', 'BNUM', '2']

注意:
    在这里我可以看到所有值都被视为参数。

4

0 回答 0