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 :(

  • Raikin@feddit.de
    link
    fedilink
    arrow-up
    6
    ·
    1 year ago

    What module are you currently using? I think the built-in module argparse should solve your problem but maybe I’m misunderstanding what you’re asking for.

    • bloopernova@programming.devOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      I already have the arguments parsed, the bit that I’m confused about is where I decide what to do based on those various combinations of arguments.

      I’m using Traitlets.

    • bloopernova@programming.devOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      I was doing it wrong. I was trying to do appname thing --action when I should have been doing appname action --thing. Thank you for commenting, I’m sorry to waste your time.

    • bloopernova@programming.devOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      Traitlets. It parses the arguments for me just fine, I’m just confused on how to turn those into calls to methods without a mess of if/else or switch statements, which seem fragile.