How to get arguments from ArgParser to write to a textfile so it can be read with fromfile_prefix_chars

Sumary

I want a script to save all input arguments to a text file in such a format that I can re-run the script using fromfile_prefix_chars with that file. This is to allow for easy reproducability and store the arguments that were used to generate certain data.

I have a very brittle and incomplete implementation. I’m sure there is a better way but have not found it via google.
** Q: how to capture a) all arguments with b) clean and concise code ?**

Workflow Example

  1. Run script python script.py --count 7 --channel-stdev 17 /somepath
    This should produce a text file called args_out.txt with the content
--count
1
--channel-mean
4.5
--channel-stdev
3
/somepath
  1. I can re-run the script using python script.py @args_out.txt

Minimal example

from pathlib import Path
import logging
import argparse
from argparse import Namespace, ArgumentParser
from typing import Optional, List, Tuple


def get_arg_parser() -> ArgumentParser:
    parser = ArgumentParser(description='Test',
                            fromfile_prefix_chars='@')
    parser.add_argument('--count', default=1, type=int,
                        help='Number of ')
    parser.add_argument('--channel-mean', default=4.5, type=float,
                        help='Mean number ')
    parser.add_argument('--channel-stdev', default=3, type=float,
                        help='Standard deviation ')

    # Logging control
    log_group = parser.add_mutually_exclusive_group()
    log_group.add_argument('-v', '--debug', action='store_true', help='Enable debug mode')
    log_group.add_argument('-q', '--quiet', action='store_true', help='Enable quiet mode')

    # I/O control
    parser.add_argument('input', nargs='+', help='Path to a single')
    return parser


def parse_args(arg_list: Optional[List[str]] = None) -> Namespace:
    parser = get_arg_parser()
    return parser.parse_args(arg_list)


def convert_args_to_text(args: Namespace, parser: ArgumentParser) -> str:
    """ Convert an argparse.Namespace object to a list of strings that can be written to a file and then
    read as arguments again. """
    required, optionals, optionals_store_true = get_arg_name_to_argument_dicts(parser)

    text = ''
    for k, v in vars(args).items():
        k_dash = k.replace('_', '-')
        if k_dash in optionals:
            text = add_key_and_value(text, key=k_dash, value=v)

        elif k_dash in optionals_store_true:
            if v:
                text = add_key_and_value(text, key=v, value=None)

        elif k_dash in required:
            text = add_key_and_value(text, key=None, value=v)
        else:
            logging.warning(f"skipping argument {k}")

    return text


def get_arg_name_to_argument_dicts(parser: ArgumentParser) -> Tuple[dict, dict, dict]:
    """ Here I try to extract the argument names and whether they are optional or required from the ArgParser
    This is very brittle FIXME """
    optionals = {}
    optionals_store_true = {}  # These are only added if they are true
    required = {}
    for optk, optv in parser._optionals._option_string_actions.items():
        if not optk.startswith('--'):
            # Skip short options
            continue

        if isinstance(optv, argparse._StoreAction):
            optionals[optk.strip('--')] = optv
        elif isinstance(optv, argparse._StoreTrueAction):
            optionals_store_true[optk.strip('--')] = optv

    for req_group_actions in parser._positionals._group_actions:
        name = req_group_actions.dest
        required[name] = req_group_actions

    return required, optionals, optionals_store_true


def add_key_and_value(text: str, key: Optional[str], value: Optional[Tuple[str, List]]) -> str:
    """ Add a argument key and value to a text block. """

    if key is not None:
        text += f"--{key}n"

    if value is None:
        return text

    if isinstance(value, list):
        for vi in value:
            text += f'{vi}n'
    else:
        text += f'{value}n'

    return text


def main():
    args = parse_args()

    print(args)

    text = convert_args_to_text(args, get_arg_parser())
    with open('args_out.txt', 'w') as f:
        f.write(text)


if __name__ == '__main__':
    main()

Thank you for your help

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật