Essentially, I am trying to develop a command-line type program that has certain commands with their arguments. Unfortunately, I cannot figure out how to make a second argument for my commands. Example: ping (second argument). Here is what I've tried so far:
inpt = input("$")
for i in range(0, len(inpt), 4):
if inpt == "print":
print(inpt.replace("print","")) #should print the second argument as a test
input()
1 Answer 1
Depending on the situation, to take arguments you would be looking at argv imported from sys.
example: test2.py from sys import argv
script, arg1, arg2 = argv
print(script)
print(arg1)
print(arg2)
Running the file
python3 test2.py my_arg1 my_arg2
test2.py
my_arg1
my_arg2
If you want to do the same thing but after asking for input from the user, then look at the split() method in the str class. You can use split to split on spaces, or any other character you'd like.
inpt.split()to produce a list of words in the variable. For example if the user typedping foo bar, you would get['ping', 'foo', 'bar']../test.py arg1 arg2 arg3etc. You might want to check out pythons optparse: docs.python.org/2/library/optparse.html