On Tue, Jan 27, 2009 at 4:01 PM, Johan Ekh <ekh.jo...@gmail.com> wrote: > Thank you James, > but I just can't optparse to accept an array, only integers, floats ans > strings. > > My code looks like this > > from optparse import OptionParser > parser = OptionParser() > parser.add_option('-t', '--dt', action='store', type='float', dest='dt_i', > default=0.1, help='time increment where lsoda saves results') > parser.add_option('-T', '--tstop', action='store', type='float', > dest='tstop_i', default=1.0, help='duration of the solution') > parser.add_option('-m', '--mass_vector', action='store', type='float', > dest='m_i', default=[1.0, 1.0], help='vector with lumped masses') > op, args = parser.parse_args(sys.argv[1:]) > > I want this to work for m_i = array([1.0, 2.0, 3.0]) but the optparse > complains that m_i is not a float.
What you want to do is accept a string as an argument to -m/--mass_vector and parse this string into a list. For example: parser.add_option('-m', '--mass_vector', action='store',dest='m_i', default=None, help='vector with lumped masses') op, args = parser.parse_args(sys.argv[1:]) if op.m_i is None: m_i = [1.0, 1.0] else: xs = m_i.split(",") m_i = [float(x.strip()) for x in xs] Of course be aware that this code does no error checking and if you pass in rubbish, it will likely throw an exception. cheers Jaems -- http://mail.python.org/mailman/listinfo/python-list