Added `be --verbose-version' option to print full version information.
[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("--verbose-version", action="store_true", dest="verbose_version",
35                   help="Print verbose version information and exit.")
36 parser.add_option("-d", "--dir", dest="dir", metavar="DIR",
37                   help="Run this command from DIR instead of the current directory.")
38
39 try:
40     options,args = parser.parse_args()
41     for option,value in cmdutil.option_value_pairs(options, parser):
42         if value == "--complete":
43             if option == "dir":
44                 if len(args) == 0:
45                     args = ["."]
46                 paths = cmdutil.complete_path(args[0])
47                 raise cmdutil.GetCompletions(paths)
48 except cmdutil.GetHelp:
49     print cmdutil.help(parser=parser)
50     sys.exit(0)
51 except cmdutil.GetCompletions, e:
52     print '\n'.join(e.completions)
53     sys.exit(0)
54
55 if options.version == True or options.verbose_version == True:
56     print version.version(verbose=options.verbose_version)
57     sys.exit(0)
58 if options.dir != None:
59     os.chdir(options.dir)
60
61 try:
62     if len(args) == 0:
63         raise cmdutil.UsageError, "must supply a command"
64     sys.exit(cmdutil.execute(args[0], args[1:]))
65 except cmdutil.GetHelp:
66     print cmdutil.help(sys.argv[1])
67     sys.exit(0)
68 except cmdutil.GetCompletions, e:
69     print '\n'.join(e.completions)
70     sys.exit(0)
71 except cmdutil.UnknownCommand, e:
72     print e
73     sys.exit(1)
74 except cmdutil.UsageError, e:
75     print "Invalid usage:", e
76     if len(args) == 0:
77         print cmdutil.help(parser=parser)
78     else:
79         print "\nArgs:", args
80         print cmdutil.help(sys.argv[1])
81     sys.exit(1)
82 except cmdutil.UserError, e:
83     print "ERROR:"
84     print e
85     sys.exit(1)