Shell Smith is a Clojure library that simplifies the creation of command-line interface (CLI) tools by providing a robust configuration management system with smart defaults based on your namespace.
Key Features
- Namespace-Aware Configuration: One of Shell Smith's most powerful features is its ability to automatically use the current namespace as the default name for:
- Configuration file naming (
<name>.yml
) - Environment variable prefix pattern
- When no explicit
:name
option is provided
- Configuration file naming (
- Multi-source Configuration: Shell Smith manages configurations from:
- Default values
- YAML configuration files
- Environment variables
- Command-line arguments
How It Works
The library uses a config macro that merges configuration values from different sources. Here's an example showing both explicit naming and namespace-based defaults:
;; With explicit naming (def config-explicit (shell-smith/config usage :defaults {:port 3000} :name "mytool")) ;; Using namespace-based defaults ;; If your namespace is 'my.app.server' ;; - Will look for 'my.app.server.yml' ;; - Will match env vars starting with 'MY_APP_SERVER_' (def config-from-ns (shell-smith/config usage :defaults {:port 3000}))
Configuration Resolution
The configuration sources are merged in this order (from lowest to highest priority):
- Default values
- YAML configuration file (
<name>.yml
in current working directory) - Environment variables (matching pattern
<UPPERCASE_NAME>_*
) - Command-line arguments
For example, if your namespace is my.app.tool, Shell Smith will automatically:
- Look for my.app.tool.yml in the current directory
- Match environment variables starting with
MY_APP_TOOL_
This convention-over-configuration approach makes it incredibly easy to organize your CLI tool's configuration while maintaining flexibility when needed.
If you liked this post, please consider supporting our Free and Open Source software work – you can sponsor us on Github and Patreon or star our FLOSS repositories.