I'm struggling to get runtime arguments and combine them into a list. I'd like for run-time arguments to be like the following python main.py --all or python main.py --endpoint1 --endpoint2. I only have 2 endpoints to worry about right now, possibly more in the future, so I'd like the solution to consider that possibility.
main.py looks like this at the moment:
import api_vendor import argparse if __name__ == "__main__": parser = argparse.ArgumentParser(description="Pick args") parser.add_argument("--all", help="Get endpoint", action="store_true") parser.add_argument("--endpoint1", help="Get endpoint1", action="store_true") parser.add_argument("--endpoint2", help="Get endpoint2", action="store_true") get_all = os.getenv("GET_ALL") == "YES" or args.all get_endpoint1 = os.getenv("GET_ENDPOINT1") == "YES" or get_all or args.endpoint1 get_endpoint2 = os.getenv("GET_ENDPOINT2") == "YES" or get_all or args.endpoint2 av = api_vendor.API() if get_all = TRUE or endpoint1 = TRUE: av.get_endpoint(endpoint1=TRUE) if get_all = TRUE or endpoint2 = TRUE: av.get_endpoint(endpoint2=TRUE) I don't like this since it's kind of messy and starting to look like spaghetti code. Whichever arguments are set, I'd like to loop through and call get_endpoint something like:
for endpoint in endpoints: av.get_endpoint(endpoint)
vars(args)to convert it to dictionary and it allows to use it withf-stringlikeargs[f"endpoint{x}"]- so it can be used infor-loop. And the same you can do withos.getenv(f"GET_ENDPOINT{x}")But if you plan more values then better use other parameters inargparseto get many values. You could even use--endpoints "1,2"to get it as list of numbers.. Shortly: read doc for argparse--endpoints "1,3:5,7"