Skip to content. | Skip to navigation

Sections
Personal tools
You are here: Home How To Python CLI Program Skeleton

CLI Program Skeleton

This is skeleton code for a command-line interface (CLI) program.

I make these CLI programs fairly frequently as personal utilities and dislike re-writing this boilerplate. It may be amenable to a library, but I haven't put the time in to generalize it.

#! /usr/bin/env python


import sys
from optparse import OptionParser
from textwrap import dedent


__version__ = '0.1'

def header(width=70):
  hline = "=" * width
  print hline
  print (" make-surveys v.%s " % __version__).center(width)
  print hline

def doopts():
  usage = 'usage: make-survey [-i inputsdir] texfile'
  parser = OptionParser(usage)
  parser.add_option("-i", "--inputs", dest="inputs",
           default='Inputs',
           help="directory where inputs are",
           metavar="INPUTS")
  return parser

def usage(parser, msg=None, width=70):
  err = ' ERROR '.center(width, '#')
  errbar = '#' * width
  hline = '=' * width
  if msg is not None:
     print '\n'.join(('', err, msg, errbar, ''))
  print hline
  print
  print parser.format_help()
  sys.exit(0)

def main():
  header()
  parser = doopts()
  (options, args) = parser.parse_args()

  try:
    texfile = args[0]
  except IndexError:
    usage(parser, 'No texfile specified.')

  '\newcommnand{\version}{version-0%s}'


if __name__ == "__main__":
  main()

 

Document Actions