Source code for script_options.standard_argparse

'''
--------------------
standard_argparse.py
--------------------

A module that creates a :py:class:`argparge.ArgumentParser` with a standard 
set of initial options

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

:created_on: 2013-04-08

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

__version__ = "1.0"

import argparse, tempfile
import options_parsing as op

[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''' 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 = op.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 = op.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 = op.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 = op.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)