omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables

Home Page:https://jsonargparse.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flattening `nested_key`

idelchi opened this issue Β· comments

πŸš€ Feature request

Is there any way to pass a complete configuration as a dataclass to jsonargparse, without having one nested level of nested_key?

Small example:

@dataclass
class Address:
    street: str = ""
    city: str = ""
    state: str = ""
    zip: str = ""


@dataclass
class AppConfiguration:
    address: Address
    name: str = ""



parser = jsonargparse.ArgumentParser()

parser.add_argument("--config", action=jsonargparse.ActionConfigFile)
parser.add_dataclass_arguments(AppConfiguration, "app")

output is:

  --app.name NAME
  --app.address
  --app.address.street 
  ...

Motivation

I'm not sure if I have missed some configuration option or other way of achieving this, but would be convenient to pass one complete class and have a configuration as:

  --name NAME
  --address
  --address.street 
  ...

Pitch

Alternatives

Adding each dataclass separately using add_argument("--address", type=Address....)

I'm not sure if I have missed some configuration option or other way of achieving this, but would be convenient to pass one complete class and have a configuration as:

This is already possible. You will get what you want if instead of add_dataclass_arguments you do:

parser.add_class_arguments(AppConfiguration)

That's perfect - thank you so much for the quick answer and great library!! πŸ˜ƒ