Skip to content

Commit f236181

Browse files
committed
add support for config parsing
1 parent 67f1253 commit f236181

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

dev_docs/myscript.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
from argparse import ArgumentParser
2+
from configparser import ConfigParser
23

34
import requests
45
from requests.auth import HTTPBasicAuth
56

67
parser = ArgumentParser()
7-
parser.add_argument("host", help="The host to connect to")
8-
parser.add_argument("username", help="The username for the user")
9-
parser.add_argument("password", help="The password for the user")
8+
parser.add_argument("--host", default=None, help="The host to connect to")
9+
parser.add_argument("--username", default=None, help="The username for the user")
10+
parser.add_argument("--password", default=None, help="The password for the user")
1011
args = parser.parse_args()
1112

12-
host = args.host
13-
username = args.username
14-
password = args.password
13+
config = ConfigParser()
14+
config.read("config.ini")
15+
host = config["DEFAULT"]["host"]
16+
username = config["DEFAULT"]["username"]
17+
password = config["DEFAULT"]["password"]
18+
19+
host = args.host if args.host else host
20+
username = args.username if args.username else username
21+
password = args.password if args.password else password
1522

1623
response = requests.get(
17-
f"https://{host}/api/v1/data",
18-
auth=HTTPBasicAuth(username, password)
24+
f"https://{host}/api/v1/data", auth=HTTPBasicAuth(username, password)
1925
)
2026
mydata = response.json()["the-data-i-want"]
2127

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy