#!/usr/local/bin/pythonw # coding: iso-8859-1 # Copyright © 2009 by Amos Newcombe # # This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with this program. If not, see . '''a generic command-line program to be used as a framework ''' import sys from optparse import OptionParser class commander: 'a base class to interpret a command line' # ¤ Metadata sUsage = None sVersion = '%prog 2008.06.22.0' # ¤ Creation def __init__(self, lsLine): 'Initialize self.' # print '%s(%s)' % (self.__class__.__name__, ', '.join(lsLine)) self.lsLine = lsLine self.optParser = OptionParser(usage=self.sUsage, version=self.sVersion) self.options() self.opt, self.lsArgs = self.optParser.parse_args() # print '%s(%s)' % (self.__class__.__name__, ', '.join(self.lsArgs)) self.ResolveOptions() def options(self): 'Populate the option parser with available options.' pass # STUB def ResolveOptions(self): 'Interpret the command line options.' pass # STUB # ¤ Description def __str__(self, sName=None): 'Reproduce the command line (bug: some args need quotes).' return ' '.join(self.lsLine) # BUG: args with spaces are not quoted def __repr__(self): 'Reproduce the Python instantiation call.' return '%s(%s)' % (self.__class__.__name__, repr(self.lsLine)) # ¤ Operation def main(self): 'Pass each command line argument to self.main1().' # print '%s.main()' % self.__class__.__name__ for sArg in self.lsArgs: self.main1(sArg) def main1(self, sArg): 'Echo a command line argument on its own line.' print 'arg: %s' % `sArg` # STUB: echo command line arguments class evaluator(commander): 'abstract base class for a commander that applies a function to arguments from the command line and optionally standard input' # ¤ Creation def options(self): 'Populate the option parser with available options.' commander.options(self) self.optParser.add_option('-i', '--stdin', action='store_true', dest='useStdin', default=False, help='get additional arguments from stdin' ) # ¤ Operation def main(self): 'Process the command line and optionally standard input.' if self.lsArgs: for sArg in self.lsArgs: print self.main1(sArg), print if self.opt.useStdin: for sLine in sys.stdin: for sArg in sLine.strip().split(): print self.main1(sArg), print def main1(self, sArg): 'Apply self.function() to the argument.' try : return self.function(sArg) except StandardError, ex: return 'ERROR: ' + repr(ex) def function(self, sArg): 'Define this to be the function you want to apply to the arguments.' raise NotImplementedError, '%s.function()' % self.__class__.__name__ if __name__ == '__main__': import sys commander(sys.argv).main()