#!/usr/bin/env python3

import logging
import os
import configparser
import argparse

log = logging.getLogger(__name__)


class Config:
    config_files = ["config.ini"]
    defaults = {}

    def __init__(self):
        self.cfg = configparser.ConfigParser(self.defaults)
        self.cfg.read(self.config_files)

    def parse_args(self, args=None):
        """Parse command-line arguments"""

        parser = argparse.ArgumentParser("monprog.py")
        parser.add_argument(
            "-v", "--verbose", help="Enable verbose output", action="store_true"
        )
        parser.set_defaults(func=(lambda _: parser.print_help()))
        # ajouter des arguments en ligne de commande ici

        self.args = parser.parse_args(args)
        logging.basicConfig(level=logging.DEBUG if self.args.verbose else logging.INFO)
        log.debug("Verbose mode enabled")

    def __getitem__(self, name: str):
        """Implements [] operator for reading values"""
        try:
            return getattr(self.args, name)
        except NameError:
            return os.environ.get(
                name.upper(), self.cfg[self.cfg.default_section][name]
            )

    def run(self):
        """Runs the configured action of the parser"""
        return self.args.func(self)


if __name__ == "__main__":
    c = Config()
    c.parse_args()
    c.run()