0

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()
3
  • click.pocoo.org/6 Commented Feb 24, 2018 at 4:38
  • Use inpt.split() to produce a list of words in the variable. For example if the user typed ping foo bar, you would get ['ping', 'foo', 'bar']. Commented Feb 24, 2018 at 4:39
  • 1
    It looks like you are trying to get values by using the input, if so just do what John Gordon is saying. But, you describe passing in arguments from the command line, like ./test.py arg1 arg2 arg3 etc. You might want to check out pythons optparse: docs.python.org/2/library/optparse.html Commented Feb 24, 2018 at 4:53

1 Answer 1

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.

answered Feb 24, 2018 at 4:40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.