From 8b07320f76c6ae587decd2220c2b1e2823e5fd2e Mon Sep 17 00:00:00 2001 From: luca0N! Date: Thu, 9 Jun 2022 22:07:08 -0300 Subject: [PATCH] Improve command line parsing Improve command line argument parser. This will make it easier to add code to read command line options (which previously weren't available). --- src/main.cxx | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main.cxx b/src/main.cxx index 5882f46..c74cb82 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -47,26 +47,26 @@ void printIntro() { int main(int argc, char *argv[]) { printIntro(); - std::string path; + std::string path = ""; // Parse cmdline options - switch (argc) { - case 1: - path = "."; - std::cout << "Assuming working directory is \".\"" - << std::endl; - break; - case 2: - path = argv[1]; - std::cout << "Using specified directory: " - << path << std::endl; - break; - default: + for (int argi = 1; argi < argc; argi++) { + if (argv[argi][0] == '-') { + // This is a cmdline option (starts with a dash). + + } else if (path == "") { + path = argv[argi]; + } else { + std::cerr << "error: website directory was already " << + "provided (`" << path << "'), refusing to continue with `" << + argv[argi] << "'.\n"; printUsage(argv[0]); exit(RETURN_FAILED_INVALID_SYNTAX); - break; + } } + std::cout << "Using specified directory: " << path << "\n"; + SwgContext ctx; parse_config(&ctx, path);