lundi 15 mars 2021

Best way to handle if-else with argparse?

Say we have a lot of options for argument parsing

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Download files from Canvas.')

    parser.add_argument(
        '-user_id',
        type=str,
        default="",
    )

    parser.add_argument(
        '-course_id',
        type=str,
        default="",
    )

    parser.add_argument(
        '-student_id',
        type=str,
        default="",
    )
# ...
    parser.add_argument(
        '-book_id',
        type=str,
        default="",
    )

And I want to run functions determined by the inputs

if user_id != "" and course_id == "" and student_id != "" and book_id == "":
    f()

elif user_id != "" and course_id == "" and student_id != "" and book_id != "":
    g()

# ...

elif user_id == "" and course_id == "" and student_id == "" and book_id == "":
    h()

and so on. Well, these are a lot of if-else statements. I want to eliminate having to do all of that. Does anyone have sufficient ways to reduce the amount of comparisons among variables before a function is run? Or does it depend on context? Thank you!!

Aucun commentaire:

Enregistrer un commentaire