More effective way of passing several arguments to script?
<p>My command line script takes several arguments including strings, ints, floats and lists, and it is becoming a bit tricky to do the call with all arguments:</p>

<pre><code>python myscript.py arg1 arg2 arg3 ... argN
</code></pre>

<p>To avoid having to write all arguments in the command line I have created a text file with all arguments which I simply read line by line to collect arguments. This is working fine, but is this the best practice for doing this? Are there a more effective way?</p>

<p>For me I think python getopt is the way to go.</p>

<pre><code>#!/usr/bin/python

import sys, getopt

def main(argv):
   inputfile = ''
   outputfile = ''
   try:
      opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
   except getopt.GetoptError:
      print 'test.py -i &lt;inputfile&gt; -o &lt;outputfile&gt;'
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print 'test.py -i &lt;inputfile&gt; -o &lt;outputfile&gt;'
         sys.exit()
      elif opt in ("-i", "--ifile"):
         inputfile = arg
      elif opt in ("-o", "--ofile"):
         outputfile = arg
   print 'Input file is "', inputfile
   print 'Output file is "', outputfile

if __name__ == "__main__":
   main(sys.argv[1:])
</code></pre>

<p>Usage:</p>

<pre><code>usage: test.py -i &lt;inputfile&gt; -o &lt;outputfile&gt;
</code></pre>

<p>It's easier to run when you have a name for the argument.</p>

<p>Ref: <a href="http://www.tutorialspoint.com/python/python_command_line_arguments.htm" rel="nofollow">http://www.tutorialspoint.com/python/python_command_line_arguments.htm</a></p>

python arguments parameter-passing