Skip to main content

Snakemake Argparse Bridge

A Python package that allows existing argparse-based scripts to work seamlessly in both command-line and Snakemake environments with minimal code changes.

Why does this exist?

You just wrote a Python script for your Snakemake rule that uses argparse so you can test and run it on the command line. But then when you integrate it into your workflow, you realize you need to:

  1. Rewrite the script to work with Snakemake's script: directive
  2. Maintain two versions - one for CLI testing, one for Snakemake
  3. Manually map Snakemake variables (wildcards, input, output) to script arguments

This bridge eliminates that friction. Just add one decorator and your script works seamlessly in both environments.

Installation

pip install snakemake-argparse-bridge

Quick Start

1. Decorate your existing script

# scripts/process_sample.py
import argparse
from snakemake_argparse_bridge import snakemake_compatible

@snakemake_compatible(mapping={
    'sample': 'wildcards.sample',
    'input_file': 'input[0]',
    'output_file': 'output[0]',
    'threads': 'threads'
})
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--sample', required=True)
    parser.add_argument('--input-file', required=True)
    parser.add_argument('--output-file', required=True)
    parser.add_argument('--threads', type=int, default=1)
    
    # This works in both CLI and Snakemake contexts!
    args = parser.parse_args()
    
    # Your processing logic here
    print(f"Processing {args.sample} with {args.threads} threads")

if __name__ == '__main__':
    main()

2. Use in Snakemake

# Snakefile
rule process_sample:
    input: "data/{sample}.txt"
    output: "results/{sample}_processed.txt"
    threads: 4
    script: "scripts/process_sample.py"

3. Still works from command line

python scripts/process_sample.py --sample test --input-file data.txt --output-file results.txt --threads 2

How it works

The @snakemake_compatible decorator temporarily patches argparse.ArgumentParser.parse_args() to:

  • In Snakemake context: Extract values from snakemake.wildcards, snakemake.input, snakemake.params, etc.
  • In CLI context: Use normal argparse behavior

Mapping Options

Explicit Mapping

@snakemake_compatible(mapping={
    'sample': 'wildcards.sample',           # Wildcard
    'input_file': 'input[0]',               # First input file
    'output_file': 'output[0]',             # First output file  
    'method': 'params.method',              # Parameter
    'threads': 'threads',                   # Thread count
    'memory': 'resources.mem_mb',           # Memory resource
    'log_file': 'log[0]',                   # Log file
})

Available Snakemake Attributes

  • input, input[0], input[1], etc. - Input files
  • output, output[0], output[1], etc. - Output files
  • wildcards.{name} - Wildcard values
  • params.{name} - Rule parameters
  • log, log[0], etc. - Log files
  • threads - Number of threads
  • resources.{name} - Resource specifications
  • config.{name} - Configuration values

Testing

# Run tests
pytest tests/

# Test with actual Snakemake (requires snakemake installation)
pytest tests/test_snakemake_integration.py

Requirements

  • Python 3.7+
  • No additional dependencies for basic usage
  • Snakemake (for Snakemake integration, obviously)

License

MIT License

Metadata

Release files for snakemake-argparse-bridge 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for snakemake-argparse-bridge 0.2.0
File Size Uploaded Provenance Actions
snakemake_argparse_bridge-0.2.0.tar.gz 10.9 kB With provenance Details for snakemake_argparse_bridge-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for snakemake-argparse-bridge 0.2.0
File Interpreter ABI Platform Actions
snakemake_argparse_bridge-0.2.0-py3-none-any.whl Python 3 none any Details for snakemake_argparse_bridge-0.2.0-py3-none-any.whl

Total release size: 15.4 kB

Release files / snakemake_argparse_bridge-0.2.0.tar.gz

Download URL snakemake_argparse_bridge-0.2.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f64984815a4ff01ddc35337ebe40c33c53ff0a829eb3988df7ea71356295e24f
BLAKE2b-256 checksum
How to use checksums
b79b9a03434eb6d17e0430d8cffd7ecc8dbd59b7ce620f2257c7a0b8601783bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 25, 2026.

Transparency log

Release files / snakemake_argparse_bridge-0.2.0-py3-none-any.whl

Download URL snakemake_argparse_bridge-0.2.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2119d6f7c12dfc2d91ae898bf0a46094ae695c317360e59f363f831c8fe2274e
BLAKE2b-256 checksum
How to use checksums
3d46c84dc9d2bb9a47575b17c52a730fb28a7c7de7d3d0817d5d341e0d7f73f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 25, 2026.

Transparency log

Security for snakemake-argparse-bridge 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for snakemake_argparse_bridge-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page