bw2 / ConfigArgParse

A drop-in replacement for argparse that allows options to also be set via config files and/or environment variables.
MIT License
728 stars 121 forks source link

parse_known_args in combination with is_config_file works incorrectly #152

Open johann-petrak opened 5 years ago

johann-petrak commented 5 years ago

It seems that the way how an is_config_file option is processed does not honour the allow_abbrev setting when parsing allowing unknown args and instead uses (and splits?) an unknown option that is a prefix of a known option.

Here is example code:

    import configargparse
    p = configargparse.ArgParser(
        add_config_file_help=False,
        add_env_var_help=False,
        auto_env_var_prefix="ENV",
        ignore_unknown_config_file_keys=False,
        default_config_files=[],
        description=None,
        usage=None,
        add_help=False,
        allow_abbrev=False,
        prefix_chars='-')
    #  p.add("--comp1.cfile")
    p.add("--comp1.cfile", is_config_file_arg=True)
    args = ["--comp", "2"]
    ns, unkn = p.parse_known_args(args)
    print("NS=",ns,"UNKN=",unkn)

This will terminate in the parse_known_args(args) call showing the usage and the error message "error: File not found: 2"

However, if the same option is used without the is_config_file_arg=True setting, everthing works as expected and the "--comp" option is correctly ignored.

johann-petrak commented 5 years ago

This happens because in line 725, new instances of argparse.ArgumentParser are created without passing on the allow_abbrev setting, so this setting is set to the default of True.