Skip to content

Latest commit

 

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

argdump

Serialize and deserialize Python argparse parsers.

Installation

pip install argdump
# or
uv add argdump

Usage

import argparse
import argdump

# Create a parser
parser = argparse.ArgumentParser(prog="mytool")
parser.add_argument("input")
parser.add_argument("-v", "--verbose", action="count", default=0)
parser.add_argument("--format", choices=["json", "csv"])

# Serialize
data = argdump.dump(parser)       # dict
json_str = argdump.dumps(parser)  # JSON string

# Deserialize
restored = argdump.load(data)
restored = argdump.loads(json_str)

# Use normally
args = restored.parse_args(["input.txt", "-vvv", "--format", "json"])

JSON Schema

A JSON Schema for validating serialized output is available at docs/schema-v1.json.

Features

  • All standard actions (store, append, count, etc.)
  • Subparsers with aliases and per-command help text
  • Mutual exclusion and argument groups
  • Type converters (builtins, FileType, importable functions)
  • Choices, defaults, metavar, help text
  • Environment metadata ($env) for reproducibility

Options

# Exclude environment metadata
argdump.dump(parser, include_env=False)

# Non-strict mode: skip unresolvable types instead of raising
argdump.load(data, strict=False)

Limitations

Lambdas and closures cannot be serialized. Use strict=False to skip them:

parser.add_argument("--value", type=lambda x: int(x) * 2)

argdump.load(argdump.dump(parser), strict=False)  # type becomes None
argdump.load(argdump.dump(parser), strict=True)   # raises UnresolvableTypeError

A mutually exclusive group nested inside a named argument group is reconstructed as a top-level mutex group: exclusivity is still enforced, but in --help its members appear under the default options heading rather than the named group.

License

MIT

About

Serialize and deserialize argparse parsers

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages