added option -h and -v

This commit is contained in:
2021-03-10 23:29:49 +01:00
parent 11ad7fc3bc
commit 55d39ef321
4 changed files with 39 additions and 4 deletions

27
ml.cpp
View File

@@ -1773,7 +1773,20 @@ MlValue MlEnvironment::get(const std::string &name) const {
throw MlError(MlValue::atom(name), *this, ATOM_NOT_DEFINED);
}
int main(int argc, const char **argv) {
std::vector<std::string> getCmdOption(char *argv[], int argc, const std::string &option) {
std::vector<std::string> tokens;
for (int i = 1; i < argc; ++i) {
if (option == argv[i] && i + 1 < argc) {
i++;
tokens.push_back(std::string(argv[i]));
}
}
return tokens;
}
bool cmdOptionExists(char **begin, char **end, const std::string &option) { return std::find(begin, end, option) != end; }
int main(int argc, char *argv[]) {
MlEnvironment env;
std::vector<MlValue> args;
for (int i = 0; i < argc; i++)
@@ -1786,6 +1799,18 @@ int main(int argc, const char **argv) {
// for xcode profiling
// run(read_file_contents("/Users/vaclavt/Development/mlisp/tests/test.lsp"), env);
// help
if (cmdOptionExists(argv, argv + argc, "-h")) {
std::cout << "Usage:\n\t-h print this help\n\t-f source_file - executes code in file\n\t-c code - runs passed code\n\t-i runs repl\n\t-v prints version string\n\n";
return 0;
}
// version
if (cmdOptionExists(argv, argv + argc, "-v")) {
std::cout << VERSION << std::endl;
return 0;
}
if (argc == 1 || (argc == 2 && std::string(argv[1]) == "-i"))
repl(env);
else if (argc == 3 && std::string(argv[1]) == "-c")