Skip to main content

Plac: parsing the command line the easy way

plac is a Python package that can generate command line parameters from function signatures.

plac works on Python 2.6 through all versions of Python 3.

plac has no dependencies beyond modules already present in the Python standard library.

plac implements most of its functionality in a single file that may be included in your source code.

Quickstart

plac can automatically generate command line parameters from a function signature.

import plac

def main(model, iter=100, debug=False):
    """
    A script for machine learning
    """
    print (model, iter, debug)

if __name__ == '__main__':
    # Execute function via plac.call()
    plac.call(main)

The program above can now take parameters from the command line like so:

python example.py A 1000 

Running the script with no parameters as python example.py would print:

usage: example.py [-h] model [iter] [debug]
example.py: error: the following arguments are required: model

In addition, the program can also generate a help message:

python example.py -h

Produces the following output:

usage: example.py [-h] model [iter] [debug]

A script for machine learning

positional arguments:
  model
  iter        [100]
  debug       [False]

options:
  -h, --help  show this help message and exit

Additional customization

When you need more control plac offers three decorators to describe positional, option and flag type parameters:

import plac

# Add decorators to the function
@plac.pos('model', help="model name", choices=['A', 'B', 'C'])
@plac.opt('iter', help="iterations", type=int)
@plac.flg('debug', help="debug mode")
def main(model, iter=100, debug=False):
    """
    A script for machine learning
    """
    print (model, iter, debug)

if __name__ == '__main__':
    # Execute function via plac.call().
    plac.call(main)

That will produce the following help:

usage: example.py [-h] [-i 100] [-d] {A,B,C}

A script for machine learning

positional arguments:
  {A,B,C}             model name

options:
  -h, --help          show this help message and exit
  -i 100, --iter 100  iterations
  -d, --debug         debug mode

Decorator reference

To use plac all you need to know are the following three decorators:

  • @plac.pos - for positional parameters model
  • @plac.opt - for key value options --iter 100
  • @plac.flg - for flags --debug

that have the following signatures:

# Positional parameters.
pos(arg, help=None, type=None, choices=None, metavar=None):

# Option parameters.
opt(arg, help=None, type=None, abbrev=None, choices=None, metavar=None):

# Flag parameters.
flg(arg, help=None, abbrev=None):

Zero dependencies ... not even plac :-)

Notably, the main functionality of plac is implemented in a single Python module called plac_core.py that, if necessary, may be included and distributed with your source code thus reducing external dependencies in your code.

Copy plac_core.py to your package then use it like so:

from mypackage import plac_core as plac

Avoiding name clashes

Python syntax, or your variable naming may impose constraints on what words may be used as parameters. To circumvent that limitation append a trailing underscore to the name. plac will strip that underscore from the command line parameter name:

import plac

@plac.flg('list_')   # avoid clash with builtin
@plac.flg('yield_')  # avoid clash with keyword
@plac.opt('sys_')    # avoid clash with a very common name
def main(list_, yield_=False, sys_=100):
    print(list_)
    print(yield_)
    print(sys_)

if __name__ == '__main__':
    plac.call(main)

produces the usage:

usage: example13.py [-h] [-l] [-y] [-s 100]

optional arguments:
  -h, --help         show this help message and exit
  -l, --list
  -y, --yield        [False]
  -s 100, --sys 100  [100]

Variable arguments

plac may accept multiple positional arguments and even additional key=value pairs:

import plac

@plac.pos('args', help="words")
@plac.opt('kwds', help="key=value", )
def main(*args, **kwds):
    print(args)
    print(kwds)

if __name__ == '__main__':
    plac.call(main)

the usage will be:

usage: example15.py [-h] [args ...] [kwds ...]

positional arguments:
  args        words
  kwds        key=value

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

when running it as:

python example15.py A B x=10 y=20

the program prints:

('A', 'B')
{'x': '10', 'y': '20'}

Installation

pip install plac

Testing

Run

python doc/test_plac.py

You will see several apparent errors, but this is right, since the tests are checking for several error conditions. The important thing is that you get a line like

Executed XX tests OK

Code

Author: Michele Simionato, michele.simionato@gmail.com

Maintainer: Istvan Albert, istvan.albert@gmail.com

Issues

License

BSD License

Release files for plac 1.4.7

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

Source distribution (sdist)

Source distribution for plac 1.4.7
File Size Uploaded
plac-1.4.7.tar.gz 39.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for plac 1.4.7
File Interpreter ABI Platform
plac-1.4.7-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 61.1 kB

Release files / plac-1.4.7.tar.gz

Download URL plac-1.4.7.tar.gz
Size 39.1 kB
Tags Source
SHA-256 checksum
How to use checksums
957ffbab8ea8ab8c0b9f2694daa2a95f3560151cd162bcbc91e97ff94d45c4f9
BLAKE2b-256 checksum
How to use checksums
d65f40553c01a1d1424118196e7c29defe40715fd5f56939b9c824aa480112cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / plac-1.4.7-py2.py3-none-any.whl

Download URL plac-1.4.7-py2.py3-none-any.whl
Size 22.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8af4bcd5d52e06c31fab641bffb7040d0eb0e579e2f70a4d2e87af5eed0612ea
BLAKE2b-256 checksum
How to use checksums
14f1405c082aeb4ec5ec0744d1e059d9da9530bb7aaa8fd5dafae97706a2cfcd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

1.4.7 This release

2 release files

1.4.6

2 release files

1.4.5

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.5

2 release files

1.3.4

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.9.6

2 release files

0.9.5

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

1 release file

0.9.1

1 release file

0.9.0

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.5

1 release file

0.7.4

1 release file

0.7.3

1 release file

0.7.2

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.0

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

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