I want to be able to call this script with a single id, a list of ids, a url, or a csv file.

Is there a simple way to express it? Here I’ve only added options to call the script with nothing, a single id or a list of ids. But it already looks too messy for my taste.

def parse_args():
    try:
        if len(sys.argv) == 3:
            # List
            if sys.argv[2].startswith("[") and sys.argv[2].endswith("]"):
                work_ids = sys.argv[2].strip("[").strip("]").split(",")
                download_ao3_fics_and_import_tags(work_ids)
            else: # single work_id
                download_ao3_fic_and_import_tags(sys.argv[2])
        elif len(sys.argv) != 3:
            main()
        else:
            usage()
    except Exception:
        usage()
  • Arthur Besse@lemmy.ml
    link
    fedilink
    arrow-up
    3
    ·
    2 years ago

    i highly recommend using click for all of your argument parsing needs.

    click makes it easy to build very nice commandline interfaces.

    You can install it on debian/ubuntu with apt install python3-click and it is also packaged in many other distributions, making it a very reasonable dependency to add to any program. Aside from that missing piece of information (and a silly recommendation to install it via pip), its quickstart is good and the place to start.