Skip to main content

The smartest command line arguments parser in the world

Project description

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 automatically generates the command line parameters from the function signature.

It 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)

And that's it! The program can now take parameters from the command line like so:

python example.py -d -i 1000 B 

Running the script with python example.py -h will give you the following help message: :

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

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

usage: example.py [-h] [-i 100] [-d] {A,B,C}
example.py: error: the following arguments are required: model

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plac-1.4.1.tar.gz (37.5 kB view details)

Uploaded Source

Built Distribution

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

plac-1.4.1-py2.py3-none-any.whl (22.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file plac-1.4.1.tar.gz.

File metadata

  • Download URL: plac-1.4.1.tar.gz
  • Upload date:
  • Size: 37.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for plac-1.4.1.tar.gz
Algorithm Hash digest
SHA256 8276d038466f22210ca4f7fa0b270473bea2ec85fff495de179f56934372d5fa
MD5 9745dfdb2d0a08c6dbd25fec4d6d3e15
BLAKE2b-256 220285f691b18bd1dc48c3a5073876d5876243140c1fbce1174da0dff24409cd

See more details on using hashes here.

File details

Details for the file plac-1.4.1-py2.py3-none-any.whl.

File metadata

  • Download URL: plac-1.4.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for plac-1.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5b02d38cfb07c6b95d9803dfdd108a618fdb4691c1759b9156bfb34e566d28c4
MD5 3a196840ad297dce6c7bf3b15f7542aa
BLAKE2b-256 89f66029fa8e976a0861dd61bd661d3d54c082026b0ff746cf9b3056f3e3b9cb

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