Python interactive shell with cmd or cmd2 variables?

411 Views Asked by At

I am using cmd library to design the python interactive tool (shell). My requirement is to provide variables and store values against variables. Is it possible in the cmd or cmd2 library?

>> get_values **A_VARIABLE 100 B_VARIABLE 200**

Expectation is: A_VARIABLE should store 100 as value and also similarly B_VARIABLE=200. In the code, I should able to access these variables.

#!/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 !")

My program's output:

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']

Note:
    Here I could see all values are taken as arguments.

0

There are 0 best solutions below