<commit_before>#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <boost/utility/string_ref.hpp>
#include "../ast/ast_printer.h"
#include "../ast/decl.h"
#include "../parser/parser.hpp"

extern FILE* yyin;
int yyparse();
extern std::vector<Decl> globalAST;

int main(int argc, char** argv) {
    if (argc == 1) {
        std::cout << "error: no input files" << std::endl;
        return 1;
    }

    std::vector<boost::string_ref> args(argv + 1, argv + argc);

    const auto it = std::find(args.begin(), args.end(), "-print-ast");
    const bool printAST = it != args.end();
    if (printAST) args.erase(it);

    for (boost::string_ref filePath : args) {
        yyin = fopen(filePath.data(), "rb");

        if (!yyin) {
            std::cout << "error: no such file: '"  << filePath << "'" << std::endl;
            return 1;
        }

        int result = yyparse();
        if (result != 0) return result;

        if (printAST) {
            std::cout << globalAST;
        }
        globalAST.clear();
    }
}
<commit_msg>refactor command-line argument checking to checkFlag() function<commit_after>#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <boost/utility/string_ref.hpp>
#include "../ast/ast_printer.h"
#include "../ast/decl.h"
#include "../parser/parser.hpp"

extern FILE* yyin;
int yyparse();
extern std::vector<Decl> globalAST;

/// If `args` contains `flag`, removes it and returns true, otherwise returns false.
bool checkFlag(boost::string_ref flag, std::vector<boost::string_ref>& args) {
    const auto it = std::find(args.begin(), args.end(), flag);
    const bool contains = it != args.end();
    if (contains) args.erase(it);
    return contains;
}

int main(int argc, char** argv) {
    if (argc == 1) {
        std::cout << "error: no input files" << std::endl;
        return 1;
    }

    std::vector<boost::string_ref> args(argv + 1, argv + argc);
    const bool printAST = checkFlag("-print-ast", args);

    for (boost::string_ref filePath : args) {
        yyin = fopen(filePath.data(), "rb");

        if (!yyin) {
            std::cout << "error: no such file: '"  << filePath << "'" << std::endl;
            return 1;
        }

        int result = yyparse();
        if (result != 0) return result;

        if (printAST) {
            std::cout << globalAST;
        }
        globalAST.clear();
    }
}

{'fraction_non_alphanumeric': 0.1706611570247934, 'fraction_numerical': 0.004132231404958678, 'mean_word_length': 2.71319018404908, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 18, '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': '5731012', 'n_tokens_mistral': 802, 'n_tokens_neox': 784, 'n_words': 210}