console python

Good code drop for any Python command line is

import os
import re
import pprint 
from optparse import OptionParser
import logging

parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
                  help="Files to Find", metavar="FILE")
parser.add_option("-r", "--replace",
                  dest="replace", 
                  help="string to replace: A Reg ex is used")
parser.add_option("-w", "--write",
                  dest="write", 
                  default=False,
                  help="Should I execute this command True/False")
parser.add_option("-d", "--debug",
                  dest="debug", 
                  default=True,
                  help="Debug Mode True/False")
(options, args) = parser.parse_args()


if options.debug==True:
    logging.basicConfig(level=logging.DEBUG)
    logging.debug('Debug Enabled')
else:
    logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)

logger.info('Starting')


'''Code Here

logger.info("Program Finishing")

It provides quick

  • logging
  • parser options