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

Encoding with utf-8 #219

Open mastrolube opened 3 years ago

mastrolube commented 3 years ago

Hi there, I have a problem reading a config file. Maybe I'm dumb but I can't find the solution. When opening a config file how can I specify to use utf-8 encoding instead of cp1252? Some people have trouble with not Latin characters and I found out that problem occurs inside configargparse.py/ArgumentParser/parse_known_args.. Maybe I've something to specify in the head of the config file? Any suggestions?

bw2 commented 3 years ago

It's currently somewhat cumbersome but you should be able to do it with

def file_open(path, mode="rt"):
     return open(path, mode, encoding='UTF-8')

import configargparse
p = configargparse.ArgumentParser(config_file_open_func=file_open)
...
bw2 commented 3 years ago

The default encoding is platform dependent. I wonder if we should explicitly set the encoding to 'UTF-8'. If anyone has a strong opinion on this, let me know.

mastrolube commented 3 years ago

Hello! Thanks for your answer! Yes, I made something like that a week ago, and works well :)

configargparse.ArgumentParser(
            config_file_open_func=lambda filename: open(
                filename, "r+", encoding="utf-8"
            )

An Arabian windows user of our bot was unable to use config files because of that, my suggestion is to specify utf-8 as the default encoding. I can't see any downside to that.

I also find strange that windows has cp1252 as default .. 😖