ConfigArgParse icon indicating copy to clipboard operation
ConfigArgParse copied to clipboard

How to parse default arguments from config file

Open 1vash opened this issue 6 years ago • 2 comments

main.py

parser = configargparse.ArgParser(config_file_parser_class=configargparse.YAMLConfigFileParser)
parser.add('-c', '--my-config', required=True, is_config_file=True, help='config file path')
args = parser.parse_args()

args_conf.yaml

input: True

I execute main as python main.py ( and I want to execute it without any arguments, I want parse them as default ones)

The result is:

main.py: error: unrecognized arguments: --input True

At the end, I want just to use args["input"] to acceess to this flag. But because of the error described below I cannot do it. My aim is just to parse all flags from the config file instead of specifying argparse inside script/s to remove code complexity

The error is simple say us that argument that ConfigArgParse parsed from .yaml have not passed in CLI. And there are some lifehacks to get unrecognized arguments as list and then look into them but I want t have it simple without any manipulations :)

I'd like to have recognized arguments as well I would pass them via CLI but don't do it manually. My application has lots of flags and I don't intend to pass all arguments to CLI. I want to pass only required and specific flags that I want to trigger. By the way, please give some example what should I do to add required arguments as well not only default arguments in .yaml to be correctly paraser by parser.parse_args(). The library is great but there are not enough examples to feel free using it

P.S I searched for documentation and tried to use [DEFAULT] brackets as well as change config_file_parser_class=configargparse.DefaultConfigFileParser to parse from .ini but didn't succeed either.

1vash avatar Feb 20 '19 12:02 1vash

Hi, I'm not sure I have understood everything but it seems to me there are multiple matters to answer here :

  • Fact that configargparse gives you an error when you try to parse unknown arg is the normal behavior. It is the goal of the library itself. If you want to handle unknown arguments : you can use parse_known_args() and merge results of known and unknown dicts if you want to. The goal of the lib is to validate/cast parameters/config so if you want another usage, that seems like out of the scope to me.
  • use parser.add('-f', '--foo', required=True, default="blah") to set a default and required value to something.
  • you can use configargparse.ArgParser(..., use default_config_files=['./default_conf.yam', '/etc/another_conf.yaml'], ...) to define default config files that would be parsed before to set default conf values that you can override by command line. I guess this should behave like you expect, using a parse_known_args() call.
  • in the end, if you want to read data from a yaml file, just use a yaml parser. You could read from a yaml default file with default values (2 lines of code) and get the result in a dict. You can also merge that with the things you parse from command line.

Does that help in any way? Cheers, Ronan

ronhanson avatar Feb 20 '19 16:02 ronhanson

@ronhanson Thanks a lot for the explanation but it didn't help me find the answer. I want to have 1 config file and load from it to each module (only module specific flags). For example, for visualization module I want to parse all visualization flags from the config/txt/yaml file etc. By the way, I want to specify in this config file "default" parameters and "required" argument flags and write "help" for them. It would be nice to parse config file instead of .py (as it's done right now using argparse). In other words, It would be nice to use parser.add('-f', '--foo', required=True, default="blah") inside the config file and not in .py. Something like [VISUALIZATION] foo: default 5 /n bar: required But I cannot find how can I use config file and specify what I want inside them using your library, maybe you can help me to do this.

Thanks a lot Ivan

1vash avatar Feb 21 '19 10:02 1vash