Updated GPLv2 to current GPLv2.
[be.git] / be
1 #!/usr/bin/env python
2 # Copyright (C) 2005-2009 Aaron Bentley and Panometrics, Inc.
3 #                         Chris Ball <cjb@laptop.org>
4 #                         Oleg Romanyshyn <oromanyshyn@panoramicfeedback.com>
5 #                         W. Trevor King <wking@drexel.edu>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 import os
22 import sys
23
24 from libbe import cmdutil, _version
25
26 __doc__ = cmdutil.help()
27
28 usage = "be [options] [command] [command_options ...] [command_args ...]"
29
30 parser = cmdutil.CmdOptionParser(usage)
31 parser.command = "be"
32 parser.add_option("--version", action="store_true", dest="version",
33                   help="Print version string and exit.")
34 parser.add_option("-d", "--dir", dest="dir", metavar="DIR",
35                   help="Run this command from DIR instead of the current directory.")
36
37 try:
38     options,args = parser.parse_args()
39     for option,value in cmdutil.option_value_pairs(options, parser):
40         if value == "--complete":
41             if option == "dir":
42                 if len(args) == 0:
43                     args = ["."]
44                 paths = cmdutil.complete_path(args[0])
45                 raise cmdutil.GetCompletions(paths)
46 except cmdutil.GetHelp:
47     print cmdutil.help(parser=parser)
48     sys.exit(0)
49 except cmdutil.GetCompletions, e:
50     print '\n'.join(e.completions)
51     sys.exit(0)
52
53 if options.version == True:
54     print _version.version_info["revision_id"]
55     sys.exit(0)
56 if options.dir != None:
57     os.chdir(options.dir)
58
59 try:
60     if len(args) == 0:
61         raise cmdutil.UsageError, "must supply a command"
62     sys.exit(cmdutil.execute(args[0], args[1:]))
63 except cmdutil.GetHelp:
64     print cmdutil.help(sys.argv[1])
65     sys.exit(0)
66 except cmdutil.GetCompletions, e:
67     print '\n'.join(e.completions)
68     sys.exit(0)
69 except cmdutil.UnknownCommand, e:
70     print e
71     sys.exit(1)
72 except cmdutil.UsageError, e:
73     print "Invalid usage:", e
74     if len(args) == 0:
75         print cmdutil.help(parser=parser)
76     else:
77         print "\nArgs:", args
78         print cmdutil.help(sys.argv[1])
79     sys.exit(1)
80 except cmdutil.UserError, e:
81     print "ERROR:"
82     print e
83     sys.exit(1)