Add some command line options support.

portnov [2008-08-30 14:58:45]
Add some command line options support.
Filename
qwerty.py
diff --git a/qwerty.py b/qwerty.py
index 0a80465..922be41 100755
--- a/qwerty.py
+++ b/qwerty.py
@@ -4,6 +4,7 @@
 import sys
 import os
 import re
+from getopt import getopt
 import time

 from Xlib import X,display,Xatom
@@ -52,6 +53,27 @@ config_file = os.path.expanduser('~/.config/qwerty.conf')
 sections = {}
 speckeys = {}

+def help():
+    print """Synopsis:
+qwerty.py [-c | --config /path/to/config] [-f] [-g | --geometry geometry] [-d] [-h | --help]
+
+-c | --config /path/to/config
+    Use specified config file instead of ~/.config/qwerty.conf
+
+-f
+    Do not show fullscreen window
+
+-d
+    Do not show window decorations
+
+-g | --geometry geometry
+    Specify window geometry
+
+-h | --help
+    Show this help and exit.
+"""
+
+
 def find_icon(name):
     if os.path.exists(name):
         return name
@@ -239,6 +261,33 @@ class Key(object):
     def __repr__(self):
         return "<Key %s: %s>" % (self.key, self.title)

+def parse_cmdline():
+    global config_file
+
+    fullscreen = True
+    decorate = True
+    geometry = ''
+
+    opts,args = getopt(sys.argv[1:], 'c:g:fdh',['config=','geometry=','help'])
+    for k,v in opts:
+        if k == '-c' or k == '--config':
+            config_file = v
+        if k == '-g' or k == '--geometry':
+            geometry = v
+            fullscreen = False
+        if k == '-f':
+            fullscreen = False
+        if k == '-d':
+            decorate = False
+        if k == '-h' or k == '--help':
+            help()
+            sys.exit()
+    if fullscreen:
+        decorate = False
+
+    return fullscreen, geometry, decorate
+
+
 def dict_from_keylist(keys):
         return dict([((k.key,tuple(k.mods)), k) for k in keys])

@@ -568,7 +617,14 @@ class GUI(object):
         self.window.connect('destroy', self.exit)
         self.window.connect('key-press-event', self.on_key)
         self.window.connect('key-release-event', self.key_released)
-        self.window.fullscreen()
+
+        fullscreen,geometry,decorate = parse_cmdline()
+        if fullscreen:
+            self.window.fullscreen()
+        else:
+            self.window.parse_geometry(geometry)
+        self.window.set_decorated(decorate)
+
         self.window.add(self.main)

         self.window.show_all()
ViewGit