{"id":3740,"library":"plac","title":"Plac: The Smartest Command Line Arguments Parser","description":"Plac is a Python library (version 1.4.5) that simplifies command-line argument parsing by automatically generating parsers from function signatures. It works across various Python 3 versions and is known for its minimal dependencies, often relying only on the standard library. Releases are somewhat infrequent but target Python compatibility updates.","status":"active","version":"1.4.5","language":"en","source_language":"en","source_url":"https://github.com/ialbert/plac","tags":["command-line","cli","argparse","parsing","decorators","utilities"],"install":[{"cmd":"pip install plac","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The primary way to import the library.","symbol":"plac","correct":"import plac"},{"note":"The entry point for plac to parse arguments and execute your main function is `plac.call()`.","wrong":"plac.main(main_function)","symbol":"call","correct":"plac.call(main_function)"}],"quickstart":{"code":"import plac\n\ndef main(model, iter: ('iterations for training', 'option', 'i', int)=100, debug: ('enable debug mode', 'flag', 'd')=False):\n    \"\"\" A script for machine learning \"\"\"\n    print(f\"Model: {model}, Iterations: {iter}, Debug: {debug}\")\n\nif __name__ == '__main__':\n    plac.call(main)","lang":"python","description":"This quickstart demonstrates how Plac automatically generates command-line arguments from a function's signature and annotations. Run this script and try `python your_script.py my_model -i 200 -d` or `python your_script.py -h` to see the generated help message."},"warnings":[{"fix":"Migrate any `plac` server implementations to use `asyncio` or another modern asynchronous framework. The `plac` core CLI parsing remains unaffected.","message":"The `plac` server functionality, which relied on Python's `asyncore` and `asynchat` modules, was removed in `plac` v1.4.0 due to their deprecation and subsequent removal in Python 3.12+. This affects users of `plac`'s experimental server, which is no longer directly supported by `plac` itself.","severity":"breaking","affected_versions":"1.4.0+"},{"fix":"Rename function arguments to avoid Python keywords if they are intended to be exposed as command-line options/flags.","message":"Plac does not support the `destination` concept found in `argparse`. If an argument name in your function signature clashes with a Python keyword (e.g., trying to define `--yield`), it's impossible to implement directly. You must change the argument name or use `argparse` if such a name is critical.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use positional arguments for truly required inputs. If an option-like syntax is desired for a required value, consider custom validation or switch to `argparse` directly for more fine-grained control over 'required' options.","message":"Plac does not directly support 'required options'. Its design philosophy aligns with `argparse`'s guideline that options should generally be optional. If a parameter is truly required, it should typically be defined as a positional argument in your function signature.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For testing `plac` applications, it's recommended to wrap `plac.call()` in a `try...except SystemExit` block to catch the exit gracefully and inspect `sys.stderr` for error messages without terminating the test runner.","message":"`plac.call()` handles invalid command-line input by printing an error message to `stderr` and then raising a `SystemExit`. This behavior, while standard for CLI applications, can disrupt automated tests by abruptly exiting the Python interpreter.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Adjust tests to be less brittle regarding whitespace and formatting in help messages, or conditionally update expected help strings based on the Python version being used. Focus on verifying key components of the help message rather than exact character-for-character matches.","message":"Python 3.13.0a4 and newer versions have introduced changes in the help text output format. This can cause existing `plac` application tests to fail if they rely on exact string comparisons of the generated `help` output.","severity":"breaking","affected_versions":"1.4.3+ on Python 3.13+"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}