1

How is it possible to convert a script accepting a command-line argument to be able to receive the argument through standard user input?

Currently I have a file and to process it I need to run python abc.py query whereas I would like the file to be able to run through python abc.py and then in the next line it should ask the user for input through raw_input and the response should be passed to argument (i.e. 'query').

3
  • 2
    Can you explain what you mean a little better? It seems like you've already solved the problem, (don't use the arguments, just rewrite the script to take input from raw_input...) Commented Aug 18, 2011 at 0:04
  • looks like a duplicate: stackoverflow.com/questions/1450393/… Commented Aug 18, 2011 at 0:20
  • @Sam if you're worried about having command line arguments be too complex, you can use the optparse or argparse modules to use command line flags. Commented Aug 18, 2011 at 2:10

1 Answer 1

2

There is no way to make this happen without changing abc.py itself.

If you can change abc.py then a good way is to have it check for the command-line argument, and if it's not there then ask for it... something like this:

if __name__ == '__main__':
    if len(sys.argv) > 1:
        file_to_process = sys.argv[1]
    else:
        file_to_process = raw_input("Enter file to process: ").strip()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.