argdispatch


Nameargdispatch JSON
Version 1.3.1 PyPI version JSON
download
home_pagehttp://framagit.org/spalax/argdispatch
SummaryA drop-in replacement for `argparse` dispatching subcommand calls to functions, modules or binaries.
upload_time2023-10-03 19:59:59
maintainer
docs_urlNone
authorLouis Paternault
requires_python<4,>=3.7
licenseGPLv3 or any later version
keywords argparse argument commandline dispatch
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            argdispatch 🐈 Drop-in replacement for `argparse` dispatching subcommand calls to functions, modules or binaries
================================================================================================================

If your parser has a few subcommands, you can parse them with ``argparse``. If you have more, you still can, but you will get a huge, unreadable code. This module makes this easier by dispatching subcommand calls to functions, modules or binaries.

Examples
--------

Example 1 : Manual definition of subcommands
""""""""""""""""""""""""""""""""""""""""""""

For instance, consider the following code for ``mycommand.py``:

.. code-block:: python

   import sys
   from argdispatch import ArgumentParser

   def foo(args):
       """A function associated to subcommand `foo`."""
       print("Doing interesting stuff")
       sys.exit(1)

   if __name__ == "__main__":
       parser = ArgumentParser()
       subparser = parser.add_subparsers()

       subparser.add_function(foo)
       subparser.add_module("bar")
       subparser.add_executable("baz")

        parser.parse_args()

With this simple code:

* ``mycommand.py foo -v --arg=2`` is equivalent to the python code ``foo(['-v', '--arg=2'])``;
* ``mycommand.py bar -v --arg=2`` is equivalent to ``python -m bar -v --arg=2``;
* ``mycommand.py baz -v --arg=2`` is equivalent to ``baz -v --arg=2``.

Then, each function, module or binary does whatever it wants with the arguments.

Example 2 : Automatic definition of subcommands
"""""""""""""""""""""""""""""""""""""""""""""""

With programs like `git <http://git-scm.com/>`_, if a ``git-foo`` binary exists, then calling ``git foo --some=arguments`` is equivalent to ``git-foo --some=arguments``. The following code, in ``myprogram.py`` copies this behaviour:

.. code-block:: python

   import sys
   from argdispatch import ArgumentParser

   if __name__ == "__main__":
       parser = ArgumentParser()
       subparser = parser.add_subparsers()

       subparser.add_submodules("myprogram")
       subparser.add_prefix_executables("myprogram-")

       parser.parse_args()

With this program, given that binary ``myprogram-foo`` and python module ``myprogram.bar.__main__.py`` exist:

* ``myprogram foo -v --arg=2`` is equivalent to ``myprogram-foo -v --arg=2``;
* ``myprogram bar -v --arg=2`` is equivalent to ``python -m myprogram.bar -v --arg=2``.

Example 3 : Defining subcommands with entry points
--------------------------------------------------

Now that your program is popular, people start writing plugins. Great! You want to allow them to add subcommands to your program. To do so, simply use this code:

.. code-block:: python

   import sys
   from argdispatch import ArgumentParser

   if __name__ == "__main__":
       parser = ArgumentParser()
       subparser = parser.add_subparsers()

       # You probably should only have one of those.
       subparser.add_entrypoints_functions("myprogram.subcommand.function")
       subparser.add_entrypoints_modules("myprogram.subcommand.module")

       parser.parse_args()

With this code, plugin writers can add lines like those in their ``setup.cfg``:

.. code-block:: cfg

   [options.entry_points]
   myprogram.subcommand.function =
       foo = mypluginfoo:myfunction
   myprogram.subcommand.module =
       bar = mypluginbar

Then, given than function ``myfunction()`` exists in module ``mypluginfoo``, and than module ``mypluginbar`` exists:

* ``myprogram foo -v --arg=2`` is equivalent to the python code ``myfunction(['-v', '--arg=2'])``;
* ``myprogram bar -v --arg=2`` is equivalent to ``python -m mypluginbar -v --arg=2``.

Documentation
"""""""""""""

The complete documentation is available on `readthedocs <http://argdispatch.readthedocs.io>`_.

To compile it from source, download and run::

      cd doc && make html

What's new?
-----------

See `changelog <https://framagit.org/spalax/argdispatch/blob/main/CHANGELOG.md>`_.

Download and install
--------------------

* From sources:

  * Download: https://pypi.python.org/pypi/argdispatch
  * Install (in a `virtualenv`, if you do not want to mess with your distribution installation system)::

        python3 -m pip install .

* From pip::

    pip install argdispatch

* Quick and dirty Debian (and Ubuntu?) package

  This requires `stdeb <https://github.com/astraw/stdeb>`_ to be installed::

      python3 setup.py --command-packages=stdeb.command bdist_deb
      sudo dpkg -i deb_dist/argdispatch-<VERSION>_all.deb

            

Raw data

            {
    "_id": null,
    "home_page": "http://framagit.org/spalax/argdispatch",
    "name": "argdispatch",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "<4,>=3.7",
    "maintainer_email": "",
    "keywords": "argparse argument commandline dispatch",
    "author": "Louis Paternault",
    "author_email": "spalax@gresille.org",
    "download_url": "https://files.pythonhosted.org/packages/eb/3a/73c5217259db73208702c7f04fe387be8487ea3ba6493e3f143780d3444b/argdispatch-1.3.1.tar.gz",
    "platform": null,
    "description": "argdispatch \ud83d\udc08 Drop-in replacement for `argparse` dispatching subcommand calls to functions, modules or binaries\n================================================================================================================\n\nIf your parser has a few subcommands, you can parse them with ``argparse``. If you have more, you still can, but you will get a huge, unreadable code. This module makes this easier by dispatching subcommand calls to functions, modules or binaries.\n\nExamples\n--------\n\nExample 1 : Manual definition of subcommands\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nFor instance, consider the following code for ``mycommand.py``:\n\n.. code-block:: python\n\n   import sys\n   from argdispatch import ArgumentParser\n\n   def foo(args):\n       \"\"\"A function associated to subcommand `foo`.\"\"\"\n       print(\"Doing interesting stuff\")\n       sys.exit(1)\n\n   if __name__ == \"__main__\":\n       parser = ArgumentParser()\n       subparser = parser.add_subparsers()\n\n       subparser.add_function(foo)\n       subparser.add_module(\"bar\")\n       subparser.add_executable(\"baz\")\n\n        parser.parse_args()\n\nWith this simple code:\n\n* ``mycommand.py foo -v --arg=2`` is equivalent to the python code ``foo(['-v', '--arg=2'])``;\n* ``mycommand.py bar -v --arg=2`` is equivalent to ``python -m bar -v --arg=2``;\n* ``mycommand.py baz -v --arg=2`` is equivalent to ``baz -v --arg=2``.\n\nThen, each function, module or binary does whatever it wants with the arguments.\n\nExample 2 : Automatic definition of subcommands\n\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nWith programs like `git <http://git-scm.com/>`_, if a ``git-foo`` binary exists, then calling ``git foo --some=arguments`` is equivalent to ``git-foo --some=arguments``. The following code, in ``myprogram.py`` copies this behaviour:\n\n.. code-block:: python\n\n   import sys\n   from argdispatch import ArgumentParser\n\n   if __name__ == \"__main__\":\n       parser = ArgumentParser()\n       subparser = parser.add_subparsers()\n\n       subparser.add_submodules(\"myprogram\")\n       subparser.add_prefix_executables(\"myprogram-\")\n\n       parser.parse_args()\n\nWith this program, given that binary ``myprogram-foo`` and python module ``myprogram.bar.__main__.py`` exist:\n\n* ``myprogram foo -v --arg=2`` is equivalent to ``myprogram-foo -v --arg=2``;\n* ``myprogram bar -v --arg=2`` is equivalent to ``python -m myprogram.bar -v --arg=2``.\n\nExample 3 : Defining subcommands with entry points\n--------------------------------------------------\n\nNow that your program is popular, people start writing plugins. Great! You want to allow them to add subcommands to your program. To do so, simply use this code:\n\n.. code-block:: python\n\n   import sys\n   from argdispatch import ArgumentParser\n\n   if __name__ == \"__main__\":\n       parser = ArgumentParser()\n       subparser = parser.add_subparsers()\n\n       # You probably should only have one of those.\n       subparser.add_entrypoints_functions(\"myprogram.subcommand.function\")\n       subparser.add_entrypoints_modules(\"myprogram.subcommand.module\")\n\n       parser.parse_args()\n\nWith this code, plugin writers can add lines like those in their ``setup.cfg``:\n\n.. code-block:: cfg\n\n   [options.entry_points]\n   myprogram.subcommand.function =\n       foo = mypluginfoo:myfunction\n   myprogram.subcommand.module =\n       bar = mypluginbar\n\nThen, given than function ``myfunction()`` exists in module ``mypluginfoo``, and than module ``mypluginbar`` exists:\n\n* ``myprogram foo -v --arg=2`` is equivalent to the python code ``myfunction(['-v', '--arg=2'])``;\n* ``myprogram bar -v --arg=2`` is equivalent to ``python -m mypluginbar -v --arg=2``.\n\nDocumentation\n\"\"\"\"\"\"\"\"\"\"\"\"\"\n\nThe complete documentation is available on `readthedocs <http://argdispatch.readthedocs.io>`_.\n\nTo compile it from source, download and run::\n\n      cd doc && make html\n\nWhat's new?\n-----------\n\nSee `changelog <https://framagit.org/spalax/argdispatch/blob/main/CHANGELOG.md>`_.\n\nDownload and install\n--------------------\n\n* From sources:\n\n  * Download: https://pypi.python.org/pypi/argdispatch\n  * Install (in a `virtualenv`, if you do not want to mess with your distribution installation system)::\n\n        python3 -m pip install .\n\n* From pip::\n\n    pip install argdispatch\n\n* Quick and dirty Debian (and Ubuntu?) package\n\n  This requires `stdeb <https://github.com/astraw/stdeb>`_ to be installed::\n\n      python3 setup.py --command-packages=stdeb.command bdist_deb\n      sudo dpkg -i deb_dist/argdispatch-<VERSION>_all.deb\n",
    "bugtrack_url": null,
    "license": "GPLv3 or any later version",
    "summary": "A drop-in replacement for `argparse` dispatching subcommand calls to functions, modules or binaries.",
    "version": "1.3.1",
    "project_urls": {
        "Documentation": "http://argdispatch.readthedocs.io",
        "Homepage": "http://framagit.org/spalax/argdispatch",
        "Source": "https://framagit.org/spalax/argdispatch",
        "Tracker": "https://framagit.org/spalax/argdispatch/issues"
    },
    "split_keywords": [
        "argparse",
        "argument",
        "commandline",
        "dispatch"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7b287396d8a38096b987c4f89a14d1d119c2a27b17d2fb8a2b58ef77a4571663",
                "md5": "a89c8757e54c92ff9eb97e05f049790c",
                "sha256": "fbc7623fb6f90e26df54a4a9f4ddd5a8162fe94f47547f8cacdf5fd38693635b"
            },
            "downloads": -1,
            "filename": "argdispatch-1.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a89c8757e54c92ff9eb97e05f049790c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.7",
            "size": 21812,
            "upload_time": "2023-10-03T19:59:58",
            "upload_time_iso_8601": "2023-10-03T19:59:58.217589Z",
            "url": "https://files.pythonhosted.org/packages/7b/28/7396d8a38096b987c4f89a14d1d119c2a27b17d2fb8a2b58ef77a4571663/argdispatch-1.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eb3a73c5217259db73208702c7f04fe387be8487ea3ba6493e3f143780d3444b",
                "md5": "6a2ce56356493d6543c803e0cdf8faf4",
                "sha256": "cb857465fd3e6cf3e2b583474b16a84b4e166cb1e68e035a80bdc13b3c51061c"
            },
            "downloads": -1,
            "filename": "argdispatch-1.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6a2ce56356493d6543c803e0cdf8faf4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.7",
            "size": 41850,
            "upload_time": "2023-10-03T19:59:59",
            "upload_time_iso_8601": "2023-10-03T19:59:59.739597Z",
            "url": "https://files.pythonhosted.org/packages/eb/3a/73c5217259db73208702c7f04fe387be8487ea3ba6493e3f143780d3444b/argdispatch-1.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-03 19:59:59",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "argdispatch"
}
        
Elapsed time: 0.38505s