{ "info": { "author": "Tikitu de Jager", "author_email": "tikitu@logophile.org", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Environment :: Console", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 2.7", "Topic :: Utilities" ], "description": "The ``argparse_config`` utility reads defaults for commandline args from a\nconfig file. The cute thing is, it figures out what config options to expect\nbased on your ``argparse`` commandline args definition.\n\nLet's say I'm reimplementing the Mercurial commandline client. I specify the\ncommandline argument processing with ``argparse``, of course::\n\n >>> arg_parser = ArgumentParser('hg')\n >>> arg_parser.add_argument('--repository')\n >>> sub_parsers = arg_parser.add_subparsers()\n >>>\n >>> merge_parser = sub_parsers.add_parser('merge')\n >>> merge_parser.add_argument('--tool')\n >>> merge_parser.add_argument('--force', action='store_true', default=False)\n >>>\n >>> commit_parser = sub_parsers.add_parser('commit')\n >>> commit_parser.add_argument('--user')\n >>> commit_parser.add_argument('--message')\n\nWhen I go to use this client, though, I have to keep specifying my ``--user``\nwith every commit, and ``--tool`` with every merge. That sucks! What I want is\nto have my client understand a simple config file format::\n\n [merge]\n tool: meld\n\n [commit]\n user: Tikitu de Jager \n\nAnd obviously, as I add more arguments and subcommands to my client, it should\nallow me to add defaults in the config file without writing more code.\n\nThis is what ``argparse_config`` gives you. To use it with the mercurial client\n``arg_parser`` above::\n\n >>> import argparse_config\n >>> argparse_config.read_config_file(arg_parser, '/home/tikitu/.my_hg.cfg')\n\n... and that's it. Calling ``arg_parser.parse_args()`` will parse args as usual,\nbut the *default* values will be taken from the config file, if they are given\nthere::\n\n >>> parsed_args = arg_parser.parse_args(['merge'])\n >>> parsed_args.tool\n 'meld'\n\nWhat can I put in the config file?\n----------------------------------\n\nUnder the hood ``argparse_config`` uses the standard library ConfigParser_.\nArguments that aren't for a subcommand go in the section ``[default]``. The\nnames are munged from the commandline argument, removing leading dashes and\nconverting internal dashes to underscores (e.g. ``--log-level`` becomes\n``log_level:``).\n\n.. _ConfigParser: http://docs.python.org/2/library/configparser.html\n\nFlags (i.e. commandline args that take no parameters) are turned on if present\nin the config, just like the commandline::\n\n [default]\n verbose\n\nis the equivalent of ``--verbose``. Either ``verbose:`` or ``verbose`` will work,\nbut (watch out!) ``verbose: a-value`` doesn't do anything different to ``verbose``.\n\nWriting a config file from some commandline arguments\n-----------------------------------------------------\n\nIncluded in the package is a utility to generate a config file following these\nrules, from a given set of commandline arguments. The easy way to use it is to\nlet it add a command to your args parsing::\n\n >>> config_parser = get_config_parser('/home/tikitu/.my_hg.cfg')\n >>> add_config_block_subcommand(arg_parser, sub_parsers, config_parser=config_parser)\n\nand use it on the commandline::\n\n $ hg.py config default --repository ssh://hg@bitbucket.org/tikitu/argparse_config > new_config.rc\n $ hg.py config commit --username 'Tikitu de Jager ' >> new_config.rc\n $ cat new_config.rc\n [default]\n repository: ssh://hg@bitbucket.org/tikitu/argparse_config\n [commit]\n username: Tikitu de Jager \n\nYou can also use it programatically, if you like. That looks like this::\n\n >>> parsed_args = arg_parser.parse_args(['--repository', 'https://bitbucket.org/tikitu/argparse_config', 'merge'])\n >>> print argparse_config.generate_config(arg_parser, parsed_args, section='default', only_non_defaults=True)\n [default]\n repository: https://bitbucket.org/tikitu/argparse_config\n\nSome complications make this less useful than it could be, sadly:\n\n* If you use subcommands, you can only parse the args for one of them at a time\n (use the ``section`` argument to specify which, or leave it off to get the\n ``[default]`` section).\n* We can't tell the difference between default values written in code (which should\n not be added to the config file) and written in a previously-read-in config file\n (which should). This is why ``only_non_defaults`` exists.\n\nHow does it work?\n-----------------\n\nBy gudgeling about in the private internals of ``argparse``. Yes, that's not\npretty.\n\nGotchas\n-------\n\nAny required arguments that are present in a config file will show as optional,\nnot required, in the ``--help`` output. (This is a bug-by-design, due to not\nhaving any clever idea about how to do it better.) It may help to tell\nyourself, \"It's not required *on the commandline* because I gave it in the\nconfig file.\" (I will gladly make this dodgy rationalisation disappear if I\nfigure out how to handle required arguments more tidily.)\n\nHacking\n-------\n\nIt's on BitBucket_. Feel free to play. It comes with a handy ``zc.buildout``\nwrapper too, overkill though that clearly is.\n\n.. _BitBucket: http://bitbucket.org/tikitu/argparse_config\n\nTODO\n----\n\nIt's \"alpha software\" at present; likely to be buggy and lots of stuff ain't\nthere yet. Check the `issues list`_ to stay up to date. Some remaining open issues:\n\n* How to deal with multi-value args? (The config-file library this is built on doesn't support them.)\n* The \"write me a config file\" support is scrappy. Can we do better?\n\n.. _issues list: http://bitbucket.org/tikitu/argparse_config/issues?status=new&status=open", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://bitbucket.org/tikitu/argparse_config", "keywords": "", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "argparse_config", "package_url": "https://pypi.org/project/argparse_config/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/argparse_config/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://bitbucket.org/tikitu/argparse_config" }, "release_url": "https://pypi.org/project/argparse_config/0.5.1/", "requires_dist": null, "requires_python": null, "summary": "Default values for argparse commandline args read from a config file.", "version": "0.5.1" }, "last_serial": 786425, "releases": { "0.4": [ { "comment_text": "", "digests": { "md5": "e4b814fe52cbd62b960227cb2a4b80bf", "sha256": "383fd4921d918e24a681837e6106914e87359fd498a4eb931bcf3d0207a53f73" }, "downloads": -1, "filename": "argparse_config-0.4.tar.gz", "has_sig": false, "md5_digest": "e4b814fe52cbd62b960227cb2a4b80bf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6175, "upload_time": "2012-12-22T18:03:53", "url": "https://files.pythonhosted.org/packages/1c/f8/afdae75d40b479d9fa73d1439ee29ea67a4fa6db89384369d7e1216d43b8/argparse_config-0.4.tar.gz" } ], "0.4a4": [ { "comment_text": "", "digests": { "md5": "956a0fcfb0cef137a07759cf322fe092", "sha256": "7deb420ee27fd4f419fb89234dbb3f5422676d6dd36fa761747bdc3c8518f7af" }, "downloads": -1, "filename": "argparse_config-0.4a4.tar.gz", "has_sig": false, "md5_digest": "956a0fcfb0cef137a07759cf322fe092", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4732, "upload_time": "2012-11-25T20:42:23", "url": "https://files.pythonhosted.org/packages/c1/22/39760aaf30f98e45cd84a9e3465a4f9fed7eab1ab5fd88a8d365118c2a07/argparse_config-0.4a4.tar.gz" } ], "0.5.1": [ { "comment_text": "", "digests": { "md5": "55907c9ee57fb6f42d0889d7d7e17afc", "sha256": "432cb92e15e66ba819e207fba1c6e32a9344fa8fd7dca84489da1d1c7a670bb8" }, "downloads": -1, "filename": "argparse_config-0.5.1.tar.gz", "has_sig": false, "md5_digest": "55907c9ee57fb6f42d0889d7d7e17afc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7095, "upload_time": "2013-02-09T23:13:44", "url": "https://files.pythonhosted.org/packages/b1/86/d2cae485867b2ea31c5b8d6e3e52b35eb61dabc14c0cbec3549b63cf7a41/argparse_config-0.5.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "55907c9ee57fb6f42d0889d7d7e17afc", "sha256": "432cb92e15e66ba819e207fba1c6e32a9344fa8fd7dca84489da1d1c7a670bb8" }, "downloads": -1, "filename": "argparse_config-0.5.1.tar.gz", "has_sig": false, "md5_digest": "55907c9ee57fb6f42d0889d7d7e17afc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7095, "upload_time": "2013-02-09T23:13:44", "url": "https://files.pythonhosted.org/packages/b1/86/d2cae485867b2ea31c5b8d6e3e52b35eb61dabc14c0cbec3549b63cf7a41/argparse_config-0.5.1.tar.gz" } ] }