Simple, Elegant, Typed Argument Parsing
simple-parsing allows you to transform your ugly argparse scripts into beautifully structured, strongly typed little works of art.
Using dataclasses, simple-parsing makes it easier to share and reuse command-line arguments - no more copy pasting!
Supports inheritance, nesting, easy serialization to json/yaml, automatic help strings from comments, and much more!
# examples/demo.py
from dataclasses import dataclass
from simple_parsing import ArgumentParser
parser = ArgumentParser()
parser.add_argument("--foo", type=int, default=123, help="foo help")
@dataclass
class Options:
""" Help string for this group of command-line arguments """
log_dir: str # Help string for a required str argument
learning_rate: float = 1e-4 # Help string for a float argument
parser.add_arguments(Options, dest="options")
args = parser.parse_args()
print("foo:", args.foo)
print("options:", args.options)
$ python examples/demo.py --log_dir logs --foo 123
foo: 123
options: Options(log_dir='logs', learning_rate=0.0001)
$ python examples/demo.py --help
usage: demo.py [-h] [--foo int] --log_dir str [--learning_rate float]
optional arguments:
-h, --help show this help message and exit
--foo int foo help (default: 123)
Options ['options']:
Help string for this group of command-line arguments
--log_dir str Help string for a required str argument (default:
None)
--learning_rate float
Help string for a float argument (default: 0.0001)
installation
pip install simple-parsing
Examples
API Documentation (Under construction)
Features
-
Automatic "--help" strings
As developers, we want to make it easy for people coming into our projects to understand how to run them. However, a user-friendly
--helpmessage is often hard to write and to maintain, especially as the number of arguments increases.With
simple-parsing, your arguments and their decriptions are defined in the same place, making your code easier to read, write, and maintain. -
Modular, Reusable, Cleanly Grouped Arguments
(no more copy-pasting)
When you need to add a new group of command-line arguments similar to an existing one, instead of copy-pasting a block of
argparsecode and renaming variables, you can reuse your argument class, and let theArgumentParsertake care of adding relevant prefixes to the arguments for you:parser.add_arguments(Options, dest="train") parser.add_arguments(Options, dest="valid") args = parser.parse_args() train_options: Options = args.train valid_options: Options = args.valid print(train_options) print(valid_options)
$ python examples/demo.py \ --train.log_dir "training" \ --valid.log_dir "validation" Options(log_dir='training', learning_rate=0.0001) Options(log_dir='validation', learning_rate=0.0001)
These prefixes can also be set explicitly, or not be used at all. For more info, take a look at the Prefixing Guide
-
Easy serialization:
Easily save/load configs to
jsonoryaml!. -
Inheritance!
You can easily customize an existing argument class by extending it and adding your own attributes, which helps promote code reuse accross projects. For more info, take a look at the inheritance example
-
Nesting!:
Dataclasses can be nested within dataclasses, as deep as you need!
-
Easier parsing of lists and tuples :
This is sometimes tricky to do with regular
argparse, butsimple-parsingmakes it a lot easier by using the python's builtin type annotations to automatically convert the values to the right type for you. As an added feature, by using these type annotations,simple-parsingallows you to parse nested lists or tuples, as can be seen in this example -
Enums support
-
(More to come!)
Examples:
Additional examples for all the features mentioned above can be found in the examples folder
Release files for simple-parsing 0.0.11.post10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| simple_parsing-0.0.11.post10.tar.gz | 51.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| simple_parsing-0.0.11.post10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 100.9 kB
Release files / simple_parsing-0.0.11.post10.tar.gz
| Download URL | simple_parsing-0.0.11.post10.tar.gz |
|---|---|
| Size | 51.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e9ad8e09d78a932fdc30f46f49b775da3be84c53a38e9403d7142d8828145689
|
|
BLAKE2b-256 checksum How to use checksums |
1b5c97c7bb58c07ba155d2af6cf8b28c06e22c7271be762dea712679c746ae60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.7
|
Release files / simple_parsing-0.0.11.post10-py3-none-any.whl
| Download URL | simple_parsing-0.0.11.post10-py3-none-any.whl |
|---|---|
| Size | 49.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6693e9f67dc0cb3cbf7aed01060c821efdb11445eb76f0ad8b235965672ba9e0
|
|
BLAKE2b-256 checksum How to use checksums |
31ab47e091e656d32766c663006200077e857b634f0d87e90e9ee4447b8e4cba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.7
|