argdispatch


Nameargdispatch JSON
Version 1.4.0 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_time2024-12-26 23:20:04
maintainerNone
docs_urlNone
authorLouis Paternault
requires_python<4,>=3.8
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": null,
    "docs_url": null,
    "requires_python": "<4,>=3.8",
    "maintainer_email": null,
    "keywords": "argparse argument commandline dispatch",
    "author": "Louis Paternault",
    "author_email": "spalax@gresille.org",
    "download_url": "https://files.pythonhosted.org/packages/96/8a/125428463e6ba19ffda856e901dfcf3b3c1172367eeeacf5ada12e38f145/argdispatch-1.4.0.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.4.0",
    "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": "ec44e3b23ea07e5fbd2c6fa7f992fb42e9cc3be62cf2db274e49e5ec1be42769",
                "md5": "22ecdc3cc674d46d59ff3b6024031f32",
                "sha256": "198a219972dbab132c4b42ddbd52ca063b766e015816bd7f61bad7f0f886a72a"
            },
            "downloads": -1,
            "filename": "argdispatch-1.4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "22ecdc3cc674d46d59ff3b6024031f32",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.8",
            "size": 21834,
            "upload_time": "2024-12-26T23:20:03",
            "upload_time_iso_8601": "2024-12-26T23:20:03.524437Z",
            "url": "https://files.pythonhosted.org/packages/ec/44/e3b23ea07e5fbd2c6fa7f992fb42e9cc3be62cf2db274e49e5ec1be42769/argdispatch-1.4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "968a125428463e6ba19ffda856e901dfcf3b3c1172367eeeacf5ada12e38f145",
                "md5": "9e921859949cd087db8812535303ac1e",
                "sha256": "3840b7c3fa9e32223b718ff5bb532fe0888943b4d6943fba96c713bfa409c71b"
            },
            "downloads": -1,
            "filename": "argdispatch-1.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9e921859949cd087db8812535303ac1e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8",
            "size": 42117,
            "upload_time": "2024-12-26T23:20:04",
            "upload_time_iso_8601": "2024-12-26T23:20:04.899742Z",
            "url": "https://files.pythonhosted.org/packages/96/8a/125428463e6ba19ffda856e901dfcf3b3c1172367eeeacf5ada12e38f145/argdispatch-1.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-26 23:20:04",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "argdispatch"
}
        
Elapsed time: 0.48717s