Undesired behaviour of wildcard routes in CLI

33 views Asked by At

I've the following routes in my CLI app:

system
system *
system info
system info *
system settings
system settings *
system tunables
system tunables *
system logging
system logging *
system logging settings
system logging settings *

and want a behaviour like this:

  • if entered system command -> exec system handler
  • if entered system ANYTHING_BUT_CONFIGURED_ROUTES command -> exec system * handler, being ANYTHING_BUT_CONFIGURED_ROUTES anything else but:

    info settings tunables logging

The issue is related app.cmd() execution order:

  • if I register the routes in inverse order than above the handlers are executed properly

  • if I register the routes in the same order than above, if I execute system info, the handler for system * is triggered instead of system info.

Any suggestion on how overcome this?

I'm solving this currently by looping and contructing an inverse array and then looping again, but this takes a lot of time for a simple CLI app and this is cumbersome, so I'm looking for a different solution.

1

There are 1 answers

6
Jason On

Not knowing the modules you use, the problem makes sense, it's matching on first found, not most specific... but you don't have to completely rearrange your list, just put the wildcards after the specific entries, like so:

system
system info
system info *
system settings
system settings *
system tunables
system tunables *
system logging
system logging settings
system logging settings *
system logging *
system *