Tim Chase wrote: > I've been learning the ropes of the optparse module and have been > having some trouble getting the help to format the way I want. > > I want to specify parts of an option's help as multiline. > However, the optparse formatter seems to eat newlines despite my > inability to find anything in optparse.py that does something > obvious like .replace("\n", " ") to eat the newlines I stick in > the help. It also seems to mung tabs. An example from my code: > > parser.add_option("-x", "--extended", > action="callback", > callback=callback_test, > type="string", # required to get metavar to show in help > dest="test", > metavar="CONF", > help="a comma-separated list of options.\n" > "s=[YYYY]MMDD,n=NAME,c=COLOR\n" > "s,start=[YYYY]MMDD\n" > "\tstart day of period (default, current day)\n" > ... > ) > > which would accept options that looked something like > > test.py -x s=20070401,n=XR71,c=black > > (my callback_test breaks apart parser.rargs[0] to deal with the > funky params). > > However, when I display the help, its reformatting eats the > newlines in my help-string and strangely looks like it converts > my tabs to spaces. > > Any hints on how to either work around the problem or fix it?
I guess this is just an optparse-y week on c.l.py. ;-) Ben Finney pointed you in the right direction for optparse. If you care to try out argparse (http://argparse.python-hosting.com/) which has a similar API, it has a builtin RawTextHelpFormatter formatter class:: import argparse def my_type(string): # split string and turn it into appropriate object(s) return 'foo(%s)' % string parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter ) parser.add_argument( "-x", "--extended", type=my_type, dest="test", metavar="CONF", help="a comma-separated list of options.\n" "s=[YYYY]MMDD,n=NAME,c=COLOR\n" "s,start=[YYYY]MMDD\n" "\tstart day of period (default, current day)\n" ) args = parser.parse_args() print args.test This program will produce output like:: $ python script.py -x sdfsdfas foo(sdfsdfas) $ python script.py --help usage: script.py [-h] [-x CONF] optional arguments: -h, --help show this help message and exit -x CONF, --extended CONF a comma-separated list of options. s=[YYYY]MMDD,n=NAME,c=COLOR s,start=[YYYY]MMDD start day of period ... HTH, STeVe -- http://mail.python.org/mailman/listinfo/python-list