{ "info": { "author": "xhjkl", "author_email": "xhjkl@users.noreply.github.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.2", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Topic :: Software Development" ], "description": "This library draws a parallel between command line args and function args in Python.\nPositionals get passed to regular function arguments, and options or flags are mapped to keyword arguments.\n\nFor example, this command invocation::\n\n $ package install -u ffmpeg -v\n\nCould be translated to this function call::\n\n package.install('ffmpeg', upgrade=True, verbose=1)\n\nThis library does the bridging automatically\nusing information supplied in form of decorators and type annotations.\nTo make a function accessible as a command-line action, decorate it with `action`::\n\n import sys\n import action\n\n @action\n def install(package_name, *, upgrade: action.Flag = False, verbose: action.Count = 0):\n \"\"\" Do the work\n \"\"\"\n\n if __name__ == '__main__':\n sys.exit(action.execute(sys.argv[1:]))\n\nAll other exported symbols are described below.\n\n----\n\n@action\n=======\nThe main decorator which is used to make actions from functions.\nIt takes a single function as an input and inspects its signature.\n\nThe name of the command being created\nis drawn from the name of original function.\n\nAll arguments before splat are counted as positionals,\nand those going after are options or flags.\n\nConfiguration through annotations\n=================================\nClient code could alter how certain arguments\nare treated and presented by annotating its arguments.\n\nOne way to do so is to supply a constructor as an annotation::\n\n @action\n def add(x: int, y: int):\n print(x + y)\n\nThat constructor shall be called upon execution to coerce types\nbefore passing arguments to the action invoked.\n\nPositionals only support this kind of annotations.\n\nOptions, on the other hand, use callable annotations differently.\nEach option or flag could occur many times,\ntherefore that behaviour should be covered by corresponding\nannotation.\nThere are some sane defaults come already packaged.\n\nFlag\n----\nDenotes whether some condition is truthy.\nCould be specified any number of times on command line.\nFirst occurrence sets the value to `True`.\nSubsequent occurrences have no effect::\n\n @action\n def add(x: int, y: int, *, pad: action.Flag = False):\n result = x + y\n format = '{}'\n if pad:\n format = '{:04}'\n print(format.format(result))\n\nCount\n-----\nInitially is `None`.\nOn first occurrence sets to one,\non each subsequent occurrence increments by one::\n\n @action\n def add(x: int, y: int, *, verbose: action.Count = 0):\n result = x + y\n\n if verbose > 3:\n print('augend:', x)\n print('addend:', y)\n print('sum: ', result)\n print()\n elif verbose > 0:\n print('{} + {} = {}'.format(result))\n else:\n print(result)\n\nKey\n---\nGeneric value specified as a command-line option::\n\n @action\n def walk(*, depth: action.Key('depth', type=int)):\n ...\n\n`Key` constructor has three arguments: `short`, `long` and `type`.\nOne of `short` or `long` is required. `type` is `str` by default.\n\nany callable\n------------\nThere is also a shorthand notation for specifying a Key::\n\n @action\n def walk(*, depth: int):\n ...\n\nShort and long names shall be deduced from the argument name.\n\n(short, long, type) triple\n--------------------------\nAnother shorthand for Key allows\nto specify short and long names manually::\n\n @action\n def walk(*, depth: ('r', 'depth', int)):\n ...\n\nOption abstract base\n--------------------\nOn a low level, to know a value for an option, the command line\nprocessor performs a folding operation over all occurrences\nof a certain option.\nTherefore, to have fine-grained control over the argument parsing\nprocess, one could subclass `action.Option` to use it\ninstead of prepackaged annotations for options.\nSubclass should override call method to take two arguments:\nthe old value and an option body.\nThat call method could either return a new value or throw an exception\nto stop command line processing right away.\nIf call method returns a value, that value shall be passed\nas old value on the next call.\n\n@action.default\n===============\nThe command line processor selects an action\nwhose name matches the first positional.\nIf there is no such action registered,\nthe command line processor attempts\nto invoke the special action marked as default::\n\n @action.default\n @action\n def install(package):\n ...\n\n # `./prog.py install ffmpeg` shall invoke `install('ffmpeg')`\n # and `./prog.py ffmpeg` shall still invoke `install('ffmpeg')`\n\nThis decorator could also be used if the program\nhas a single action::\n\n @action.default\n def list_directory():\n ...\n\naction.execute\n==============\nLook up a previously registered action whose name matches\nfirst positional from command line,\nmatch command-line arguments to selected action arguments\nand invoke that action.\n\nThe first positional argument is hidden from the command invoked.\n\n`action.execute` never calls `os.exit`,\nso it could be used in an interactive prompt.\n\naction.context\n==============\nIf you want an isolated argument parser to avoid modification\nof module-wide state, you could instantiate another `Action`\nwith this method.\n\nNormally, an `Action` object is constructed in place\nof `action` module when importing.\n\n----\n\nCoded with Love.\n\n\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/xhjkl/action.py", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "action", "package_url": "https://pypi.org/project/action/", "platform": "", "project_url": "https://pypi.org/project/action/", "project_urls": { "Homepage": "https://github.com/xhjkl/action.py" }, "release_url": "https://pypi.org/project/action/1.4.4/", "requires_dist": null, "requires_python": "", "summary": "A command-line parser you won't hate", "version": "1.4.4" }, "last_serial": 2868694, "releases": { "1.1.0b4": [ { "comment_text": "", "digests": { "md5": "3af21474bb01f4547d5fd4b55a396b10", "sha256": "e2736ad48cd209bed0c8c82761f9ddb06a9582ef526d004bf142481a21bb3a22" }, "downloads": -1, "filename": "action-1.1.0b4-py3-none-any.whl", "has_sig": false, "md5_digest": "3af21474bb01f4547d5fd4b55a396b10", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9735, "upload_time": "2016-03-14T06:50:04", "url": "https://files.pythonhosted.org/packages/45/12/4b3fdc28b8d1e61322a0fe7c389d5b5cc72b952808ab228b4cc9cb4f9702/action-1.1.0b4-py3-none-any.whl" } ], "1.4.4": [ { "comment_text": "", "digests": { "md5": "08bb0d1068fa7febae2459155cc5f10c", "sha256": "c05e5c98eed350faa4d298decd347a94bf6dab1b35b9ca18432fadfd466e09a2" }, "downloads": -1, "filename": "action-1.4.4-py3-none-any.whl", "has_sig": false, "md5_digest": "08bb0d1068fa7febae2459155cc5f10c", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 10605, "upload_time": "2017-05-12T05:34:14", "url": "https://files.pythonhosted.org/packages/3f/4f/a278769f381237ac10b25de96db124aed7b9b9209812a800398a778c7622/action-1.4.4-py3-none-any.whl" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "08bb0d1068fa7febae2459155cc5f10c", "sha256": "c05e5c98eed350faa4d298decd347a94bf6dab1b35b9ca18432fadfd466e09a2" }, "downloads": -1, "filename": "action-1.4.4-py3-none-any.whl", "has_sig": false, "md5_digest": "08bb0d1068fa7febae2459155cc5f10c", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 10605, "upload_time": "2017-05-12T05:34:14", "url": "https://files.pythonhosted.org/packages/3f/4f/a278769f381237ac10b25de96db124aed7b9b9209812a800398a778c7622/action-1.4.4-py3-none-any.whl" } ] }