Source code for friendlyargs.fargs

"""Decorators that simplify interacting with the FriendlyArgs framework"""
from functools import update_wrapper
import sys
import logging


[docs]def main(): """Decorates the primary entry-point function for your command line application""" def _main_decorator(func): log = logging.getLogger(__name__) log.debug("MainDecorator: {0}".format(func.__name__)) def _nested_main(): retval = func() if isinstance(retval, int): return retval return 0 return update_wrapper(_nested_main, func) return _main_decorator
[docs]def count(short_name, long_name): """Decorates a command function, providing an optional parameter which counts the occurrences of a character. Useful for things like verbosity flags which look like '-vvvv' Args: short_name (str): single character parameter short name, of the form "-a" or "-b" long_name (str): verbose descriptive name for the count parameter """ log = logging.getLogger(__name__) assert len(short_name) == 2 and short_name[0] == "-" assert long_name.startswith("--") def _count_decorator(func): log.debug("CountDecorator: {0}".format(func.__name__)) def _nested_count(*args): parts = sys.argv param_count = 0 for cur_part in parts: if cur_part == long_name: param_count += 1 if cur_part.startswith(short_name): num_vals = len(cur_part) - 1 sample = "-" + (short_name[1] * num_vals) if sample == cur_part: param_count += num_vals return func(param_count, *args) return update_wrapper(_nested_count, func) return _count_decorator
if __name__ == "__main__": pass