{"id":5853,"library":"argparse-dataclass","title":"Argparse-Dataclass","description":"argparse-dataclass simplifies creating command-line interfaces by automatically generating `argparse` arguments from Python dataclasses. It uses type hints for argument type inference and supports features like default values, help messages, and argument groups. The current version is 2.0.0, and it generally follows an as-needed release cadence for bug fixes and feature enhancements.","status":"active","version":"2.0.0","language":"en","source_language":"en","source_url":"https://github.com/mivade/argparse_dataclass","tags":["cli","argparse","dataclasses","configuration"],"install":[{"cmd":"pip install argparse-dataclass","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"ArgumentParser","correct":"from argparse_dataclass import ArgumentParser"}],"quickstart":{"code":"from dataclasses import dataclass\nfrom argparse_dataclass import ArgumentParser\n\n@dataclass\nclass Config:\n    input_file: str\n    output_dir: str = 'output'\n    verbose: bool = False\n    port: int = 8080\n\ndef main():\n    parser = ArgumentParser(Config)\n    config = parser.parse_args(['--input-file', 'data.txt', '--verbose'])\n    print(f\"Input: {config.input_file}, Output: {config.output_dir}, Verbose: {config.verbose}, Port: {config.port}\")\n\nif __name__ == '__main__':\n    main()","lang":"python","description":"Define your CLI arguments using a dataclass, then pass the dataclass type to ArgumentParser to automatically generate and parse arguments. This example demonstrates basic type inference and default values."},"warnings":[{"fix":"Change `parser = ArgumentParser(Config())` to `parser = ArgumentParser(Config)`.","message":"In version 2.0.0, the `ArgumentParser` constructor now expects a dataclass *type* (e.g., `Config`) instead of a dataclass *instance* (e.g., `Config()`).","severity":"breaking","affected_versions":"2.0.0 and above"},{"fix":"Update custom dataclasses to use `_argument_settings` instead of `argument_settings` for field-specific argument configuration.","message":"The internal attribute `argument_settings` (used for configuring argument parsing behavior within a dataclass) was renamed to `_argument_settings` in version 2.0.0 to signify its internal, non-public nature.","severity":"breaking","affected_versions":"2.0.0 and above"},{"fix":"Always provide explicit and correct type hints for all dataclass fields intended to become CLI arguments. For complex types, ensure they are supported or define custom parsers.","message":"`argparse-dataclass` heavily relies on Python type hints to infer the correct `argparse` argument types (e.g., `str`, `int`, `bool`, `list`). Omitting or providing incorrect type hints can lead to unexpected parsing behavior or errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To get a simple store_true/store_false flag, configure the field using `_argument_settings(action='store_true')` or `_argument_settings(action='store_false')` explicitly, or set `boolean_flags=False` when initializing `ArgumentParser`.","message":"Boolean dataclass fields automatically generate both `--field` (store `True`) and `--no-field` (store `False`) arguments by default. If you only expect a single flag without its negation, this might be unexpected.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}