From: Dave K. <dku...@cu...> - 2006-03-10 00:13:21
|
<Srikrishna_Parthasarathy <at> timeinc.com> writes: > > I am new to Jython . I have the following .py file . This > has been created based on BEA’s doc. I used to create a weblogic domain. > I want the py script to be more generic. I run now using > > > > Java weblogic.WLST filename.py Why not pass in your arguments as command line flags and command line arguments? You can use the getopt module, which is available to Jython, to parse flags and arguments. Would that solve your problem? If so, below is a sample script taken from some boiler plate that I use. You can run this script with something like the following: $ jython test_clargs.py --myflag=somevalue myargument or: $ jython test_clargs.py -m somevalue myargument Dave ============================================================= # test_clargs.py import sys import getopt def test(flag, arg1): print 'flag: "%s"' % (flag, ) print 'arg: "%s"' % (arg1, ) USAGE_TEXT = """ Usage: python test_clargs.py [options] Options: -h, --help Display this help message. Example: python test_clargs.py --myflag=somevalue arg1 """ def usage(): print USAGE_TEXT sys.exit(-1) def main(): args = sys.argv[1:] try: opts, args = getopt.getopt(args, 'hm:', ['help', 'myflag=']) except: usage() myflag = None for opt, val in opts: if opt in ('-h', '--help'): usage() elif opt in ('-m', '--myflag'): myflag = val if len(args) != 1: usage() test(myflag, args[0]) if __name__ == '__main__': main() ============================================================= |