Tank shell registered command and argument parsing

So we have an app that we’ve registered with the tk-shell engine so we can invoke it from the command line. I’m trying to figure out the best way to expose optional arguments to this command so we can call it like so:

./tank <optional_context> my_app -optionalflag1 foo -optionalflag2 bar

right now it appears that every token after ‘my_app’ gets passed as an argument to the callback function registered via sgtk.platform.Engine.register_command(), which means it’s beholden to the number of arguments that the callback function accepts. I’ve considered encapsulating all optional arguments in quotes like so:

./tank <optional_context> my_app “-optionalflag1 foo -optionalflag2 bar”

But this feels inelegant. I’d imagine there’s a very simple way to accomplish specifying additional arguments but for the life of me I don’t see it documented.

1 Like

Hey Nico! Not sure of a solution here, but I’ve run it past the engineers. I’ll let you know if we come up with anything clever.

1 Like

OK! So, I got a little more info from @Jeff_Beeland, and unfortunately we don’t have a good solution here. But you have a couple options.

This is the code that handles the commandline args – you can see that it’s pretty minimal.

So, you could fork/hack tk-shell to do what you want, or you can write a wrapper that has a nice command line UX, but then parses/repacks its inputs to send on to tank in a way that the callback accepts.

In the meantime, we’ll run it by @rob as well – seems like a totally reasonable feature request.

1 Like

Thanks for the feedback @tannaz and @Jeff_Beeland . The wrapper was my first inclination as well, but I decided to deep-dive into tk-multi-launchapp to see if there was another way of doing it (since presumably all the DCC’s being launched also take a variable number of arguments). Turns out the way they resolve this is to construct the callback function to use the *args variable length argument list:

def my_callback(*args, **kwargs)

It seems this happily allows me to pass any arbitrary tokens after my invocation and they all get passed on to my callback function.

Cheers

2 Likes

of course! *args! great solution.

1 Like