def args_to_str(
        args,
        positional_arg=None,
        fields=None,
        to_flag=True,
        arg_joiner=' ',
        val_sep=' ',
        list_joiner=' ',
        ):
    """Convert an argparse.ArgumentParser object back into a string,
    e.g. for running an external command."""
    def val_to_str(v):
        if isinstance(v, list):
            return list_joiner.join(map(str, v))
        return str(v)

    def arg_to_str(k, v):
        if to_flag:
            k = f"--{k.replace('_', '-')}"
        if v is True:
            return k
        if v is False:
            return ""
        else:
            v = val_to_str(v)
        return k + val_sep + v

    if positional_arg:
        pos_args = val_to_str(args.__dict__[positional_arg]) + val_sep
    else:
        pos_args = ""

    if fields is None:
        items = args.__dict__.items()
    else:
        items = [(k, args.__dict__[k]) for k in fields]

    return pos_args + arg_joiner.join([
        arg_to_str(k, v)
        for k, v in items
        if v is not None and k != positional_arg])