From 1736b785dda2ba5b8a81ac0db339c36b579f7f0a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 10 Dec 2017 14:53:06 +0100 Subject: Change program arguments to use current path as project path Option "-p" can be passed to specify another project path --- include/FileUtil.hpp | 1 + src/FileUtil.cpp | 13 +++++++ src/main.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 97 insertions(+), 12 deletions(-) diff --git a/include/FileUtil.hpp b/include/FileUtil.hpp index 5a0bbc3..8407a36 100644 --- a/include/FileUtil.hpp +++ b/include/FileUtil.hpp @@ -24,6 +24,7 @@ namespace sibs Result getFileContent(const char *filepath); bool fileOverwrite(const char *filepath, StringView data); const char* getHomeDir(); + Result getCwd(); } #endif //SIBS_FILEUTIL_HPP diff --git a/src/FileUtil.cpp b/src/FileUtil.cpp index 400b439..d815ebf 100644 --- a/src/FileUtil.cpp +++ b/src/FileUtil.cpp @@ -131,4 +131,17 @@ namespace sibs } return homeDir; } + + Result getCwd() + { + string cwd; + cwd.reserve(PATH_MAX); + if(getcwd(&cwd[0], PATH_MAX) != 0) + { + if(cwd.empty()) cwd = "."; + return Result::Ok(cwd); + } + + return Result::Err(strerror(errno)); + } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e3f927f..151e5a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,13 +11,13 @@ using namespace sibs; void usage() { - printf("Simple build system for native languages\n"); - printf("usage:\n"); - printf(" sibs \n"); - printf("option:\n"); - printf(" project path: Path containing project.conf\n"); - printf("examples:\n"); - printf(" sibs myProject\n"); + printf("usage: sibs COMMAND\n\n"); + printf("Simple Build System for Native Languages\n\n"); + printf("Options:\n"); + printf(" -p\t\tPath to project (directory that contains main project.conf file)\n"); + printf(" -h\t\tPrint this help menu\n\n"); + printf("Commands:\n"); + printf(" build\t\tBuild a project that contains a project.conf file\n"); exit(1); } @@ -66,12 +66,8 @@ bool isSourceFile(tinydir_file *file) return false; } -int main(int argc, const char **argv) +void build(string projectPath) { - if(argc != 2) - usage(); - - string projectPath = argv[1]; validateDirectoryPath(projectPath.c_str()); if(projectPath.back() != '/') projectPath += "/"; @@ -124,6 +120,81 @@ int main(int argc, const char **argv) { exit(8); } +} + +int main(int argc, const char **argv) +{ + string projectPath; + string command; + for(int i = 1; i < argc; ++i) + { + const char *arg = argv[i]; + if(arg[0] == '-') + { + // Option + if(strcmp(arg, "-p") == 0) + { + if(!projectPath.empty()) + { + printf("-p option defined twice, should only be defined once\n"); + exit(1); + } + + if(i < argc - 1) + { + projectPath = argv[i + 1]; + ++i; + } + else + { + printf("Expected project path after option -p\n"); + exit(1); + } + } + else if(strcmp(arg, "-h") == 0) + { + usage(); + } + else + { + printf("Invalid option %s, type sibs -h to see valid options\n", arg); + exit(1); + } + } + else + { + if(!command.empty()) + { + printf("Found command twice. First as %s, then as %s\n", command.c_str(), arg); + exit(1); + } + command = arg; + } + } + + if(projectPath.empty()) + { + Result projectPathResult = getCwd(); + if(projectPathResult.isErr()) + { + printf("%s\n", projectPathResult.getErrMsg().c_str()); + exit(11); + } + projectPath = projectPathResult.unwrap(); + } + + if(command == "build") + build(projectPath); + else if(command.empty()) + { + printf("No command provided, type sibs -h to see valid commands\n"); + exit(1); + } + else + { + printf("Invalid command %s, expected: build\n", command.c_str()); + exit(1); + } return 0; } -- cgit v1.2.3