Skip to main content

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

Release files for groper 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for groper 0.4.1
File Size Uploaded
groper-0.4.1.tar.gz 9.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for groper 0.4.1
File Interpreter ABI Platform
groper-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.9 kB

Release files / groper-0.4.1.tar.gz

Download URL groper-0.4.1.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b0516919db4003c282aa2678351fa27000ad7e36f7fee454e7b4e25cf26a74e3
BLAKE2b-256 checksum
How to use checksums
767c83e9bf57ed03a89d2e8994f439e693330887977f72c72f8dec840a879afb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release files / groper-0.4.1-py3-none-any.whl

Download URL groper-0.4.1-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
455b6a2ad64a82c5399c0f9e974cd374f8330f17dfa78ff34ddcb073657fbc5f
BLAKE2b-256 checksum
How to use checksums
1767dada5a256a0dd4aca459bab5df8042dfbecc9ffd86f3859d6a55e46e73a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.3.1

1 release file

0.3.0

1 release file

0.1.7

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page