[Python] What would be a suitable way to receive run-time arguments and combine them into an...

Discussão em 'Python' iniciado por Stack, Agosto 28, 2025.

  1. Stack

    Stack Membro Participativo

    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)

    Continue reading...

Compartilhe esta Página