1

I'm trying to make a custom command line to control a robotic arm. So I want to be able to run the program and type in servoMove(arg1,arg2) and have arg1 and arg2 get transferred into the function servoMove.

servoPos = [0,1,2,3,4]

def servoMove(servo,angle):
    servoPos[servo] = angle
    print(servoPos[servo])

def commands(cmd):
    if cmd == 'servoMove('+arg1+','+arg2+')':
        servoMove(arg1,arg2)
    else:
        print("[Error] - Unknown Command")

commands(input(""))

Clearly, the code below doesn't work for this.

    if cmd == 'servoMove('+arg1+','+arg2+')':
        servoMove(arg1,arg2)

Does anybody know how I can do this?

  • 1
    I'm guessing the command line interface is some kind of requirement in the brief, otherwise why wouldn't you just run the module and then in the shell prompt type "servoMove(etc"? – neophlegm Dec 22 '17 at 00:58
  • `"servoMove('+arg1+','+arg2+')"` – slackmart Dec 22 '17 at 00:59

3 Answers3

1

You can use a regular expression to parse the command.

import re
def commands(cmd):
    m = re.match(r'servoMove\((\d+),(\d+)\)', cmd)
    if m:
        servoMove(int(m.group(1)), int(m.group(2)))
        return
    # Put similar tests for other commands here
    # ...
    print("[Error] - Unknown Command")

This is a really crude way to do it -- if the user doesn't enter the command exactly right it will complain that the command is unknown. If you want something more robust, you need to learn how to write a real parser. Or use a better user interface, such as Tkinter to implement a form that the user can fill out.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You can use the cmd module to build a command line interface.

Here's an example:

import cmd

servoPos = [0,1,2,3,4]

def servoMove(servo,angle):
    servoPos[servo] = angle
    print(servoPos[servo])

class ServoShell(cmd.Cmd):
    prompt = '=> '

    def do_servoMove(self, arg):
        'Edit this to give a description to the function when typing ?'
        servoMove(*parse(arg))

def parse(arg):
    'Convert a comma separated string into a tuple'
    return tuple(map(int, arg.strip('()').split(',')))

if __name__ == '__main__':
    ServoShell().cmdloop()
gabe_
  • 354
  • 2
  • 5
0

Just looking at the structure the problem is in the if statement: arg1 and arg2 are undefined at that stage, so you'll get a False. For starters you'd want to replace that with something like:

#Look at the nine first characters to see if they match your function
if cmd[:9] == 'servoMove':

To extract your arguments, I'd use some string manipulation as in here. I've sliced the input to take the text between "(" and "," as arg1, and "," and ")" as arg2.

arg1 = cmd[cmd.find("(")+1:cmd.find(",")]
arg2 = cmd[cmd.find(",")+1:cmd.find(")")]

Putting it together:

def commands(cmd):
    if cmd[:9] == 'servoMove':
        arg1 = cmd[cmd.find("(")+1:cmd.find(",")]
        arg2 = cmd[cmd.find(",")+1:cmd.find(")")]
        servoMove(arg1, arg2)
    else:
        print("[Error] - Unknown Command")
neophlegm
  • 375
  • 1
  • 13
  • If you're going this way, it's probably easier to use [`.startswith()`](https://docs.python.org/3/library/stdtypes.html#str.startswith) to parse the command. – SCB Dec 22 '17 at 01:08