Skip to main content

Argparse enhanced with a config file to overwrite code-based defaults

Project description

argconfig

argparse + yaml config system

This takes the same format as the argparse.ArgumentParser class; however it adds the feature of setting a default yaml file for base configuration to override the default set in the code. Usage has the following overwrite priority:

command line > config file > default set in code

This enables a flexible configuration based, commandline interface options at scale.

Requirements

  • Python 3.7+
  • pyyaml
  • argparse

Example

Config File

Use the example.yaml config file in the same directory as your example python code:

$ cat example.yaml
foo : test1
bar : 2.0

Example Code

The example python code setsup the variables foo and bar to equal 'testing' and 2.0 respectively. (Note the syntax is almost identical as argparse.ArgumentParser.)

import argconfig

parser = argconfig.ArgumentParser(description='argconfig example',config='./example.yaml')
parser.add_argument('-f','--foo', type=str, default='testing',
                    help='foo (default=testing, config=test)')
parser.add_argument('--bar',
                    help='bar (default=None, config = 2.0)')
args = parser.parse_args()

print('foo:',args.foo)
print('bar:',args.bar)

Example Command Line Usage

Run time of the example with foo and bar set by command line:

python example.py --foo test --bar 3.0

returns the following two print statements:

foo: test
bar: 3.0

While the values set in example.yaml is used when the values are not overriden by the command line:

python example.py

returns the following two print statements:

foo: test1
bar: 2.0

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

argconfig-0.0.1.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

argconfig-0.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file argconfig-0.0.1.tar.gz.

File metadata

  • Download URL: argconfig-0.0.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.6

File hashes

Hashes for argconfig-0.0.1.tar.gz
Algorithm Hash digest
SHA256 dd732c1217fa54d8f4657c0a9bafba2154f67e749307d97d7c50bf2f56c05d17
MD5 9609cb236b923fba9cae52bf39f8f363
BLAKE2b-256 86a0aaf395102bc815d934c1b918231a651b795a40074ae1c17ff7ed0724d380

See more details on using hashes here.

File details

Details for the file argconfig-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: argconfig-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.6

File hashes

Hashes for argconfig-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 16fcdab4d201a56553c2d44fecde20b3dc6fb69a76c3742fc7a311d303c83f6e
MD5 da1fb00906fcaa1808ccb76d81c10ced
BLAKE2b-256 26106ae89662d261274a380f827230af07442a59941a865412952384fffae261

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