Name | groper JSON |
Version |
0.4.1
JSON |
| download |
home_page | None |
Summary | Library for parsing config files and command line arguments |
upload_time | 2024-10-24 20:28:30 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.1 |
license | Copyright (C) 2011-2024 Igor Partola 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. |
keywords |
args
command line
getopt
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
## groper: simple Python command line and config file options
Python programs that run from the command line often times contain code that looks like this:
DEFAULT_PORT = 8080
def main():
if len(sys.argv) > 2 and sys.argv[1] == '-p':
port = int(sys.argv[2])
else:
port = DEFAULT_PORT
While technically correct, this code does not scale well. The next evolution of this code would use either getopt, argparse or optparse to help abstract parsing command line options. However, at some point the number of options would grow so large that a configuration file might be warranted and ConfigParser is introduced.
However, the complexity of the code grows as this happens. To use (argparse|optparse|getopt) + ConfigParser effectively and correctly the program needs to:
* Parse the command line args, and figure out where the configuration file resides
* Read the configuration file and combine it with the command line args (the command line args take precidence)
* If not all required command line args are specified, print program usage and exit.
* The wrapper around ConfigPraser needs to provide a simple way to access the data
* Some values need to have defaults, which should be used if the config file and the command line args do not specify one
Implementing this logic in each Python program is redundant. However, it seems that most programs do it this way. groper provides a simple unified interface for reading command line args, configuration files, setting defaults, printing program usage and even generating sample configuration files. Here is an example of its usage:
from groper import define_opt, init_options
define_opt('server', 'host', type=str, cmd_name='host', cmd_short_name='h', default='localhost')
define_opt('server', 'port', type=int, cmd_name='port', cmd_short_name='p', default=8080)
define_opt('server', 'daemon', type=bool, cmd_name='daemon', cmd_short_name='d')
# init_options() will automatically read this file. If you don't use a config file, simply comment this out
#define_opt('meta', 'config', type=str, cmd_only=True, cmd_name='config', cmd_short_name='c', is_config_file=True)
# If specified: program usage will be printed and returned
# The cmd_group param means that when the usage is printed, this option will be specified in its own group
define_opt('meta', 'help', type=bool, cmd_only=True, cmd_name='help', cmd_short_name='H', is_help=True, cmd_group='help')
def main():
options = init_options()
print(options.server.host)
print(options.server.port)
print(options.server.daemon)
if __name__ == '__main__':
main()
That's it. You can use the options object from any module in which you import it to get access to the program settings. The *default* argument to the define_opt() function also provides a great feature: you can now specify all your constants as configurable options that can be read from the configuration file.
As if that wasn't enough, you can even generate sample configuration files:
from groper import define_opt, init_options, options
define_opt('server', 'host', type=str, cmd_name='host', cmd_short_name='h', default='localhost')
define_opt('server', 'port', type=int, cmd_name='port', cmd_short_name='p', default=8080)
define_opt('server', 'daemon', type=bool, cmd_name='daemon', cmd_short_name='d')
print generate_sample_config()
Hopefully you will find groper useful. It can be installed via PyPi:
$ pip install groper
groper is licensed under the MIT license and is Copyright (c) 2011-2024 Igor Partola
Raw data
{
"_id": null,
"home_page": null,
"name": "groper",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.1",
"maintainer_email": "Igor Partola <igor@igorpartola.com>",
"keywords": "args, command line, getopt",
"author": null,
"author_email": "Igor Partola <igor@igorpartola.com>",
"download_url": "https://files.pythonhosted.org/packages/76/7c/83e9bf57ed03a89d2e8994f439e693330887977f72c72f8dec840a879afb/groper-0.4.1.tar.gz",
"platform": null,
"description": "## groper: simple Python command line and config file options\n\nPython programs that run from the command line often times contain code that looks like this:\n\n DEFAULT_PORT = 8080\n def main():\n if len(sys.argv) > 2 and sys.argv[1] == '-p':\n port = int(sys.argv[2])\n else:\n port = DEFAULT_PORT\n\nWhile technically correct, this code does not scale well. The next evolution of this code would use either getopt, argparse or optparse to help abstract parsing command line options. However, at some point the number of options would grow so large that a configuration file might be warranted and ConfigParser is introduced.\n\nHowever, the complexity of the code grows as this happens. To use (argparse|optparse|getopt) + ConfigParser effectively and correctly the program needs to:\n\n* Parse the command line args, and figure out where the configuration file resides\n* Read the configuration file and combine it with the command line args (the command line args take precidence)\n* If not all required command line args are specified, print program usage and exit.\n* The wrapper around ConfigPraser needs to provide a simple way to access the data\n* Some values need to have defaults, which should be used if the config file and the command line args do not specify one\n\nImplementing this logic in each Python program is redundant. However, it seems that most programs do it this way. groper provides a simple unified interface for reading command line args, configuration files, setting defaults, printing program usage and even generating sample configuration files. Here is an example of its usage:\n\n\n from groper import define_opt, init_options\n\n define_opt('server', 'host', type=str, cmd_name='host', cmd_short_name='h', default='localhost')\n define_opt('server', 'port', type=int, cmd_name='port', cmd_short_name='p', default=8080)\n define_opt('server', 'daemon', type=bool, cmd_name='daemon', cmd_short_name='d')\n\n # init_options() will automatically read this file. If you don't use a config file, simply comment this out\n #define_opt('meta', 'config', type=str, cmd_only=True, cmd_name='config', cmd_short_name='c', is_config_file=True)\n\n # If specified: program usage will be printed and returned\n # The cmd_group param means that when the usage is printed, this option will be specified in its own group\n define_opt('meta', 'help', type=bool, cmd_only=True, cmd_name='help', cmd_short_name='H', is_help=True, cmd_group='help')\n\n def main():\n options = init_options()\n print(options.server.host)\n print(options.server.port)\n print(options.server.daemon)\n\n if __name__ == '__main__':\n main()\n\n\nThat's it. You can use the options object from any module in which you import it to get access to the program settings. The *default* argument to the define_opt() function also provides a great feature: you can now specify all your constants as configurable options that can be read from the configuration file.\n\nAs if that wasn't enough, you can even generate sample configuration files:\n\n\n from groper import define_opt, init_options, options\n\n define_opt('server', 'host', type=str, cmd_name='host', cmd_short_name='h', default='localhost')\n define_opt('server', 'port', type=int, cmd_name='port', cmd_short_name='p', default=8080)\n define_opt('server', 'daemon', type=bool, cmd_name='daemon', cmd_short_name='d')\n\n print generate_sample_config()\n\n\nHopefully you will find groper useful. It can be installed via PyPi:\n\n $ pip install groper\n\ngroper is licensed under the MIT license and is Copyright (c) 2011-2024 Igor Partola\n\n",
"bugtrack_url": null,
"license": "Copyright (C) 2011-2024 Igor Partola 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.",
"summary": "Library for parsing config files and command line arguments",
"version": "0.4.1",
"project_urls": {
"Bug Tracker": "https://github.com/ipartola/groper/issues",
"Changelog": "https://github.com/ipartola/groper/commits/main/",
"Documentation": "https://github.com/ipartola/groper",
"Homepage": "https://github.com/ipartola/groper",
"Repository": "https://github.com/ipartola/groper.git"
},
"split_keywords": [
"args",
" command line",
" getopt"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1767dada5a256a0dd4aca459bab5df8042dfbecc9ffd86f3859d6a55e46e73a4",
"md5": "c8b510cb634bf1908bd24e0dcd6ae9dc",
"sha256": "455b6a2ad64a82c5399c0f9e974cd374f8330f17dfa78ff34ddcb073657fbc5f"
},
"downloads": -1,
"filename": "groper-0.4.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c8b510cb634bf1908bd24e0dcd6ae9dc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.1",
"size": 8686,
"upload_time": "2024-10-24T20:28:29",
"upload_time_iso_8601": "2024-10-24T20:28:29.007763Z",
"url": "https://files.pythonhosted.org/packages/17/67/dada5a256a0dd4aca459bab5df8042dfbecc9ffd86f3859d6a55e46e73a4/groper-0.4.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "767c83e9bf57ed03a89d2e8994f439e693330887977f72c72f8dec840a879afb",
"md5": "69b4481aa5d7dbf0b73bc01ce23ff48c",
"sha256": "b0516919db4003c282aa2678351fa27000ad7e36f7fee454e7b4e25cf26a74e3"
},
"downloads": -1,
"filename": "groper-0.4.1.tar.gz",
"has_sig": false,
"md5_digest": "69b4481aa5d7dbf0b73bc01ce23ff48c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.1",
"size": 9194,
"upload_time": "2024-10-24T20:28:30",
"upload_time_iso_8601": "2024-10-24T20:28:30.202641Z",
"url": "https://files.pythonhosted.org/packages/76/7c/83e9bf57ed03a89d2e8994f439e693330887977f72c72f8dec840a879afb/groper-0.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-24 20:28:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ipartola",
"github_project": "groper",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "groper"
}