|
| 1 | +#include <getopt.h> |
| 2 | + |
1 | 3 | #include "pluginManager.hpp" |
2 | 4 | #include "stateManager.hpp" |
| 5 | +#if (!__has_include("version.h")) |
| 6 | +#error "version.h not found, please generate it with ./scripts/generateVersion.sh" |
| 7 | +#else |
| 8 | +#include "version.h" |
| 9 | +#endif |
| 10 | + |
| 11 | +static void version() |
| 12 | +{ |
| 13 | + fmt::print( |
| 14 | + "cufetchpm {} built from branch '{}' at {} commit '{}' ({}).\n" |
| 15 | + "Date: {}\n" |
| 16 | + "Tag: {}\n", |
| 17 | + VERSION, GIT_BRANCH, GIT_DIRTY, GIT_COMMIT_HASH, GIT_COMMIT_MESSAGE, GIT_COMMIT_DATE, GIT_TAG); |
| 18 | + |
| 19 | + // if only everyone would not return error when querying the program version :( |
| 20 | + std::exit(EXIT_SUCCESS); |
| 21 | +} |
| 22 | + |
| 23 | +static void help(int invalid_opt = false) |
| 24 | +{ |
| 25 | + constexpr std::string_view help( |
| 26 | + R"(Usage: cufetchpm [OPERATION] [ARGUMENTS] [OPTIONS]... |
| 27 | +Manage plugins for customfetch. |
| 28 | +NOTE: the operations must be the first argument to pass |
| 29 | +
|
| 30 | + -h, --help Print this help menu. |
| 31 | + -V, --version Print version and other infos about the build. |
| 32 | +
|
| 33 | +OPERATIONS: |
| 34 | + add - Add a new plugin repository. Takes as an argument the git url to be cloned. |
| 35 | +)"); |
| 36 | + |
| 37 | + fmt::print("{}", help); |
| 38 | + fmt::print("\n"); |
| 39 | + std::exit(invalid_opt); |
| 40 | +} |
| 41 | + |
| 42 | +static bool parseargs(int argc, char* argv[]) |
| 43 | +{ |
| 44 | + int opt = 0; |
| 45 | + int option_index = 0; |
| 46 | + const char *optstring = "-Vh"; |
| 47 | + static const struct option opts[] = { |
| 48 | + {"version", no_argument, 0, 'V'}, |
| 49 | + {"help", no_argument, 0, 'h'}, |
| 50 | + |
| 51 | + {0,0,0,0} |
| 52 | + }; |
| 53 | + |
| 54 | + /* parse operation */ |
| 55 | + optind = 0; |
| 56 | + while ((opt = getopt_long(argc, argv, optstring, opts, &option_index)) != -1) |
| 57 | + { |
| 58 | + switch (opt) |
| 59 | + { |
| 60 | + case 0: break; |
| 61 | + case '?': help(EXIT_FAILURE); break; |
| 62 | + |
| 63 | + case 'V': version(); break; |
| 64 | + case 'h': help(); break; |
| 65 | + default: return false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return true; |
| 70 | +} |
3 | 71 |
|
4 | 72 | int main(int argc, char* argv[]) |
5 | 73 | { |
| 74 | + if (!parseargs(argc, argv)) |
| 75 | + return -1; |
| 76 | + |
6 | 77 | std::filesystem::create_directories({ getHomeCacheDir() / "cufetchpm" / "plugins" }); |
7 | 78 | StateManager state; |
8 | 79 | PluginManager man(std::move(state)); |
|
0 commit comments