Skip to main content

Library for parsing config files and command line arguments

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

groper-0.4.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

groper-0.4.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file groper-0.4.1.tar.gz.

File metadata

  • Download URL: groper-0.4.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for groper-0.4.1.tar.gz
Algorithm Hash digest
SHA256 b0516919db4003c282aa2678351fa27000ad7e36f7fee454e7b4e25cf26a74e3
MD5 69b4481aa5d7dbf0b73bc01ce23ff48c
BLAKE2b-256 767c83e9bf57ed03a89d2e8994f439e693330887977f72c72f8dec840a879afb

See more details on using hashes here.

File details

Details for the file groper-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: groper-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for groper-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 455b6a2ad64a82c5399c0f9e974cd374f8330f17dfa78ff34ddcb073657fbc5f
MD5 c8b510cb634bf1908bd24e0dcd6ae9dc
BLAKE2b-256 1767dada5a256a0dd4aca459bab5df8042dfbecc9ffd86f3859d6a55e46e73a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page