Skip to content

Instantly share code, notes, and snippets.

@maltefiala
Last active December 15, 2022 16:59
Show Gist options
  • Save maltefiala/b298647bff10332692c44a0c9899c32c to your computer and use it in GitHub Desktop.
Save maltefiala/b298647bff10332692c44a0c9899c32c to your computer and use it in GitHub Desktop.
import argparse
import configparser
# Parse the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('filename', help='the name of the config file')
args = parser.parse_args()
# Parse the input data
config = configparser.ConfigParser()
config.read(args.filename)
# Extract the username and urlpath
username = config['Data']['username']
urlpath = config['Data']['urlpath']
# Append username and urlpath to each url
for url in config['Urls'].values():
url = url.rstrip('/')
scheme, _, host = url.partition('://')
hostname, _, domain = host.partition('.')
url = f"{scheme}://{username}@{hostname}.{domain}"
url = f"{url}{urlpath}"
print(url)
# This code uses
# the argparse module to parse the command line arguments and
# extract the filename argument. This argument is then used as
# the input config file. The program can be executed with a command
# like python program.py config.ini, where program.py is the name of
# the program file and config.ini is the name of the config file.
# This produces the same output as the previous solution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment