Source code for script_options.standard_parsers

'''
--------------------
standard_parsers.py
--------------------

This module contains a set of commands initializing standard 
:py:class:`argparse.ArgumentParser` objects with standard sets of pre-defined 
options. The idea here is that I have a standard basic parser with set syntax
but can also have a 'cluster parser' with a set of pre-defined cluster oriented
options that can be added to the standard basic parser for scripts that need
cluster support, etc etc

.. moduleauthor:: Nick Schurch <nschurch@dundee.ac.uk>

:created_on: 2013-04-08

----------------
'''

__version__ = "1.0"

import argparse, tempfile
import custom_callables as cc

[docs]def standard_parser(ver, prog=None, usage=None, description=None, epilog=None, tmpdir=True, infile=True, infiletype="txt", outfile=True): '''Set up a command line parser with standard options. Depending on the options supplied to the function the standard options include an input file, an output file, a log file, a temporary directory a verbosity switch and standard :py:class:`argparse.ArgumentParser` version and help switches. ''' formatter = argparse.ArgumentDefaultsHelpFormatter parser = argparse.ArgumentParser(prog=prog, usage=usage, description=description, epilog=epilog, formatter_class=formatter, add_help=False) reqarggroup = parser.add_argument_group('Standard Options (required)') ########## input file ######### if infile: infilehelp = "Specify an input %s file (inc. path if different from " \ "the current working directory) to be consumed by this " \ "script." % infiletype reqarggroup.add_argument('infile', action = 'store', type = cc.input_file, help = infilehelp ) ########## output file ######### if outfile: outfilehelp = "Specify an output file (inc. path if different from " \ "the current working directory) to be generated by " \ "this script." reqarggroup.add_argument('outfile', action = 'store', type = cc.output_file, help = outfilehelp ) ########## log file ######### loghelp = "Specify a log file (inc. path if different from the current " \ "working directory) of the log file generated by this script." reqarggroup.add_argument('-l', '--log', action = 'store', dest='log', required=True, type = cc.output_file, help = loghelp ) optarggroup = parser.add_argument_group('Standard Options (optional)') ########## tmpdir ######### if tmpdir: tmpdirhelp = "Specify a directory to use as a temp dir for this " \ "script. If --tmpdir is listed without a directory, or " \ "is omitted entirely, then a system-generated tmpdir " \ "will be used." optarggroup.add_argument('--tmpdir', nargs = '?', action = 'store', dest='tmpdir', const = tempfile.mkdtemp(), default = tempfile.mkdtemp(), type = cc.output_path, help = tmpdirhelp ) ########## version, verbose, help ######### optarggroup.add_argument('--version', action = 'version', dest = 'version', version = '%(prog)s'+' %s' % str(ver) ) optarggroup.add_argument('-v', '--verbose', action = 'store_true', dest = 'verbose', help = 'Verbosity switch for logging and warnings' ) optarggroup.add_argument('-h', '--help', action = 'help', help = "Show this help message and exit" ) return(parser)