1#!/usr/bin/env python
2#
3# Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License
7# as published by the Free Software Foundation; either version 2
8# of the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18# 02110-1301, USA.
19#
20
21import getopt
22import sys
23
24def usage ():
25    print "Usage:"
26    print "  xxx.py [OPTION...] - Input data into tracker"
27    print ""
28    print "Help Options:"
29    print "  -h, --help             Show help options"
30    print ""
31    print "Application Options:"
32    print "  -g, --graphics         Enable GTK interface"
33    print "  -p, --period=NUM       Time (in sec) between insertion message"
34    print "  -s, --size=NUM         Amount of instances in the message"
35    print "  -t, --timeout=NUM      Switch off the program after NUM seconds"
36    print ""
37
38
39def parse_options (graphic_mode=False, period=1, msgsize=1, timeout=0):
40    try:
41        opts, args = getopt.getopt(sys.argv[1:],
42                                   "gp:s:t:h",
43                                   ["graphics", "period", "size", "timeout", "help"])
44    except getopt.GetoptError, err:
45        # print help information and exit:
46        print str (err) # will print something like "option -a not recognized"
47        usage ()
48        sys.exit (2)
49
50    options = { "graphic_mode" : graphic_mode ,
51                "period"  : period,
52                "msgsize" : msgsize,
53                "timeout" : timeout }
54
55
56    for o, a in opts:
57        if o in ["-g", "--graphics"]:
58            options ['graphic_mode'] = True
59
60        elif o in ["-p", "--period"]:
61            options ['period'] = int(a)
62
63        elif o in ["-s", "--size"]:
64            options ['msgsize'] = int (a)
65
66        elif o in ["-t", "--timeout"]:
67            options ['timeout'] = int (a)
68        elif o in ["-h", "--help"]:
69            usage ()
70            sys.exit (-1)
71        else:
72            usage ()
73            assert False, "unhandled option"
74
75    return options
76
77