Metadata-Version: 2.1
Name: cltoolbox
Version: 1.3.0
Summary: Python library for creating and manipulating CLIs
Author-email: Tim Cera <tim@cerazone.net>
License: Copyright for portions of project cltoolbox are held by Michele Lacchia as
        part of project mando. All other copyright for project cltoolbox are held by
        Tim Cera, 2022.
        
        mando project:
        The MIT License (MIT)
        
        Copyright (c) 2013 Michele Lacchia
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of
        this software and associated documentation files (the "Software"), to deal in
        the Software without restriction, including without limitation the rights to
        use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
        the Software, and to permit persons to whom the Software is furnished to do so,
        subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
        FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
        COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
        IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
        CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
        
        
        cltoolbox project:
        Copyright (c) 2022, Tim Cera
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
            * Redistributions of source code must retain the above copyright
              notice, this list of conditions and the following disclaimer.
            * Redistributions in binary form must reproduce the above copyright
              notice, this list of conditions and the following disclaimer in the
              documentation and/or other materials provided with the distribution.
            * Neither the name cltoolbox nor the
              names of its contributors may be used to endorse or promote products
              derived from this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
        DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
        (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
        LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
        ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
        SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Keywords: python,cli,argparse,arguments,command line,keyword arguments,docstring
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: End Users/Desktop
Classifier: Intended Audience :: Developers
Classifier: Environment :: Console
Classifier: License :: OSI Approved :: BSD License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.7.1
Description-Content-Type: text/x-rst
Provides-Extra: dev
License-File: LICENSE.txt
License-File: AUTHORS.rst

.. image:: https://github.com/timcera/cltoolbox/actions/workflows/python-package.yml/badge.svg
    :alt: Tests
    :target: https://github.com/timcera/cltoolbox/actions/workflows/python-package.yml
    :height: 20

.. image:: https://img.shields.io/coveralls/github/timcera/cltoolbox
    :alt: Test Coverage
    :target: https://coveralls.io/r/timcera/cltoolbox?branch=master
    :height: 20

.. image:: https://img.shields.io/pypi/v/cltoolbox.svg
    :alt: Latest release
    :target: https://pypi.python.org/pypi/cltoolbox
    :height: 20

.. image:: https://img.shields.io/badge/license-BSD--3--Clause-green
    :alt: BSD-3-Clause
    :target: https://pypi.python.org/pypi/cltoolbox/
    :height: 20

.. image:: http://img.shields.io/pypi/dd/cltoolbox.svg
    :alt: cltoolbox downloads
    :target: https://pypi.python.org/pypi/cltoolbox/
    :height: 20

.. image:: https://img.shields.io/pypi/pyversions/cltoolbox
    :alt: PyPI - Python Version
    :target: https://pypi.org/project/cltoolbox/
    :height: 20

cltoolbox: Easy Command Line Interfaces
=======================================
cltoolbox is a wrapper around ``argparse``, and allows you to write complete CLI
applications in seconds using a simple decorator.

Significant portions of this code are based on `mando`.  The primary
differences between `mando` and `cltoolbox` are:

    * `cltoolbox` supports automatic detection of Sphinx, Google, and Numpy
      docstring formats by using the `docstring_parser` library
    * `cltoolbox` supports python 3.7+

If you need support of the `mando` formatted docstring or python 2 you have to
use `mando` instead of `cltoolbox`.

Installation
------------

.. code-block:: console

    $ pip install cltoolbox

The problem
-----------

The ``argparse`` module that comes with Python requires a programmer to
duplicate information that Python can easily parse from the function signature
and docstring.  The ``cltoolbox`` does this for you by using decorators.

Quickstart
----------

.. code-block:: python

    from cltoolbox import command, main


    @command
    def echo(text, capitalize=False):
        """Echo the given text."""
        if capitalize:
            text = text.upper()
        print(text)


    if __name__ == "__main__":
        main()

Generated help:

.. code-block:: console

    $ python example.py -h
    usage: example.py [-h] {echo} ...

    positional arguments:
      {echo}
        echo      Echo the given text.

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

    $ python example.py echo -h
    usage: example.py echo [-h] [--capitalize] text

    Echo the given text.

    positional arguments:
      text

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

Actual usage:

.. code-block:: console

    $ python example.py echo spam
    spam
    $ python example.py echo --capitalize spam
    SPAM


A *real* example
----------------

Something more complex and real-world-*ish*. The code:

.. code-block:: python

    from cltoolbox import command, main


    @command
    def push(repository, all=False, dry_run=False, force=False, thin=False):
        """Update remote refs along with associated objects.

        :param repository: Repository to push to.
        :param --all: Push all refs.
        :param -n, --dry-run: Dry run.
        :param -f, --force: Force updates.
        :param --thin: Use thin pack."""

        print(
            "Pushing to {0}. All: {1}, dry run: {2}, force: {3}, thin: {4}".format(
                repository, all, dry_run, force, thin
            )
        )


    if __name__ == "__main__":
        main()

cltoolbox understands Sphinx, Google, and Numpy dostrings, from which it can
create short options and their help for you.

.. code-block:: console

    $ python git.py push -h
    usage: git.py push [-h] [--all] [-n] [-f] [--thin] repository

    Update remote refs along with associated objects.

    positional arguments:
      repository     Repository to push to.

    optional arguments:
      -h, --help     show this help message and exit
      --all          Push all refs.
      -n, --dry-run  Dry run.
      -f, --force    Force updates.
      --thin         Use thin pack.

Let's try it!

.. code-block:: console

    $ python git.py push --all myrepo
    Pushing to myrepo. All: True, dry run: False, force: False, thin: False

    $ python git.py push --all -f myrepo
    Pushing to myrepo. All: True, dry run: False, force: True, thin: False

    $ python git.py push --all -fn myrepo
    Pushing to myrepo. All: True, dry run: True, force: True, thin: False

    $ python git.py push --thin -fn myrepo
    Pushing to myrepo. All: False, dry run: True, force: True, thin: True

    $ python git.py push --thin
    usage: git.py push [-h] [--all] [-n] [-f] [--thin] repository
    git.py push: error: too few arguments

Amazed uh? Yes, cltoolbox got the short options and the help from the docstring!
You can put much more in the docstring, and if that isn't enough, there's an
``@arg`` decorator to customize the arguments that get passed to argparse.


Type annotations
----------------

cltoolbox understands Python 3-style type annotations and will warn the user if the
arguments given to a command are of the wrong type.

.. code-block:: python

    from cltoolbox import command, main


    @command
    def duplicate(string, times: int):
        """Duplicate text.

        :param string: The text to duplicate.
        :param times: How many times to duplicate."""

        print(string * times)


    if __name__ == "__main__":
        main()

.. code-block:: console

    $ python3 test.py duplicate "test " 5
    test test test test test

    $ python3 test.py duplicate "test " foo
    usage: test.py duplicate [-h] string times
    test.py duplicate: error: argument times: invalid int value: 'foo'


The `cltoolbox` supports shell autocompletion via the
``argcomplete`` package and supports custom format classes. For a complete
documentation, visit https://timcera.bibucket.io/cltoolbox/.
