brentyi / tyro

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

Support for `nargs="+", action="append"` #46

Closed vwxyzjn closed 1 year ago

vwxyzjn commented 1 year ago

Hello, this is a really cool project. I have wanted to use it to replace argparse in https://github.com/openrlbenchmark/openrlbenchmark.

There is a small issue though, currently we have snippet that looks like

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--filters", nargs="+", action="append", default=[])
args = parser.parse_args()
print(args.filters)
python tyro_test.py \
    --filters '?we=openrlbenchmark&wpn=baselines&ceik=env&cen=exp_name&metric=charts/episodic_return' 'baselines-ppo2-cnn' \
    --filters '?we=openrlbenchmark&wpn=envpool-atari&ceik=env_id&cen=exp_name&metric=charts/avg_episodic_return' 'ppo_atari_envpool_xla_jax_truncation' \
    --filters '?we=openrlbenchmark&wpn=cleanrl&ceik=env_id&cen=exp_name&metric=charts/avg_episodic_return'  'sebulba_ppo_envpool_impala_atari_wrapper?tag=v1.0.0-118-g52e2638' 'cleanba_ppo_envpool_impala_atari_wrapper?tag=v1.0.0-127-g42a800b'
[['?we=openrlbenchmark&wpn=baselines&ceik=env&cen=exp_name&metric=charts/episodic_return', 'baselines-ppo2-cnn'], ['?we=openrlbenchmark&wpn=envpool-atari&ceik=env_id&cen=exp_name&metric=charts/avg_episodic_return', 'ppo_atari_envpool_xla_jax_truncation'], ['?we=openrlbenchmark&wpn=cleanrl&ceik=env_id&cen=exp_name&metric=charts/avg_episodic_return', 'sebulba_ppo_envpool_impala_atari_wrapper?tag=v1.0.0-118-g52e2638', 'cleanba_ppo_envpool_impala_atari_wrapper?tag=v1.0.0-127-g42a800b']]

However, after multiple attempts, I wasn't sure how to do this nargs="+", action="append" with tyro. Is it even possible?

Thanks.

brentyi commented 1 year ago

Thanks for pointing this out @vwxyzjn! This isn't currently possible but I gave a stab at support for it in #47, do you mind checking if it makes sense to you?

brentyi commented 1 year ago

Actually, I see that you need not just action="append" as in the PR, but specifically also nargs="+". Having these in unison isn't done yet, but I think isn't far off... probably we can express this with a Tuple[Tuple[str, ...], ...] annotation. Will revisit.

edit: actually, just did it! in the latest version of the PR annotations like UseAppendAction[Tuple[Tuple[str, ...], ...]] and UseAppendAction[List[List[str]]] should result in the behavior you're describing.