Skip to main content

Hierarchical Argparse

PyPI PyPI - Python Version PyPI - License

Hiargparse is a hierarchical and highly sophisticated variant of argparse.

Minimal Code

from hiargparse import ArgsProvider, Arg, ChildProvider, ArgumentParser

child = ArgsProvider(
    args=[Arg('baz', default=42)]
)
root = ArgsProvider(
    args=[Arg('foo', default='bar')],
    child_providers=[ChildProvider(provider=child, name='child')]
)
parser = ArgumentParser()
parser.add_arguments_from_provider(root)
print(parser.parse_args())

Description

Hiargparse automatically generates the command-line arguments with your all classes in your tree-like module structures with minimal codings.

Suppose you make a large module, and your hierarchically deep class, say foo.bar.baz.ham.spam.Egg, requires some arguments, say heights and widths. Foo makes an instance of Bar, Bar makes an instance of Baz, ..., and Spam makes an Egg which has the two arguments. When you want to pass them command-line options, you have to write the two, heights and widths, in the constructor of Foo, Bar, ..., and Spam. What if the Egg is updated and wants to require some new arguments, like depth or length? Hiargparse passes the arguments directly to the classes without any black magics.

Features

With this module, you can

  • easily make hierarchical (tree-like) command-line arguments with argparse
  • Each argument is automatically help-texted and grouped into argparse.ArgumentGroup.
  • get a more useful Namespace object than the original
  • Accessing with dict-like key, getting the child Namespace, converting to/from dictionaries, and so on
  • write/read the arguments to/from some configure files with well known formats
  • Currently we supports yaml and toml.

Also, this module

  • is almost compatible with original argparse; you can gradually introduce it to your large projects.
  • works without command-line arguments; when you distribute your module with hiargparse, users still can select whether to feed arguments to it with a command-line or programatic way.

This module is inspired by rlpytorch.args_provider in FacebookAIResearch/ELF.

Installation

pip install hiargparse

Requirements

  • python >= 3.6.0
  • typing_extensions (for typing_extentions.Protocol in python 3.6) >= 3.6.2.1
  • pyyaml >= 3.12 (only if you use yaml write/read)
  • toml >= 0.9.4 (only if you use toml write/read)

Examples

Contribution

  • Any contribution is welcome!
  • Fork the repo, create a branch, add your awesome changes, and make a new Pull Request.
  • If you find some bugs, please report in issue.

Author

  • Keigo Kawamura (Department of Electrical Engineering and Information Systems (EEIS), Graduate School of Engineering, The University of Tokyo)
  • kkawamura@logos.t.u-tokyo.ac.jp

Release files for hiargparse 0.2.2

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

Built distribution (wheel)

Table of built distributions (wheels) for hiargparse 0.2.2
File Interpreter ABI Platform
hiargparse-0.2.2-py3-none-any.whl Python 3 none any Details

Release files / hiargparse-0.2.2-py3-none-any.whl

Download URL hiargparse-0.2.2-py3-none-any.whl
Size 24.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c4de17d2560a0db63f18d7775f6f13a9fd78badfffbb6078f36b9f0444cd926
BLAKE2b-256 checksum
How to use checksums
7e11d3b6bd878d2259de0373630b27f870c22d337b6fde93040bf66943b7697c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.2 This release

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.0.0

2 release files

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