Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save islandjoe/1d8eeb5896f3f4a32a2dbc488ac113e6 to your computer and use it in GitHub Desktop.
Save islandjoe/1d8eeb5896f3f4a32a2dbc488ac113e6 to your computer and use it in GitHub Desktop.
How to implement Python 3 ArgumentParser with arg from a config file.
import argparse
import json

arg = argparse.ArgumentParser()
arg.add_argument("-c", "--conf", required=True, help="path to the JSON configuration file")

args = vars(arg.parse_args())

conf = json.load(open(args["conf"]))

Where configuration file is config.json:

{
	"access_token": "Abnmdfjkfe$5",
	"base_path": "home"
}

How to use conf variable:

token = conf["access_token"]
path = conf["base_path"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment