mivade / argparse_dataclass

Declarative CLIs with argparse and dataclasses
MIT License
82 stars 13 forks source link

Is nested dataclass supported? #33

Open sleepwalker2017 opened 2 years ago

sleepwalker2017 commented 2 years ago
from argparse_dataclass import dataclass
from argparse_dataclass import ArgumentParser

@dataclass
class SubOption:
    a: int = 1
    b: int = 2

@dataclass
class Options:
    x: int = 42
    y: bool = False
    sub: SubOption = None

parser = ArgumentParser(Options)
x = parser.parse_args(['--sub', '1'])
print(x)

for example, how can I parse args to get a value for sub?

mivade commented 2 years ago

This example doesn't make much sense to me. What attribute of SubOption should be getting set to 1?

sleepwalker2017 commented 2 years ago

This example doesn't make much sense to me. What attribute of SubOption should be getting set to 1?

I mean, if I have a nested dataclass for example Options, how can I set the values in SubOptions.

python test.py --sub.a 2 --sub.b 1 Seems this usage is not supported. How can I set the value of sub.a?

sleepwalker2017 commented 2 years ago

This example doesn't make much sense to me. What attribute of SubOption should be getting set to 1?

Let me say it more clearly, in the following example, how can I set 'logging' to False? image