Simple way to bind function arguments to the command line. An extended fork of pseeth/argbind with new features (imported as `argbind`).
Project description
ArgBind
Build CLIs via docstrings and type annotations, with YAML support.
Note: This is an extended fork of pseeth/argbind that adds new features — modern type annotations (PEP 585/604),
Literaland flexible boolean handling, dataclassdefault_factory, and YAML$include— published on PyPI asargbind-dbraun. The import name is unchanged (import argbind), so it remains a drop-in replacement.
ArgBind is a simple way to bind function or class arguments to the command line or to .yml files! It supports scoping of arguments, similar to other frameworks like Hydra and gin-config. ArgBind is very small (only ~800 lines of code, in one file), can be used to make complex and well-documented command line programs, and allows you to configure program execution from .yml files.
If you're migrating from an ArgParse script to an ArgBind script, check out the migration guide. Scroll down to see some examples. Please also look at the current known limitations of ArgBind.
Why ArgBind?
ArgBind was written by Prem Seetharaman to help configure machine learning experiments. ML experiment configuration is often highly nested, and can get out of hand quickly. Rather than switching workflows around too much to accommodate a new framework, the goal was to make already-written scripts easily adaptable, to achieve a few things:
- Configure scripts using
.ymlfiles. Be able to save.ymlfiles that can be used to rerun scripts the exact same way twice. - Spend time writing actual functions needed to run experiments, not argument parsers.
- Be able to run experiment code from other Python scripts, notebooks, or the command line.
- Be able to specify arguments from the command line directly to various functions.
- Be able to use scoping patterns, so a function can run inside a
trainscope andtestscope, with different results (e.g., for getting a train dataset and a test dataset).
Nothing out there really fit the bill, so Prem wrote ArgBind. If you have
an argparse based script, converting it to ArgBind should be very quick! ArgBind is simple,
small, and easy to use. To get a feel for how it works, check out usage, design, and examples!
Installation
Install via pip:
python -m pip install argbind-dbraun
Or from source:
git clone https://github.com/DBraun/argbind.git
cd argbind
python -m pip install -e .
This project uses uv. To create a dev environment with the test dependencies and run the suite:
uv sync --extra tests
uv run pytest
Install the pre-commit hooks (ruff format + lint) so they run on every commit:
uvx pre-commit install
Examples
- Example 1: Hello World
- Example 2: Scope patterns
- Example 3: Typing
- Example 4: Modern type annotations (PEP 585/604)
- Example 5: Literal arguments
- Example 6: Flexible boolean syntax
- Example 7: Using default_factory with dataclasses
- Example 8: Loading, saving, and using .yml files
- Example 9: Nested .yml files with
$include - Example 10: Multi-stage programs
- Example 11: Mimic more traditional CLI, without
func.argnotation - Example 12: Debug mode
- Example 13: Migrating from ArgParse
- Example 14: Binding existing functions and classes
- Example 15: Binding functions to specific groups
Usage
There are six main functions.
bind: Binds keyword arguments (and positional arguments ifpositional=True) of a function or class to ArgBind.parse_args: Actually parses command line arguments into a dictionary.scope: Context manager that scopes a dictionary containing function arguments to be used by the functions.dump_args: Dumps the args dictionary to a.ymlfile. Used internally when program is called with--args.save path/to/save.yml.load_args: Loads args from a.ymlfile. Used internally when program is called with--args.load path/to/load.yml.get_used_args: Gets arguments that have actually been used by call functions up to this point.
Your code with ArgBind generally follows this pattern:
- Write a function with a good docstring, and typed arguments. If arguments are not typed, their type will be inferred from the type of the default.
- Bind it via
bind. - When program is called, parse the arguments via
parse_args. - Scope the arguments, and call the bound function within the context block.
- Optionally call program with
--args.saveto save the current execution configuration to a.ymlfile or--args.loadto load arguments from a prior saved execution configuration to run it the same way twice. - Optionally, run your script with
--args.debug=1to see exactly how every bound function is called.
In your program, you can call get_used_args to see which arguments were actually used. Here's a minimal example:
import argbind
@argbind.bind()
def hello(
name : str = 'world'
):
"""Say hello to someone.
Parameters
----------
name : str, optional
Who you're saying hello to, by default 'world'
"""
print("Hello " + name)
if __name__ == "__main__":
# Arguments for CLI automatically generated from bound functions under the pattern
# function_name.function_arg.
args = argbind.parse_args()
# When called within a scope, the keyword arguments map to those from CLI or
# from defaults.
with argbind.scope(args):
hello()
# get_used_args() returns the arguments that were actually used by the bound
# functions that ran -- here, {'hello.name': 'world'}.
print(argbind.get_used_args())
Help text is automatically generated from the docstring:
❯ python examples/hello_world/with_argbind.py -h
usage: with_argbind.py [-h] [--args.save ARGS.SAVE] [--args.load ARGS.LOAD] [--args.debug ARGS.DEBUG] [--hello.name HELLO.NAME]
optional arguments:
-h, --help show this help message and exit
--args.save ARGS.SAVE
Path to save all arguments used to run script to.
--args.load ARGS.LOAD
Path to load arguments from, stored as a .yml file.
--args.debug ARGS.DEBUG
Print arguments as they are passed to each function.
Generated arguments for function hello:
Say hello to someone.
--hello.name HELLO.NAME
Who you're saying hello to, by default 'world'
Execution of this could look like:
# Default arguments
❯ python examples/hello_world/with_argbind.py
Hello world
# Binding name from the command line and saving the args.
❯ python examples/hello_world/with_argbind.py --hello.name=you --args.save=/tmp/args.yml
Hello you
# Loading saved arguments.
❯ python examples/hello_world/with_argbind.py --args.load=/tmp/args.yml
Hello you
# Loading saved arguments, and overriding via command line.
❯ python examples/hello_world/with_argbind.py --args.load=/tmp/args.yml --hello.name=me
Hello me
# See how each function is called with args.debug=1.
❯ python examples/hello_world/with_argbind.py --args.load=/tmp/args.yml --args.debug=1
hello(
name : str = you
)
Hello you
You can also run the hello function from another Python script or a Jupyter notebook:
import argbind
# Import the bound function
from .hello_world import hello
# Load the args
args = argbind.load_args('/tmp/args.yml')
# Scope the args
with argbind.scope(args):
# Run the bound function
hello() # Prints 'Hello you'.
hello() # Prints 'Hello world', as it's outside scope.
# Can edit the args before scoping again.
args['hello.name'] = 'me'
with argbind.scope(args):
hello() # Prints 'Hello me'.
You'll notice that ArgBind forces you to document and type your function arguments, which is always a good idea! Please check out the examples for more details!
Design
ArgBind is designed around a decorator that can be used on
functions the user wants to expose to command line or to a .yml file.
The arguments to that function are
then bound to a dictionary. When the function is called,
each argument is looked up in the dictionary and its
value is replaced with the corresponding value in the dictionary. The
dictionary that the function looks for values in is controlled by
scope:
import argbind
@argbind.bind()
def func(arg : str = 'default'):
print(arg)
dict1 = {
'func.arg': 1,
}
dict2 = {
'func.arg': 2
}
with argbind.scope(dict1):
func() # prints 1
with argbind.scope(dict2):
func() # prints 2
func(arg=3) # prints 3.
The function arguments are bound to the command line. Continuing the simple program from above:
if __name__ == "__main__":
args = argbind.parse_args()
with argbind.scope(args):
func()
with argbind.scope(args):
func(arg=3)
You can call this function like so:
❯ python examples/readme_example.py --func.arg 5
1 # Looks up `arg` in dict1
2 # Looks up `arg` in dict2
3 # arg is passed in on python call `func(arg=3)`
5 # Looks up `arg` from command line call `--func.arg 5`
3 # arg is passed in from two places: `func(arg=3)` and `--func.arg 5`. Former overrides the latter.
The logic here is that arguments that are bound that are closer to the actual function call get priority. From highest priority, to lowest, it goes:
- Bound explicitly in Python code
- Bound via command line
- Bound via .yml file
- Bound via default for kwarg
You can also use bind directly on classes - see here.
Limitations and known issues
There are some limitations to ArgBind, some due to how Python function decorators work, and others out of a desire to keep ArgBind's code simple and straightforward.
Bound function names should be unique
Functions that are bound must be unique, even if they are in different files. The function name is resolved in the argument parser only using the immediate name, not a path to the function etc.
Supported docstring formats
ArgBind uses docstring-parser, and so the only supported styles are: ReST, Google, and Numpydoc-style docstrings.
Not all types are supported
ArgBind supports most types that might pop up in your script, but not all. The supported types can be seen in the typing and modern annotations examples.
Positional arguments should not be saved into .yml files
If a positional argument is saved into a .yml file and loaded via --args.load,
then any positional argument passed in the command line will be overridden. Take
care not to pass positional arguments via .yml files.
Issues? Questions?
If you've run into some issues with ArgBind, or have some questions, please ask via GitHub Issues. Projects like ArgBind are pretty tricky to get right, so there may be some edge cases that have been missed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file argbind_dbraun-0.5.2.tar.gz.
File metadata
- Download URL: argbind_dbraun-0.5.2.tar.gz
- Upload date:
- Size: 30.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ac5c8db6ae7f72237a93368793cd3e838a2748009364938cb3265c28df276af
|
|
| MD5 |
db51815bb54c1d394fcdbf4fd3ce7a60
|
|
| BLAKE2b-256 |
b2bad4becea0be8a9215fb57383ba4016e6c77545b7f278193eae109a580a0c2
|
Provenance
The following attestation bundles were made for argbind_dbraun-0.5.2.tar.gz:
Publisher:
publish.yml on DBraun/argbind
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
argbind_dbraun-0.5.2.tar.gz -
Subject digest:
9ac5c8db6ae7f72237a93368793cd3e838a2748009364938cb3265c28df276af - Sigstore transparency entry: 2024946596
- Sigstore integration time:
-
Permalink:
DBraun/argbind@204d88098fd2fde4a7b7b6affa113665b2e953ce -
Branch / Tag:
refs/tags/v0.5.2 - Owner: https://github.com/DBraun
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@204d88098fd2fde4a7b7b6affa113665b2e953ce -
Trigger Event:
push
-
Statement type:
File details
Details for the file argbind_dbraun-0.5.2-py3-none-any.whl.
File metadata
- Download URL: argbind_dbraun-0.5.2-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33113204aed51ab22b8c7db9c6bd9c132a4ca18f21e3e30be4d1603b3ac68f34
|
|
| MD5 |
7f74ee106f8d4098803a99056254b63c
|
|
| BLAKE2b-256 |
e10faab01c1b72da32026342f7f2455d3abc6d9d8e6f23ee04580ffc708cf31d
|
Provenance
The following attestation bundles were made for argbind_dbraun-0.5.2-py3-none-any.whl:
Publisher:
publish.yml on DBraun/argbind
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
argbind_dbraun-0.5.2-py3-none-any.whl -
Subject digest:
33113204aed51ab22b8c7db9c6bd9c132a4ca18f21e3e30be4d1603b3ac68f34 - Sigstore transparency entry: 2024946675
- Sigstore integration time:
-
Permalink:
DBraun/argbind@204d88098fd2fde4a7b7b6affa113665b2e953ce -
Branch / Tag:
refs/tags/v0.5.2 - Owner: https://github.com/DBraun
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@204d88098fd2fde4a7b7b6affa113665b2e953ce -
Trigger Event:
push
-
Statement type: