Skip to main content

Develop CLI tools with top-notch config management like a boss.

Project description

Hydraclick

Hydraclick is an open-source Python package that seamlessly integrates Hydra and Click to create production-grade command-line interfaces (CLIs). It leverages Hydra's powerful configuration management with Click's user-friendly CLI creation to provide a robust foundation for building complex CLI applications.

Features

  • Configuration Management: Utilize Hydra's dynamic configuration capabilities for flexible CLI options.
  • Structured Logging: Integrate with flogging for structured and efficient logging.
  • Easy Decorators: Simple decorators to convert functions into CLI commands.
  • Extensible: Easily extend and customize to fit your project's needs.
  • Shell Completion: Support for generating shell completion scripts.

Installation

Install Hydraclick via pip:

pip install hydraclick

Getting Started

Basic Usage

Define your function and decorate it with @hydra_command to create a CLI command.

from omegaconf import DictConfig
from hydraclick import hydra_command

@hydra_command(config_path="config", config_name="my_config")
def my_function(config: DictConfig):
    print(f"Running with config: {config.pretty()}")

Running the CLI

After defining your function, you can run it from the command line:

python my_script.py --config-path path/to/config --config-name my_config

Example

Here's a complete example of creating a CLI with Hydraclick:

import sys
from omegaconf import DictConfig
from hydraclick import hydra_command

@hydra_command(config_path="configs", config_name="app_config", run_mode="kwargs")
def main(**kwargs):
    print(f"Running with config: {kwargs}")

if __name__ == "__main__":
    main()

API Reference

hydra_command

Decorator to create CLI commands.

def hydra_command(
    config_path: str | Path | None = None,
    config_name: str | None = "config",
    version_base: str | None = None,
    run_mode: str = "config",
    preprocess_config: Callable[[DictConfig], DictConfig] | None = None,
    print_config: bool = True,
    resolve: bool = True,
    use_flogging: bool = True,
    **flogging_kwargs: Any,
) -> Callable:

Configuration Options

Hydraclick provides several configuration options to customize your CLI:

  • config_path: Path to the configuration directory. Passed to hydra.main()
  • config_name: Name of the configuration file. Passed to hydra.main()
  • version_base: Base version of the configuration. Passed to hydra.main()
  • run_mode: Mode to run the function ("config" or "kwargs").
    • "config": Pass the configuration as a single OmegaConf.DictCondig object.
    • "kwargs": Resolve the OmegaConf.DictConfig objet into a python dict and pass it as keyword arguments.
  • preprocess_config: Function to preprocess the configuration. It takes a DictConfig object and returns a DictConfig object.
  • print_config: Whether to print the configuration before execution.
  • resolve: Whether to resolve the configuration.
  • use_flogging: Whether to use flogging for structured logging.
  • **flogging_kwargs: Additional keyword arguments for flogging.

Logging with Flogging

Hydraclick integrates with flogging for structured logging. To enable flogging, ensure it's installed:

pip install hydraclick[flogging]
pip install flogging

If flogging is not available, Hydraclick will log a warning and disable structured logging.

Shell Completion

Hydraclick supports generating shell completion scripts. Use the --shell-completion option to generate scripts for your preferred shell.

cli_app command --shell-completion install=bash > my_script_completion.sh
source my_script_completion.sh

Contribution

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Commit your changes with clear messages.
  4. Submit a pull request detailing your changes.

For major changes, please open an issue first to discuss your ideas.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

If you encounter any issues or have questions, feel free to open an issue on the GitHub repository.

Acknowledgements

  • Hydra for powerful configuration management.
  • Click for creating beautiful CLIs.
  • Flogging for structured logging.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hydraclick-0.0.4.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

hydraclick-0.0.4-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file hydraclick-0.0.4.tar.gz.

File metadata

  • Download URL: hydraclick-0.0.4.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for hydraclick-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a0148d7c782eb3ddc33c476cdaedd656df1d80bc60bba09c6771350cd60ecdad
MD5 e415f5b870a63ba977f3ee709873fd9d
BLAKE2b-256 fafec8d5b5cb0353833528fd98529d3aee9bde814eb5a00bdc0088e822ad6124

See more details on using hashes here.

File details

Details for the file hydraclick-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: hydraclick-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for hydraclick-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ccf1998dae38d3f6e63cc68508bda368b2ad190ac8358b997965f87bb895e2b5
MD5 ab9a1241f93e79f51037ccb9bbdee50c
BLAKE2b-256 c60d0dab27648cb3d27628ae4bd49ede2e0ed9e69ac23fce789ff919ac9e15f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page