File: goption.py

package info (click to toggle)
gnome-python 2.28.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,288 kB
  • sloc: sh: 10,219; ansic: 7,997; xml: 2,464; python: 1,886; makefile: 392
file content (36 lines) | stat: -rw-r--r-- 1,046 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python

"""
http://live.gnome.org/GnomeGoals/PoptGOption

Since GNOME 2.10, GLib provides GOption, a commandline option parser.
The help output of your program will be much nicer :-) And it will enable us to
slowly get rid of popt (even if libgnome will still have to depend on it for
compatibility reasons). 

"""

import sys

import glib
import gnome

def callback(name, value, group):
    if name == "--example":
        print "example got %s" % value
    elif name in ("-o", "--option"):
        print "option"
    else:
        print "remaining:", value

group = glib.OptionGroup(None, None, None, callback)
group.add_entries([("example", "\0", 0, "An example option",
                    "option"),
                   ("option", "o", glib.OPTION_FLAG_NO_ARG, "An option",
                    None),
                   (glib.OPTION_REMAINING, "\0", 0, "", None),
                  ])
context = glib.OptionContext("argument")
context.set_main_group(group)

prog = gnome.init("myprog", "1.0", argv=sys.argv, option_context=context)