patrick patrick - 2 years ago 122
Python Question

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__":

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
delivers as a list.

  • Is there a simple way of doing this with this approach?

  • Or is it necessary to use an additional module such as

  • I tried feeding keyword arguments into the function as well but
    couldn't make that work either - is that a feasible approach?

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

Answer Source

I always use argparse, because it gives you nice error handling and clearly documents the options. However, this should do what you want:

if __name__ == "__main__":
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download