26
submitted 1 year ago* (last edited 1 year ago) by bloopernova@programming.dev to c/programming@programming.dev

This is solved, I was being dumb. Please see second EDIT below

This isn't really language specific, but if it helps I'm using Python. I can get the parameters just fine with the Traitlets module, but I'm still a novice really and figuring out which patterns to use is challenging.

Say you have a bunch of command line parameters. Some are booleans, where their presence means True, absence means False. Other parameters must accept one text string, and others can be used multiple times to build a list of strings.

It feels inefficient/wrong to use a bunch of IF/THEN/ELSE statements to decide what to do with the parameters, and prone to edge case errors. Is there a pattern that would invoke the correct method based on the combination of input parameters?

Examples:

app thing --dry-run --create --name=newname01 --name=newname02 --verbose

app thing --create --name=newname01 --name=newname02 --name=newname03

app job --cancel -i 01 -i 02 -i 03

EDIT: Via the Traitlets module, I get those arguments parsed and accessible as self.argname, so getting them into my app is working great. It's just deciding what to do with them that is confusing me.

Thank you, sorry for my noobness.

EDIT2: I think I understand where I'm going wrong:

I'm creating subcommands based on objects, not actions. i.e. appname thing --action should really be appname action --thing. Once things are divided up into actions, assigning things to those actions will be much, much easier and straightforward.

Sorry for a confusing and fairly pointless post :(

you are viewing a single comment's thread
view the rest of the comments
[-] nickwitha_k@lemmy.sdf.org 3 points 1 year ago

You're very welcome! I've spent a lot of time with Python and really think that argparse as of 3.x makes most non-stdlib libraries for parsing are unnecessary. You get a ton of functionally and ability to add end-user documentation as you go, while abstracting away some of the basics like type casting/checking

The addition of Match-Case, while not adding much, functionally, does a LOT for readability and keeping logic clear.

[-] bloopernova@programming.dev 3 points 1 year ago

I'm so annoyed with myself for using Traitlets for command line argument parsing! Your solution using argparse has so many more useful options, like the ability to define a mutually exclusive group of arguments.

Sigh. I live and learn and code a bunch more lol.

[-] nickwitha_k@lemmy.sdf.org 2 points 1 year ago

No reason to be annoyed with yourself. It's part of the process of learning. In starting with Traitlets, you tried something new to you and between that and refactoring to use argparse, you've given yourself more practice writing code and learned a bit more about available libraries. And, at the same time, you've worked through the logic of your CLI design, building a better understanding of ways to organize arguments.

this post was submitted on 23 Aug 2023
26 points (93.3% liked)

Programming

17314 readers
106 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 1 year ago
MODERATORS