simple-parsing 0.1.7


pip install simple-parsing

  Latest version

Released: Jan 20, 2025

Project Links

Meta
Author: Fabrice Normandin
Requires Python: >=3.8,<4.0

Classifiers

License
  • OSI Approved :: MIT License

Programming Language
  • Python :: 3
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13

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. This isn't a fancy, complicated new command-line tool either, this simply adds new features to plain-old argparse! 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)

(new) Simplified API:

For a simple use-case, where you only want to parse a single dataclass, you can use the simple_parsing.parse or simple_parsing.parse_known_args functions:

options: Options = simple_parsing.parse(Options)
# or:
options, leftover_args = simple_parsing.parse_known_args(Options)

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 descriptions 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

  • Argument subgroups

    It's easy to choose between different argument groups of arguments, with the subgroups function!

  • Setting defaults from Configuration files

    Default values for command-line arguments can easily be read from many different formats, including json/yaml!

  • 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 across 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

0.1.7 Jan 20, 2025
0.1.6 Sep 05, 2024
0.1.5 Jan 31, 2024
0.1.4 Aug 07, 2023
0.1.3 Jun 06, 2023
0.1.2.post1 Apr 27, 2023
0.1.2 Apr 19, 2023
0.1.1 Mar 14, 2023
0.1.0 Feb 20, 2023
0.0.21.post1 Dec 06, 2022
0.0.21 Oct 19, 2022
0.0.20 Jun 07, 2022
0.0.19.post1 Apr 20, 2022
0.0.19.post0 Mar 18, 2022
0.0.19 Mar 17, 2022
0.0.18 Jan 19, 2022
0.0.17 Nov 03, 2021
0.0.16.post2 Aug 09, 2021
0.0.16 Jul 29, 2021
0.0.15.post1 May 06, 2021
0.0.15 Apr 27, 2021
0.0.14.post1 Apr 05, 2021
0.0.14 Mar 30, 2021
0.0.13 Feb 20, 2021
0.0.12.post7 Feb 03, 2021
0.0.12.post6 Feb 01, 2021
0.0.12.post5 Jan 14, 2021
0.0.12.post4 Jan 07, 2021
0.0.12.post3 Dec 22, 2020
0.0.12.post2 Nov 20, 2020
0.0.12.post1 Nov 03, 2020
0.0.12 Oct 14, 2020
0.0.11.post18 Aug 04, 2020
0.0.11.post17 Aug 04, 2020
0.0.11.post16 Aug 04, 2020
0.0.11.post15 Aug 01, 2020
0.0.11.post14 Jul 31, 2020
0.0.11.post13 Jul 21, 2020
0.0.11.post12 Jul 03, 2020
0.0.11.post11 Jun 30, 2020
0.0.11.post10 Jun 29, 2020
0.0.11.post9 Jun 27, 2020
0.0.11.post8 Jun 25, 2020
0.0.11.post7 Jun 24, 2020
0.0.11.post6 Jun 22, 2020
0.0.11.post5 Jun 18, 2020
0.0.11.post4 Jun 18, 2020
0.0.11.post3 Jun 17, 2020
0.0.11.post2 Jun 17, 2020
0.0.11.post1 Jun 17, 2020
0.0.11 Jun 16, 2020
0.0.10.post3 May 30, 2020
0.0.10.post2 May 23, 2020
0.0.10.post1 May 22, 2020
0.0.10 May 20, 2020
0.0.9 Mar 30, 2020
0.0.8.post1 Mar 03, 2020
0.0.8 Mar 01, 2020
0.0.7 Feb 18, 2020
0.0.6 Jan 30, 2020
0.0.5 Jan 10, 2020
0.0.4 Jan 10, 2020
0.0.3.post6 Dec 25, 2019
0.0.3.post5 Dec 25, 2019
0.0.3.post4 Dec 25, 2019
0.0.3.post3 Dec 19, 2019
0.0.3.post2 Dec 19, 2019
0.0.3.post1 Dec 19, 2019
0.0.3 Dec 03, 2019
0.0.3rc1 Dec 03, 2019
0.0.2 Oct 23, 2019
0.0.1 Oct 07, 2019

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras:
Dependencies:
docstring-parser (<1.0,>=0.15)
typing-extensions (>=4.5.0)