[Tutor] Solution found for option parser

Andrew Robert andrew.arobert at gmail.com
Wed May 17 02:57:15 CEST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi everyone,

Just to be complete, I figured I would share the solution to use option
parser for both command line and configuration file option passing.

I hope it may be of use to someone in the future.


parser = OptionParser()

if len(sys.argv) == 2:
    lines = open(sys.argv[1],"rb").readlines()
    for line in lines:
	    line=line.strip()
	    if not line:
		    continue
	    short, long, dest, help, default = line.split(";")
	    help = "\t\t" + help # Prepend tabs to help message
	    parser.add_option(short, long, dest=dest, help=help, default=default)
else:
    parser.add_option("-m","--qmanager", dest="qmanager",
                help="\t\tQueue Manager to inquire against"),
    parser.add_option("-q","--queue", dest="queue",
                help="\t\tQueue the message will be sent to"),
    parser.add_option("-d","--dest", dest="dest",
                help="\t\tDestination File Name"),
    parser.add_option("-f", "--file",
                action="store", type="string", dest="filename",
                help="File to be transmitted", metavar="FILE")

(options, args) = parser.parse_args()


thanks all for the insight
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)

iD8DBQFEanTrDvn/4H0LjDwRAmt0AJ4jf84OXoNdrxzz+A/pdKgiPnhyAwCfZQrG
QCuhjN8sbYq+WhzmLToKIZs=
=nOFN
-----END PGP SIGNATURE-----


More information about the Tutor mailing list