python-fire icon indicating copy to clipboard operation
python-fire copied to clipboard

feature request: a way to support nested dict input

Open link89 opened this issue 3 years ago • 1 comments

Background

Hi, I just come into a case that need to allow user to override some default value of a json configuration. The configuration is a nested dict and so is the input. I didn't find fire has good support for such case so here is a proposal.

Feature

Take this as an example

def my_command(config: dict):
  ...

I think fire may support such input to build the actual value of config

my_command --config.pos.x=20 --config.pos.y=10
# or
my_command --config.pos='{"x": 20, "y":10}'
# or
my_command --config='{"pos": {"x": 20, "y":10}}'

And both will end up with the following dict

{
  "pos": { "x": 20, "y": 10}
}

The beauty of this design can avoid a lot of unecessary " and {} (and you need to pair them correctly) input of a JSON data. It's less error prone and comforable to use this style in command line. Currently I can handle this via kwargs, but I think it would be nice to have native support.

link89 avatar Aug 19 '22 07:08 link89

Thanks for this suggestion. This is a feature we might consider for the future, but it isn't currently planned. In the mean time, you may be interested in the ml_collections library. See in particular config_flags: https://ml-collections.readthedocs.io/en/latest/config_flags.html

dbieber avatar Sep 13 '22 17:09 dbieber