this post was submitted on 06 Sep 2025
110 points (95.1% liked)
Programming
26280 readers
401 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities !webdev@programming.dev
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Counterpoint: Yes, parse don't validate, but CLIs should not be dealing with dependency management.
I love Python's
argparsebecause:FileTypeas a target)curl <URL>notcurl --url <URL>.server --serve --port 8080andserver --reloadwith rules for mix-and-matching those, doserver serve --port 8080andserver reloadwith two separate subparsers.--xml --json, do-f [xml|json].or(pattern of yours IMO should always be replaced by a subparser (which can use inheritance!). As a user the options' data model should be immediately intuitive to me as I look at the--helpand having mutually exclusive flags forces the user to do the extra work of dependency management. Don't doserver --env prod --auth abc --ssl, doserver serve prod --auth abc --sslwhereprodis its own subparser inheriting fromAbstractServeParseror whatever.Thinking of CLI flags as a direct mapping to runtime variables is the fundamental mistake here I think. A CLI should be a mapping to the set(s) of behavior(s) of your application. A good CLI may have mandatory positional arguments but has 0 mandatory flags, 0 mutually exclusive flags, and if it implements multiple separate behaviors should be a tree of subparsers. Any mandatory or mutually exclusive flags should be an immediate warning that you're not being very UNIX-y in your CLI design.
I've used the node.js version of argparse, which as I understand it, is a clone of the python implementation and I've not seen how to do mutually exclusive flags. Mind you, at the time I didn't need them, so it wasn't an issue, but I don't recall seeing any way to do it either.
Did I miss something?
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_mutually_exclusive_group
However I've never had to use that feature. Like I said it can make sense in specific contexts but it is a pretty strong indicator that you have built in a CLI antipattern or too much complexity.