import copy


def modify_namespace(namespace, **kwargs):
    """Update arguments in a passed Namespace.

    :param namespace:       argparse.Namespace object
    :param kwargs:          keyworded arguments

    The expected usage pattern is, for a command-line application with many
    or complex arguments, to define a base argparse.Namespace object, then
    change only a few arguments, specific to a test. This function takes
    a base namespace and a dictionary of argument: value pairs, and
    returns the modified namespace.
    """
    new_namespace = copy.deepcopy(namespace)
    for argname, argval in kwargs.items():
        setattr(new_namespace, argname, argval)
    return new_namespace