A small utility for simplifying and cleaning up argument parsing scripts.
Project description
Simple, Elegant Argument Parsing
simple-parsing
extends the capabilities of the builtin argparse
module by allowing you to group related command-line arguments into neat, reusable dataclasses and let the ArgumentParser take care of creating the arguments for you.
# examples/demo.py
import argparse
from dataclasses import dataclass
import simple_parsing
assert issubclass(simple_parsing.ArgumentParser, argparse.ArgumentParser)
parser = simple_parsing.ArgumentParser()
@dataclass
class Options:
""" Group of command-line arguments for this script """
log_dir: str # a required string parameter
learning_rate: float = 1e-4 # An optional float parameter
# you can add arguments as you usual would
parser.add_argument("--foo", dest="foo", type=int, default=123)
# or let the parser create the arguments for you
parser.add_arguments(Options, dest="options")
args = parser.parse_args()
foo: int = args.foo # get the parsed 'foo' value
options: Options = args.options # get the parsed Options instance
print("foo:", foo)
print("options:", options)
what you get for free:
$ python examples/demo.py --log_dir "logs"
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
Options ['options']:
Group of command-line arguments for this script
--log_dir str a required string parameter (default: None)
--learning_rate float
An optional float parameter (default: 0.0001)
installation
python version | command |
---|---|
>= 3.7 | pip install simple-parsing |
== 3.6.X | pip install dataclasses simple-parsing |
Project GitHub Repository
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
--help
message 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 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
argparse
code and renaming variables, you can reuse your argument class, and let theArgumentParser
take care of addingrelevant
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.experiment_name "training" \ --valid.experiment_name "validation" Options(experiment_name='training', learning_rate=0.0001) Options(experiment_name='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
-
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!
-
Easy serialization: Since dataclasses are just regular classes, its easy to add methods for easy serialization/deserialization to popular formats like
json
oryaml
. -
Easier parsing of lists and tuples: This is sometimes tricky to do with regular
argparse
, butsimple-parsing
makes it a lot easier by using the standard type annotations of thetyping
module to automatically convert the parsed values to the right type for you.As an added feature, by using these type annotations,
simple-parsing
allows you to parse nested lists or tuples, as can be seen in this example -
Enums support
-
(More to come!)
Examples:
Additional examples for each of the above-mentioned features can be found in the the project repository.
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
Hashes for simple_parsing-0.0.3.post3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dc47cc056334a55344486ebc7a93d18fd265b2a7646eaff87788b0a89f547be |
|
MD5 | 474a737f1c35a0a755c3f7b5aa7667a9 |
|
BLAKE2b-256 | ecc70fbcb7416cfb2c7764ed9f1cc88239fa76c4f63643fb6bd9f2e59b9c3b20 |
Hashes for simple_parsing-0.0.3.post3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e330aae08b661ca20db6b2c44f61fedcf44ac50b600c823d3a0fab453eff787e |
|
MD5 | c292857ef2c9e57866608ff80d0b3cd0 |
|
BLAKE2b-256 | ee7b723f5fd139d3d9726c747f717f66e764be97cb11ef1742fa26f38a3f6b58 |