brentyi / tyro

CLI interfaces & config objects, from types
https://brentyi.github.io/tyro
MIT License
514 stars 27 forks source link

Print dataclass as arguments #72

Closed apoorvkh closed 1 year ago

apoorvkh commented 1 year ago

Hey, thanks for your work on this project -- it seems really elegant and is nice to use so far!

I think it would be pretty cool if there was a function that would print a dataclass as a list of commandline arguments.

For example:

python 01_basics/02_dataclasses.py --field1 hello produces Args(field1='hello', field2=3).

Can we implement a tyro.to_arguments(container) -> str such that tyro.to_arguments(Args(field1='hello', field2=3)) produces --field1 hello --field2 3? Would be really nice to programmatically generate containers and their respective "launch commands" in this way.

Is this already possible? Please let me know what you think! Thanks!

brentyi commented 1 year ago

Hey!

This is something I've also thought about.

The main roadblock is that a fully-featured version of this function would be nontrivial to implement, particularly when all of the intricacies of tyro.conf and subcommands are considered. It'd also be nice to not have the same logic duplicated twice in distant parts of the codebase (for example: whether booleans should be populated as --bool, --no-bool or --bool {True,False}), so if we were to implement something like this the move would probably be to shift toward a system where the lower-level abstractions inside of tyro have some notion of local invertibility, where both the existing str from CLI => Python object and Python object => str from CLI rules are defined in the same place.

I could be convinced otherwise but am not sure we can justify the development/maintenance effort here, but if you want this feature for a restricted subset of what tyro supports as input, building it on a per-project basis doesn't seem too bad.

For programatically generating containers, maybe you can look at the base configs example and see if it could fulfill a similar purpose? In the example we're populating the parser with a dictionary of container instances, which could also be generated programatically.

apoorvkh commented 1 year ago

Thanks for your response! And, sounds good, I will try that and it should be fine on a per-project basis.

brentyi commented 1 year ago

Okay, if you have other issues please let me know!

It's not the most elegant thing in the world but if you're curious about how I'm doing the launch command generation here's how that was handled for our recent ICCV paper:

apoorvkh commented 1 year ago

Great, thanks and enjoy ICCV if you are going!

I did actually encounter one other issue and I made a PR for it in #73. Please let me know!