Back to index

python-cliapp  1.20120630
example.py
Go to the documentation of this file.
00001 # Copyright (C) 2011  Lars Wirzenius
00002 # 
00003 # This program is free software; you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation; either version 2 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License along
00014 # with this program; if not, write to the Free Software Foundation, Inc.,
00015 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 
00017 
00018 '''Example for cliapp framework.
00019 
00020 This implements an fgrep-like utility.
00021 
00022 '''
00023 
00024 
00025 import cliapp
00026 import logging
00027 
00028 
00029 class ExampleApp(cliapp.Application):
00030 
00031     '''A little fgrep-like tool.'''
00032     
00033     def add_settings(self):
00034         self.settings.string_list(['pattern', 'e'], 
00035                                   'search for regular expression PATTERN',
00036                                    metavar='REGEXP')
00037 
00038         self.settings.boolean(['dummy'], 'this setting is ignored',
00039                               group='Test Group')
00040 
00041     # We override process_inputs to be able to do something after the last
00042     # input line.
00043     def process_inputs(self, args):
00044         self.matches = 0
00045         cliapp.Application.process_inputs(self, args)
00046         self.output.write('There were %s matches.\n' % self.matches)
00047 
00048     def process_input_line(self, name, line):
00049         logging.debug('processing %s:%s' % (name, self.lineno))
00050         for pattern in self.settings['pattern']:
00051             if pattern in line:
00052                 self.output.write('%s:%s: %s' % (name, self.lineno, line))
00053                 self.matches += 1
00054                 logging.debug('Match: %s line %d' % (name, self.lineno))
00055     
00056     
00057 app = ExampleApp(version='0.1.2')
00058 app.settings.config_files = ['example.conf']
00059 app.run()
00060