<commit_before>import sys

import trafaret as T
from trafaret_config import read_and_validate, ConfigError


def load_config(fname):
    config = load_and_validate(fname)
    return config


def load_and_validate(fname):
    primitive_ip_regexp = r'^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'

    TRAFARET = T.Dict({
        T.Key('postgres'):
            T.Dict({
                'database': T.String(),
                'user': T.String(),
                'password': T.String(),
                'host': T.String(),
                'port': T.Int(),
                'minsize': T.Int(),
                'maxsize': T.Int(),
            }),
        T.Key('host'): T.String(regex=primitive_ip_regexp),
        T.Key('port'): T.Int(),
    })

    try:
        config = read_and_validate(fname, TRAFARET)
    except ConfigError as e:
        e.output()
        sys.exit(1)
    except FileNotFoundError as e:
        print(type(e).__name__, e)
        sys.exit(1)

    return config<commit_msg>Update load_config() to use argparse options (#1462)<commit_after>import sys
import argparse

import trafaret as T
from trafaret_config import commandline


def load_config(fname):
    primitive_ip_regexp = r'^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'

    TRAFARET = T.Dict({
        T.Key('postgres'):
            T.Dict({
                'database': T.String(),
                'user': T.String(),
                'password': T.String(),
                'host': T.String(),
                'port': T.Int(),
                'minsize': T.Int(),
                'maxsize': T.Int(),
            }),
        T.Key('host'): T.String(regex=primitive_ip_regexp),
        T.Key('port'): T.Int(),
    })

    ap = argparse.ArgumentParser()
    commandline.standard_argparse_options(ap, default_config=fname)
    #
    # define your command-line arguments here
    #
    options = ap.parse_args()

    try:
        config = commandline.config_from_options(options, TRAFARET)
    except FileNotFoundError as e:
        print(type(e).__name__, e)
        sys.exit(1)

    return config
{'fraction_non_alphanumeric': 0.1565510597302505, 'fraction_numerical': 0.01878612716763006, 'mean_word_length': 2.023289665211063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1352084', 'n_tokens_mistral': 711, 'n_tokens_neox': 682, 'n_words': 129}