Skip to content

Commit

Permalink
reroll main
Browse files Browse the repository at this point in the history
  • Loading branch information
mtygesen committed Apr 15, 2024
1 parent cb9a437 commit a49e9e9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ project(dpl)
enable_testing()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CMAKE_CXX_STANDARD 20)

# Can not be upgraded to C++20 because of a bug in github actions with chrono header
set(CMAKE_CXX_STANDARD 17)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
Expand Down
25 changes: 11 additions & 14 deletions Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ using namespace antlr4;
using namespace dplgrammar;

int main(int argc, char **argv) {
// Put args in span for safe handling
std::span<char *> args = std::span(argv, size_t(argc));

std::string description = "DPL Interpreter";
std::string version = "0.0.1";
std::string author = "P4-ACMMMRW";
std::string usage = std::string(args[0]) + " [options] <input file> [arguments]";
std::string usage = std::string(argv[0]) + " [options] <input file> [arguments]";
argz::about about{description, version, author, usage};
about.print_help_when_no_options = false;

Expand All @@ -25,16 +22,16 @@ int main(int argc, char **argv) {
int fileArgIndex = 0;
bool isOption = true;
for (int i = 1; i < argc; ++i) {
isOption = std::string(args[i]) == "-h" || std::string(args[i]) == "--help" ||
std::string(args[i]) == "-v" || std::string(args[i]) == "--version";
isOption = std::strcmp(argv[i], "-h") == 0 || std::strcmp(argv[i], "--help") == 0 ||
std::strcmp(argv[i], "-v") == 0 || std::strcmp(argv[i], "--version") == 0;

if (!isOption) {
fileArgIndex = i;
break;
}
}

// Parse all arguments except fileArgIndex and args[0]
// Parse all arguments except fileArgIndex and argv[0]
try {
argz::parse(about, options, argc, argv, fileArgIndex);
} catch (const std::exception &e) {
Expand All @@ -49,22 +46,22 @@ int main(int argc, char **argv) {
}

// If after options comes an argument write error
if (args[fileArgIndex][0] == '-') {
std::cerr << "Error: expected input file but received argument \"" << args[fileArgIndex]
if (argv[fileArgIndex][0] == '-') {
std::cerr << "Error: expected input file but received argument \"" << argv[fileArgIndex]
<< "\"\n";
std::cout << "Usage: " << usage << '\n';
return EXIT_FAILURE;
}

// Check if input file exists
if (!std::filesystem::exists(args[fileArgIndex])) {
std::cerr << "Error: file \"" << args[fileArgIndex] << "\" not found\n";
if (!std::filesystem::exists(argv[fileArgIndex])) {
std::cerr << "Error: file \"" << argv[fileArgIndex] << "\" not found\n";
return EXIT_FAILURE;
}

std::ifstream file{std::filesystem::path(args[fileArgIndex])};
std::ifstream file{std::filesystem::path(argv[fileArgIndex])};
if (!file.is_open()) {
std::cerr << "Error: file \"" << args[fileArgIndex] << "\" could not be opened\n";
std::cerr << "Error: file \"" << argv[fileArgIndex] << "\" could not be opened\n";
return EXIT_FAILURE;
}

Expand All @@ -86,4 +83,4 @@ int main(int argc, char **argv) {
}

return EXIT_SUCCESS;
}
}
1 change: 0 additions & 1 deletion include/Main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
#include <fstream>
#include <iostream>
#include <regex>
#include <span>

#endif
2 changes: 1 addition & 1 deletion linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ for file in $(find . -name '*.cpp' -o -name '*.hpp' | grep -v -e './lib' -e './b
continue
fi

clang-tidy -p build -checks='boost-*,bugprone-*,performance-*,readability-*,portability-*,clang-analyzer-*,cppcoreguidelines-*' -fix -extra-arg=-std=c++20 $file
clang-tidy -p build -checks='boost-*,bugprone-*,performance-*,readability-*,portability-*,clang-analyzer-*,cppcoreguidelines-*' -fix -extra-arg=-std=c++17 $file
echo ""
done

0 comments on commit a49e9e9

Please sign in to comment.