Package: runfunc
runfunc=======I liked optfunc but it hasn't been put on PyPI for actual use so I went aheadand rewrote it to use a different syntax to be able to include option helpand input validators.Basic Example=============import sysimport runfunc as rfclass Help(rf.Help):"""\This is a blurb about this program. Its very entertaining towatch and think about what we might do with it.Look ma. Formatted text.And stuff"""usage   = "%prog [options] value"value = rf.Check(int, "A value to multiply.")factor = rf.Check(float, "A factor by which to multiply.", opt='f')random = rf.Stream("r", "An input file to read from.", opt='r')verbose = rf.Flag("Multiply verbosely.", opt='v')def main(value, factor=2.0, random=sys.stdin, verbose=False):newval = value * factorif verbose:print "%d * %s = %s" % (value, factor, newval)else:print newvalrf.run(main, Help())Help Class==========The basic outline for using `runfunc` is to create a subclass of `runfunc.Help`and use provide this class when running the function. This subclass isresponsible for specifying all of the possible arguments to be passed to thefunction. It can contain extra arguments that are ignored so that the samesubclass can be reused for multiple command line scripts.class Help(rf.Help):"""\This is a blurb about this program. Its very entertaining towatch and think about what we might do with it.Look ma. Formatted text.And stuff"""usage   = "%prog [options] value"value = rf.Check(int, "A value to multiply.")factor = rf.Check(float, "A factor by which to multiply.", opt='f')random = rf.Stream("r", "An input file to read from.", opt='r')verbose = rf.Flag("Multiply verbosely.", opt='v')A couple points to note:* The class's doc string will be used as the program's help description* If a `usage` attribute is present, it will be used for the program's help* Any other attributes that inherit from `runfunc.Arg` will be available for use as a program argument.Running a function==================runfunc.run(callable, help_object, argv=None, check=True)* `callable` is callable object.* `help_object` is an instance of a class that inherits from `runfunc.Help`.* `argv` is a list of arguments. If None, `sys.argv[1:]` is used* `check=True` will prevent the function from running when imported as a module.Validator Types===============Check(func, desc, opt=None)---------------------------Use an arbitrary function to validate an input.* func - A callable taking a single argument. Raises an exception on error.* desc - Help message that describes the option* opt - A single character option name.Flag(desc, opt=None)--------------------Passes true based on the presence of an option. Generally only useful forkeyword arguments.* desc - Help message that describes the option* opt - A single character option name.List(desc, opt=None, validator=None)--------------------------Append each value seen to a list. Validator is applied before appending eachvalue.* desc - Help message that describes the option* opt - A single character option name.* validator - A callable taking a single argument. Raises an exception on error.Choice(choices, desc, opt=None, validator=None)-----------------------------------------------Limit input to a specified set of values. Validator is applied before testingfor membership in the set.* choices - A on object providing item access to test if an option is allowable.* desc - Help message that describes the option* opt - A single character option name.* validator - A callable taking a single argument. Raises an exception on error.Regexp(pattern, desc, opt=None, flags=0)----------------------------------------Require input to match a regular expression.* pattern - A string acceptable for `re.compile(pattern, flags)`* desc - Help message that describes the option* opt - A single character option name.* flags - Any modifiers for compiling the regular expressionEmail(desc, opt=None)---------------------Require input to look like a valid email address. Subclass of Regexp* desc - Help message that describes the option* opt - A single character option name.IpAddr(desc, opt=None)----------------------Require input to look like a valid IP Address. Subclass of Regexp.* desc - Help message that describes the option* opt - A single character option name.Path(flags, desc, opt=None)---------------------------Require that input specifies a path. Tests are executed depending on the flagspassed. FILE requires that the path looks like a file (no trailing pathspecifier). DIR requires the trailing path specifier. EXISTS will check if thepath actually exists. PARENT requires that the path's parent exist. Anycombination of flags can be passed though passing FILE | DIR would guarantee anerror during validation.* flags - An OR of FILE, DIR, EXISTS, PARENT* desc - Help message that describes the option* opt - A single character option name.Stream(mode, desc, opt=None)----------------------------Open a path for use as a `file` object. A useful pattern is to use a defaultvalue of `sys.stdin`, `sys.stdout`, or `sys.stderr` for common command linesemantics.* mode - Passed to `open(path, mode)` when opening the stream* desc - Help message that describes the option* opt - A single character option name.Custom Validators=================Custom validators can be created by subclassing the Arg class and overridingthe validate method.An example for specifying a Range validator would look something like this:import runfunc as rfclass Range(rf.Arg):def __init__(self, func, left, right, desc, opt=None):super(Range, self).__init__(desc, opt=opt)self.func = funcself.left = leftself.right = rightdef validate(self, option, optstr, value, parser):value = self.func(value)if value < self.left or value > self.right:raise rf.OptionValueError("Value is outside range.")setattr(parser.values, option.dest, value)Notice that func is specifiable so that users can convert the raw value toan appropriate data type.