Skip to main content

Lightweight argument parsing using a decorator

Project description

Build status PyPI Version Anaconda Cloud Version

Parsable is a lightweight decorator-based command line parser library. Parsable was written to be simpler than argparse, optparse, and argh.

Installation

Install from PyPI with pip

pip install parsable

Or just download parsable.py and add to your project.

Usage

Parsable uses just two pieces of syntax: a @parsable command decorator, and a parsable() dispatch function.

  1. Import parsable.

    from parsable import parsable
  2. Decorate functions you want parsed. Parsable inspects the function to decide how to parse arguments. Arguments without default values are parsed as strings. Default arguments of any type T can be parsed as long as T(some_string) can do the parsing.

    @parsable
    def my_function(required_arg, optional_bool=True, optional_int=1):
        '''Help messages are not just a good idea, they are required'''
        # parsable automatically converts types based on default arguments:
        assert isinstance(required_arg, str)
        assert isinstance(optional_string, bool)
        assert isinstance(optional_int, int)
        # ...
    
    @parsable
    def do_stuff_with_files(*filenames, inplace=True):
        '''This does something to each file'''
        # ...
  3. Dispatch at the end of the script.

    if __name__ == '__main__':
        parsable()
  4. Use your new script

    $ python my_script.py my_function demo optional_int=5
    ...
    
    # parsable replaces - with _ to make functions easier to read
    $ python my_script.py do-stuff-with-files *.py inplace=false
    ...

Advanced Usage

To show verbose information (commmand name and timing info), set the environment variable PARSABLE_VERBOSE=true.

If you use parsable for many modules in a package, you can collect them in your setup.py using parsable.find_entry_points().

from parsable import parsable
from setuptools import setup

setup(
    name='my_package',
    entry_points=parsable.find_entry_points('my_package'),
    ...
)

LICENSE

Parsable is dual-licensed under the MIT and GPL2 licenses.

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

parsable-0.3.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

parsable-0.3.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file parsable-0.3.2.tar.gz.

File metadata

  • Download URL: parsable-0.3.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for parsable-0.3.2.tar.gz
Algorithm Hash digest
SHA256 2a8c9cc1319090371527207003284df1e939f592f5a99dca2d3f907ab5b37095
MD5 434c2537178255180181d4ebdb88ae04
BLAKE2b-256 a6adfb6adb44441b9d5348fe10d6ab0b4838f53a0941690e80b981ed8f760472

See more details on using hashes here.

File details

Details for the file parsable-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: parsable-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for parsable-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7345c4bb9097a88f649ec0eddfda359f562d32557ad2125a300f19ac2a149cc0
MD5 f489c20790439b3c874992c65af55d27
BLAKE2b-256 c13c1e733c27f4536c79dd6c9fb41ec7e8f8bd511427032c27650871f84541a2

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