Git Product home page Git Product logo

plac's Introduction

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 offers three decorators to mark positional, option and flag type parameters:

import plac

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

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

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 are the following decorators:

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

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

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

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

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 at the a line like

Executed XX tests OK

Code

Author: Michele Simionato, [email protected]

Maintainer: Istvan Albert, [email protected]

Issues

License

BSD License

plac's People

Contributors

ajschumacher avatar awhetter avatar fabianoengler avatar ialbert avatar jspeed-meyers avatar mariusvniekerk avatar micheles avatar pabs3 avatar poleguy avatar radek-sprta avatar refi64 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.