pydantic-cli icon indicating copy to clipboard operation
pydantic-cli copied to clipboard

Cli support nested params

Open y805939188 opened this issue 2 years ago • 2 comments

Hi. In my scenario, I has a nested model of pydantic:


class B(BaseModel):
  name: str

class A(BaseModel):
  b: B
  
class Wrapper(BaseModel):
  class Config(DefaultConfig):
    CLI_JSON_ENABLE = True
  a: A

I can pass the args from json file by "--json-config":

{
  a: {
    b: {
      "name": "test"
    }
  }
}

But I won't pass a nested args in command line likemy-cli --a.b.name test

I want to ask that is it possible to support this feature?

Thank you very much.

y805939188 avatar Jan 30 '23 11:01 y805939188

The lack of nested model support is a core design decision.

https://github.com/mpkocher/pydantic-cli#limitations

I think you might be better suited by a more dictionary munging centric library.

Perhaps a library that has an interface like this that does some minimal json munging/loading and then passes the raw dict to pydantic for validation.

my-tool -o a.b.name=test --option a.b.max=10 --json-config=my-config.json

This is probably easily doable with a minimal amount of code with a thin layer around argparse.

However, it's not something that I'm interested in supporting in pydantic-cli.

Thanks for your feedback.

Here's a list of other projects that might be more aligned with your project's requirements.

https://github.com/mpkocher/pydantic-cli#other-related-tools

Best of luck to you on your future projects.

mpkocher avatar Feb 03 '23 06:02 mpkocher

The lack of nested model support is a core design decision.

https://github.com/mpkocher/pydantic-cli#limitations

I think you might be better suited by a more dictionary munging centric library.

Perhaps a library that has an interface like this that does some minimal json munging/loading and then passes the raw dict to pydantic for validation.

my-tool -o a.b.name=test --option a.b.max=10 --json-config=my-config.json

This is probably easily doable with a minimal amount of code with a thin layer around argparse.

However, it's not something that I'm interested in supporting in pydantic-cli.

Thanks for your feedback.

Here's a list of other projects that might be more aligned with your project's requirements.

https://github.com/mpkocher/pydantic-cli#other-related-tools

Best of luck to you on your future projects.

Thank your advice very much. I would consider that modify my code. pydantic-cli is a fantastic library 👍🏻.

y805939188 avatar Feb 03 '23 08:02 y805939188

Hi. In my scenario, I has a nested model of pydantic:

class B(BaseModel):
  name: str

class A(BaseModel):
  b: B
  
class Wrapper(BaseModel):
  class Config(DefaultConfig):
    CLI_JSON_ENABLE = True
  a: A

I can pass the args from json file by "--json-config":

{
  a: {
    b: {
      "name": "test"
    }
  }
}

But I won't pass a nested args in command line likemy-cli --a.b.name test

I want to ask that is it possible to support this feature?

Thank you very much.

Pydantic Settings has support for this and might be more aligned with your requirements.

https://docs.pydantic.dev/latest/concepts/pydantic_settings/#command-line-support

Thanks for your feedback.

mpkocher avatar Jul 22 '24 22:07 mpkocher