<commit_before>/*********************************** LICENSE **********************************\
 *                            Copyright 2017 Morphux                            *
 *                                                                              *
 *        Licensed under the Apache License, Version 2.0 (the "License");       *
 *        you may not use this file except in compliance with the License.      *
 *                  You may obtain a copy of the License at                     *
 *                                                                              *
 *                 http://www.apache.org/licenses/LICENSE-2.0                   *
 *                                                                              *
 *      Unless required by applicable law or agreed to in writing, software     *
 *       distributed under the License is distributed on an "AS IS" BASIS,      *
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *        See the License for the specific language governing permissions and   *
 *                       limitations under the License.                         *
 \******************************************************************************/

#include <m_args.h>
#include <m_list.h>

#define LIB_OPT_TOKEN_HELP 'h'
#define LIB_OPT_STRING_HELP "help"
#define LIB_OPT_TOKEN_VERSION 'V'
#define LIB_OPT_STRING_VERSION "version"

u32_t read_opt(const int ac, char **av, const mopts_t *opts, mlist_t **args) {
    u32_t       ret = 0, i = 1, it, k;
    u8_t        n_dash;

    if (ac == 0 || av == NULL || opts == NULL || args == NULL)
        return ret;

    for (/* Using u32_t i */ u32_t j = 0; i < (u32_t)ac; i++)
    {

        if (av[i] == NULL || strlen(av[i]) == 0)
            continue ;

        n_dash = 0;
        for (j = 0; av[i][j] != '\0' && av[i][j] == '-'; j++)
            n_dash++;

        /* Single letter option */
        if (n_dash == 1)
        {

            /* If single dash alone, its a parameter */
            if (strlen(av[i]) < 2)
            {
                list_add(*args, av[i], strlen(av[i]) + 1);
                continue ;
            }

            /* Builtins options */
            if (av[i][1] == LIB_OPT_TOKEN_HELP)
                opt_help(opts, 0);
            else if (av[i][1] == LIB_OPT_TOKEN_VERSION)
                p_version(0);

            /* Search the option in the opts array */
            for (u32_t z = 1; av[i][z] != '\0'; z++)
            {
                for (it = 0; !IS_EOL(opts[it]) && opts[it].opt != av[i][z]; it++)
                    ;

                /* Can't find the option */
                if (IS_EOL(opts[it]))
                {
                    m_error("Unknow option -%s\n", &(av[i][z]));
                    opt_help(opts, 1);
                }
                else
                {
                    if (opts[it].take_arg)
                    {
                        if (i + 1 < (u32_t)ac)
                        {
                            if (!opts[it].callback(av[++i]))
                            {
                                m_error("Wrong value \"%s\" for argument -%c\n",
                                    av[i], opts[it].opt);
                                usage(opts);
                                exit(1);
                            }
                            ret++;
                            break ;
                        }
                        else
                        {
                            m_error("Option -%c must take an argument\n",
                                    opts[it].opt);
                            opt_help(opts, 1);
                        }
                    }
                    else
                    {
                        opts[it].callback(NULL);
                        ret++;
                    }
                }
            }
        /* Word option */
        }
        else if (n_dash == 2)
        {
            bool    got_arg = false;

            /* If double dash alone, skip it and then stop reading options */
            if (strlen(av[i]) < 3)
            {<commit_msg>Fix(Args): Add an assertion on callback validity<commit_after>/*********************************** LICENSE **********************************\
 *                            Copyright 2017 Morphux                            *
 *                                                                              *
 *        Licensed under the Apache License, Version 2.0 (the "License");       *
 *        you may not use this file except in compliance with the License.      *
 *                  You may obtain a copy of the License at                     *
 *                                                                              *
 *                 http://www.apache.org/licenses/LICENSE-2.0                   *
 *                                                                              *
 *      Unless required by applicable law or agreed to in writing, software     *
 *       distributed under the License is distributed on an "AS IS" BASIS,      *
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *        See the License for the specific language governing permissions and   *
 *                       limitations under the License.                         *
 \******************************************************************************/

#include <m_args.h>
#include <m_list.h>

#define LIB_OPT_TOKEN_HELP 'h'
#define LIB_OPT_STRING_HELP "help"
#define LIB_OPT_TOKEN_VERSION 'V'
#define LIB_OPT_STRING_VERSION "version"

u32_t read_opt(const int ac, char **av, const mopts_t *opts, mlist_t **args) {
    u32_t       ret = 0, i = 1, it, k;
    u8_t        n_dash;

    if (ac == 0 || av == NULL || opts == NULL || args == NULL)
        return ret;

    for (/* Using u32_t i */ u32_t j = 0; i < (u32_t)ac; i++)
    {

        if (av[i] == NULL || strlen(av[i]) == 0)
            continue ;

        n_dash = 0;
        for (j = 0; av[i][j] != '\0' && av[i][j] == '-'; j++)
            n_dash++;

        /* Single letter option */
        if (n_dash == 1)
        {

            /* If single dash alone, its a parameter */
            if (strlen(av[i]) < 2)
            {
                list_add(*args, av[i], strlen(av[i]) + 1);
                continue ;
            }

            /* Builtins options */
            if (av[i][1] == LIB_OPT_TOKEN_HELP)
                opt_help(opts, 0);
            else if (av[i][1] == LIB_OPT_TOKEN_VERSION)
                p_version(0);

            /* Search the option in the opts array */
            for (u32_t z = 1; av[i][z] != '\0'; z++)
            {
                for (it = 0; !IS_EOL(opts[it]) && opts[it].opt != av[i][z]; it++)
                    ;

                /* Can't find the option */
                if (IS_EOL(opts[it]))
                {
                    m_error("Unknow option -%s\n", &(av[i][z]));
                    opt_help(opts, 1);
                }
                else
                {
                    if (opts[it].take_arg)
                    {
                        if (i + 1 < (u32_t)ac)
                        {
                            if (!opts[it].callback(av[++i]))
                            {
                                m_error("Wrong value \"%s\" for argument -%c\n",
                                    av[i], opts[it].opt);
                                usage(opts);
                                exit(1);
                            }
                            ret++;
                            break ;
                        }
                        else
                        {
                            m_error("Option -%c must take an argument\n",
                                    opts[it].opt);
                            opt_help(opts, 1);
                        }
                    }
                    else
                    {
                        /* Function pointer can't be NULL */
                        assert(opts[it].callback);
                        opts[it].callback(NULL);
                        ret++;
                    }
                }
            }
        /* Word option */
        }
        else if (n_dash == 2)
        {
            bool    got_arg = false;

            /* If double dash alone, skip it and then stop reading options */
            if (strlen(av[i]) < 3)
            {
{'fraction_non_alphanumeric': 0.13841974725404513, 'fraction_numerical': 0.011101925121058226, 'mean_word_length': 0.9141048824593129, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3042674', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2055, 'n_words': 570}