Skip to main content

A small utility for simplifying and cleaning up argument parsing scripts.

Project description

Build Status PyPI version

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 --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, 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 argparse code and renaming variables, you can reuse your argument class, and let the ArgumentParser take 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 json or yaml!.

  • 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, but simple-parsing makes 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-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 all the features mentioned above can be found in the examples folder

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

simple_parsing-0.0.11.post17.tar.gz (57.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_parsing-0.0.11.post17-py3-none-any.whl (55.3 kB view details)

Uploaded Python 3

File details

Details for the file simple_parsing-0.0.11.post17.tar.gz.

File metadata

  • Download URL: simple_parsing-0.0.11.post17.tar.gz
  • Upload date:
  • Size: 57.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.7

File hashes

Hashes for simple_parsing-0.0.11.post17.tar.gz
Algorithm Hash digest
SHA256 beb32f9c7c9ec9fe68e408aaf7fac789f16fc762cd875a05d2a566dc839a7ec9
MD5 2f131cf2f85cb5a052e20d0700638e63
BLAKE2b-256 b8c0eea9f4b12b976e62f9dfc25176745d16d07071ce75db2976a24ef39e21ce

See more details on using hashes here.

File details

Details for the file simple_parsing-0.0.11.post17-py3-none-any.whl.

File metadata

  • Download URL: simple_parsing-0.0.11.post17-py3-none-any.whl
  • Upload date:
  • Size: 55.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.7

File hashes

Hashes for simple_parsing-0.0.11.post17-py3-none-any.whl
Algorithm Hash digest
SHA256 099e0be48bb73902b34d1b2b7dd198564b91fe28f01439c308b590d829e20fe8
MD5 c5489ee3715cda47e32ed94bd1e956ea
BLAKE2b-256 e1199b883ee6e595a13b8b83e00cace527ef0fb4ebce3683ab25699557a393cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page