Skip to main content

Build Status

Simple, Elegant Argument Parsing

Do you ever find youself stuck with an endless list of command-line arguments, say, when specifying all the hyper-parameters of your ML model? Well, say no more, here's something to make your life just a little bit easier!

simple-parsing uses python 3.7's amazing dataclasses to allow you to define your arguments in a simple, elegant, and object-oriented way. When applied to a dataclass, the ParseableFromCommandLine base-class enables creating instances of that class automatically from the provided command-line arguments.

Documentation: SimpleParse Wiki

installation

python version >= 3.7:

pip install simple-parsing

python version == 3.6.X:

pip install dataclasses simple-parsing

Basic Usage:

Instead of adding your command-line arguments with parser.add_argument(...), you can instead define your arguments directly in code! Simply create a dataclass to hold your arguments, adding ParseableFromCommandLine as a base class:

"""A basic example of how to use simple-parsing."""
import argparse
from dataclasses import dataclass, field
from typing import List

from simple_parsing import Formatter, ParseableFromCommandLine

parser = argparse.ArgumentParser(formatter_class=Formatter)

@dataclass()
class Options(ParseableFromCommandLine):
	""" A class which groups related parameters. """

	some_int: int			# Some required int parameter
	some_float: float = 1.23	# An optional float parameter

	name: str = "default"   	# The name of some important experiment

	log_dir: str = "/logs" 		# an optional string parameter
	flag: bool = False 		# Wether or not we do something

	# This is a list of integers (empty by default)
	some_integers: List[int] = field(default_factory=list)

	# Converting the list items to the right type will be taken care of for you!
	some_floats: List[float] = field(default_factory=list)

# add the arguments
Options.add_arguments(parser)

# parse the arguments from stdin
args = parser.parse_args()

# create an instance of Options with the arguments
options = Options.from_args(args)

# Do whatever you want using the Options object here!
print(options)

Executing the script:

This script is called just like any other argparse script, and the values are stored inside the object:

$ python ./examples/basic_example.py --some_int 123 --flag true --some_integers 23 45 67
Options(some_int=123, some_float=1.23, name='default', log_dir='/logs', flag=True, some_integers=[23, 45, 67], some_floats=[])

However, we get a lot of nice information for free! For instance, passing the "--help" option displays relevant information for each argument:

$ python ./basic_example.py --help
usage: basic_example.py [-h] --some_int int [--some_float float] [--name str]
                        [--log_dir str] [--flag [str2bool]]
                        [--some_integers [int [int ...]]]
                        [--some_floats [float [float ...]]]

optional arguments:
  -h, --help            show this help message and exit

Options:
  A class which groups related parameters.

  --some_int int        Some required int parameter (default: None)
  --some_float float    An optional float parameter (default: 1.23)
  --name str            The name of some important experiment (default:
                        default)
  --log_dir str         an optional string parameter (default: /logs)
  --flag [str2bool]     Wether or not we do something (default: False)
  --some_integers [int [int ...]]
                        This is a list of integers (empty by default)
                        (default: [])
  --some_floats [float [float ...]]
                        Converting the list items to the right type will be
                        taken care of for you! (default: [])

Easily convert to/from a dictionary:

# convert the dataclass to a dict
options_dict = options.asdict()

# create an instance from a dict
options_ = Options(**options_dict)
assert options == options_

You can then use whichever library you like (yaml, json, etc.) and save the dict to a file:

import json
with open(options.name + ".json", "w") as f:
	json.dump(options_dict, f, indent=1)
$ cat default.json
{
 "some_int": 123,
 "some_float": 1.23,
 "name": "default",
 "log_dir": "/logs",
 "flag": true,
 "some_integers": [
  23,
  45,
  67
 ],
 "some_floats": []
}

Loading from a dictionary or JSON file:

with open("default.json") as f:
	params = json.load(f)
	default_options = Options(**params)
	assert options == default_options
	print(default_options)

Metadata

Release files for simple-parsing 0.0.2

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

Source distribution (sdist)

Source distribution for simple-parsing 0.0.2
File Size Uploaded
simple_parsing-0.0.2.tar.gz 15.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for simple-parsing 0.0.2
File Interpreter ABI Platform
simple_parsing-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 27.0 kB

Release files / simple_parsing-0.0.2.tar.gz

Download URL simple_parsing-0.0.2.tar.gz
Size 15.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f379ae20a5946ff19918e21adc7c89880f1b1d6e9f4e55167e315bd8a9cce1e9
BLAKE2b-256 checksum
How to use checksums
d7fe08883719795703f96379b94a96ad9b8ded5552d7199bfe768cc767a3354d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

Release files / simple_parsing-0.0.2-py3-none-any.whl

Download URL simple_parsing-0.0.2-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f2ff83f6a02dd2df1348ac24a52d8d6925efeec1497d3b74fc79425f081d7809
BLAKE2b-256 checksum
How to use checksums
806c6036c93fc3175a2dde21db5af0b0377475514d99072dbece173c750a6b86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7

Release history Release notifications | RSS feed

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.21

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

This release

0.0.2 This release

2 release files

0.0.1

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