Cligen

CLI code generator that creates command-line parsers from YAML specifications for Python projects.

What it is

Cligen is a developer utility that generates command-line parsing code for Python applications. It reads a YAML specification and writes a complete __main__.py file that handles argument parsing, subcommands, and configuration loading. The tool requires Python 3.8 or higher and produces code that runs without external dependencies beyond the standard library.

The generator supports complex command structures including nested subcommands that inherit options from parent parsers. Users can define multiple long and short options for each parameter, specify smart types like date parsing with relative expressions, and set default values that can be overridden by configuration files in YAML, INI, or PON formats. The YAML specification uses custom tags for concise definitions, with shorthand aliases such as !H for help text and !Opt for options.

Configuration file handling follows XDG directory standards across platforms. The generated code automatically checks for a --config flag to load explicit configuration paths. Default values from config files merge with command-line arguments, allowing flexible deployment scenarios. Cligen can generate code that instantiates classes and calls methods, invokes functions directly, or executes inline code embedded in the YAML specification.

Performance is a notable improvement over the earlier argparse-based generator. The current implementation produces code roughly twice the size but twice as fast. The legacy argparse generator remains available via a command-line flag for compatibility. This makes cligen suitable for projects where startup latency matters, such as frequently invoked CLI tools or containerized environments.

Developers building command-line interfaces in Python will find cligen useful for reducing boilerplate while maintaining full control over parsing behavior. The YAML-driven approach enables programmatic manipulation of CLI definitions, making it easier to enforce consistency across multiple utilities in an organization. The tool is distributed as open-source software with documentation hosted on Read the Docs.

This description was generated by AI from the site's public content and may contain errors.

What you can do there

  • Generate CLI parsing code from YAML specifications
  • Support nested subcommands with option inheritance
  • Load defaults from YAML INI or PON configuration files
  • Produce fast dependency-free __main__.py modules
  • Define command structures using concise YAML tags

Who it's for

Python developers building command-line interfaces who want to reduce boilerplate and manage CLI definitions in YAML.

Frequently asked questions about cligen.readthedocs.io

›What Python version does cligen require?

Cligen requires Python 3.8 or higher to run the generated code.

›Can cligen generate code that uses argparse?

Yes, the legacy argparse generator can be invoked with the --type argparse flag, though it is unmaintained and slower than the default generator.

›What configuration file formats does cligen support?

Cligen supports YAML, INI, and PON configuration files, with JSON support planned. YAML requires ruamel.yaml to be installed.

›How does cligen handle subcommand inheritance?

Subcommands can inherit options from their parent parser, allowing parent options to appear before or after the subcommand without reordering the command line.

›Where does cligen look for user configuration files?

Configuration files are searched in XDG config directories on Linux and macOS, and under %APPDATA% on Windows, using the package name as the default basename.

›Can I define CLI options inline in __init__.py instead of a separate YAML file?

Yes, the YAML specification can be stored in the _cligen_data variable within __init__.py to reduce file clutter in the project root.

›Does cligen have any runtime dependencies?

The generated code depends only on the Python standard library unless the configuration file format requires an external library such as ruamel.yaml for YAML parsing.