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').

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
Sirius
  • 736
  • 2
  • 9
  • 22
  • 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...) – marr75 Aug 18 '11 at 00:04
  • looks like a duplicate: http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python – Patrick Aug 18 '11 at 00: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. – KyleWpppd Aug 18 '11 at 02:10

1 Answers1

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()
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237