I'm trying to create a "command line" in Python, which makes a user input a certain command and its associated data, without the use of brackets.

So the user would input:

>>> test 1 2 3

So it would carry out a command:

def test(firstnum,secondnum,thirdnum):
	return firstnum,secondnum,thirdnum

The operation I have in mind is much longer/different, and I just need a bit of guidance to get me started.

I have a command line:

usercmd= raw_input("Enter a command -->")
   if usercmd == "exit":
   if usercmd == 'test %s %s %s' % (firstnum,secondnum,thirdnum):

Now the first command, exit, works fine, but I don't know how to take user input from a command line.

The second command, which is supposed to take test and pass them off as strings to test, does not. I'm trying to do this without an "Enter first number:", "Enter second number:", "Enter third number" staged user inputs.

So the user would input:

Enter command: test 1 2 3

then the firstnum,secondnum,thirdnum get passed to test and it carries out its operations (in this case, returning the three values).

Can anyone think of a way to do this?


I would say you could take the input string, and split it at each whitespace into a list. Index 0 would be the function name, and each index after would be the arguments. Like:

cmdlist = usercmd.split(' ')  # split at each space

This would make a list like:

[ 'test', '1', '2', '3' ]

Then you just need to call the function and pass the indices 1 and above. If you want more details, just ask but I hope this got you started in a new direction!

thanks! put me on the right track straight away

Thanks shadwickman! My command line now works just as it should.

This question has already been answered. Start a new discussion instead.