patrick
patrick

Reputation: 4852

Using sys.argv for input through Terminal

I wrote a Python script that I am now trying to get to run via the command line. It consists of a function that takes one obligatory and a few optional arguments.

def main(input_folder, iterations = 1000, probability_cutoff = - 40 , threshold = 10): ...

Now I am trying to make it executable through the command line like so:

if __name__ == "__main__": main(sys.argv[1])

This works well as long as I put in only one argument; but I don't know how to accept the additional, optional input that sys.argv delivers as a list.

I am working with Python 2.7 on a Mac. Any help is much appreciated!

Upvotes: 0

Views: 907

Answers (1)

Don Kirkby
Don Kirkby

Reputation: 56230

I always use argparse, because it gives you nice error handling, converts strings to ints or open files, and clearly documents the options. However, this should do what you want:

if __name__ == "__main__":
    main(*sys.argv[1:])

Upvotes: 2

Related Questions