19
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
import os, sys, getopt
29
29
def __init__( self, version ):
30
self.name = os.path.basename( sys.argv[ 0 ] )
30
31
self.version = version
31
self.program = os.path.basename( sys.argv[ 0 ] )
34
def die( self, error_message ):
35
prefix = self.name + ( ' ' + the.command if the.command else '' )
36
print >> sys.stderr, '%s: %s' % ( prefix, error_message )
34
40
def print_usage( self, error_message ):
35
print >> sys.stderr, self.program + ": " + error_message
36
print >> sys.stderr, "Try '" + self.program + " --help' for more information."
41
suffix = ' ' + the.command if the.command else ''
42
self.die( error_message + \
43
"\nTry '%s%s --help' for more information." % \
44
( self.name, suffix ) )
40
47
def print_help( self ):
41
print "Usage: " + self.program + " COMMAND [OPTION]..."
48
print "Usage: " + self.name + " COMMAND [OPTION]..."
43
50
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789
44
51
print "Manage your home directories, across multiple computers, similar to how you"
64
71
print "For help about a particular command (including the additional options that the"
65
72
print "command accepts) try typing:"
66
print " $ " + self.program + " COMMAND --help"
73
print " $ " + self.name + " COMMAND --help"
132
140
sys.argv[ 1: ], "",
133
141
[ "help", "version" ] )
135
for opt, optargs in opts:
143
for opt, optarg in opts:
136
144
# we only show help if there are no non-option arguments (e.g.,
137
145
# a command) specified. If a command has been specified it will
138
146
# have to be parsed and --help will be handled by it, instead)
142
150
self.print_version()
144
152
except getopt.GetoptError as e:
145
# ignore any errors -- we aren't parsing the command line properly
153
# ignore errors -- we aren't parsing the command line properly yet
148
156
# find the first non-option argument (the command)
149
self.command = self.get_command_argument( sys.argv[ 1: ] )
150
if self.command == None:
157
the.command = self.get_command_argument( sys.argv[ 1: ] )
158
if the.command == None:
151
159
self.print_usage( "missing command" )
153
161
# check command is valid
154
self.command = self.check_command( self.command )
155
if self.command == None:
162
the.command = self.check_command( the.command )
163
if the.command == None:
156
164
self.print_usage( "bad command" )
158
166
# calculate class name
159
bits = self.command.split( '-' )
167
bits = the.command.split( '-' )
160
168
class_name = 'Command'
162
170
class_name += bit[ 0 ].upper() + bit[ 1 : ]
164
# instantiate and run the command class
165
module = __import__( 'stdhome.command_' + self.command )
166
the_class = getattr( module, class_name )
167
instance = the_class()
172
# instantiate the command class
173
module = __import__( 'stdhome.command_' + the.command,
174
fromlist = [ class_name ] )
175
instance = getattr( module, class_name )()
179
instance.parse_command_line()
181
except ( getopt.GetoptError, self.UsageError ) as e:
182
self.print_usage( e.msg )
183
except self.FatalError as e:
185
if the.verbose and e.output:
186
message += '\n\nOUTPUT:\n' + e.output
190
class UsageError( Exception ):
192
def __init__( self, error_message ):
193
self.msg = error_message
196
class FatalError( Exception ):
198
def __init__( self, message, output = None ):