omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
https://jsonargparse.readthedocs.io
MIT License
302 stars 41 forks source link

Add support for `*args` arguments in `CLI` #514

Open rusmux opened 1 month ago

rusmux commented 1 month ago

🚀 Feature request

Add support for *args arguments in CLI

Motivation

Currently I cannot instantiate *args-like arguments. For example:

In main.py

from jsonargparse import CLI

class A:

    def __init__(self, *a: int, b: int) -> None:
        self.a = a
        self.b = b

def main(a: A) -> None:
    print(a.a, a.b)

if __name__ == '__main__':
    CLI(main)

In config.yaml:

a:
  class_path: __main__.A
  init_args:
    a: [1, 2]
    b: 3

In terminal:

python -m main --config config.yaml

Gives error:

error: Parser key "a":
  Problem with given class_path '__main__.A':
    Validation failed: No action for key "a" to check its value.

Pitch

The above code should runs without errors.

Alternatives

mauvilsa commented 1 month ago

Thank you for the proposal! I think about this.