[leglug-users] Python - Parsing command line params

Hisham Mardam Bey hisham.mardambey at gmail.com
Mon Jun 25 16:32:31 EDT 2007


Naim,

You asked on #leglug about Python and command line option parsing,
here's some code (excuse the + signs) someone committed to our CVS
repository the other day:

+# Parse command line
+from optparse import OptionParser
+
+def parse_geometry(option, opt, value, parser):
+    try:
+        w, h = value.split("x")
+        w = int(w)
+        h = int(h)
+    except Exception, e:
+        raise optparse.OptionValueError("Invalid format for %s" % option)
+    parser.values.geometry = (w, h)
+
+usage = "usage: %prog [options]"
+op = OptionParser(usage=usage)
+op.add_option("-e", "--engine", type="choice",
+              choices=("x11", "x11-16"), default="x11-16",
+              help=("which display engine to use (x11, x11-16), "
+                    "default=%default"))
+op.add_option("-n", "--no-fullscreen", action="store_true",
+              help="do not launch in fullscreen")
+op.add_option("-g", "--geometry", type="string", metavar="WxH",
+              action="callback", callback=parse_geometry,
+              default=(800, 480),
+              help="use given window geometry")
+op.add_option("-f", "--fps", type="int", default=50,
+              help="frames per second to use, default=%default")
+
+
+# Handle options and create output window
+options, args = op.parse_args()
+if options.engine == "x11":
+    f = ecore.evas.SoftwareX11
+elif options.engine == "x11-16":
    if ecore.evas.engine_type_supported_get("software_x11_16"):
-        ee = ecore.evas.SoftwareX11_16(w=800, h=480)
+        f = ecore.evas.SoftwareX11_16
    else:
-        ee = ecore.evas.SoftwareX11(w=800, h=480)
+        print "warning: x11-16 is not supported, fallback to x11"
+        f = ecore.evas.SoftwareX11
+
+w, h = options.geometry
+ee = f(w=w, h=h)
+ee.fullscreen = not options.no_fullscreen
+edje.frametime_set(1.0 / options.fps)
+

I hope this helps.

hisham.

-- 
Hisham Mardam Bey
http://hisham.cc/
+9613609386
Codito Ergo Sum (I Code Therefore I Am)


More information about the leglug-users mailing list