1
0
mirror of https://github.com/Zygo/bees.git synced 2025-05-17 21:35:45 +02:00

Implement getopt options parser

This commit adds a simple getopt options parser to show help. This can
be used as a boilerplate for adding more options later.

Signed-off-by: Kai Krakow <kai@kaishome.de>
This commit is contained in:
Kai Krakow 2017-10-27 22:36:00 +02:00
parent 29d2d51c47
commit c6bf6bfe1d

View File

@ -19,18 +19,23 @@
#include <linux/fs.h> #include <linux/fs.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <getopt.h>
using namespace crucible; using namespace crucible;
using namespace std; using namespace std;
int int
do_cmd_help(const char **argv) do_cmd_help(char *argv[])
{ {
cerr << "Usage: " << argv[0] << " fs-root-path [fs-root-path-2...]\n" cerr << "Usage: " << argv[0] << " [options] fs-root-path [fs-root-path-2...]\n"
"Performs best-effort extent-same deduplication on btrfs.\n" "Performs best-effort extent-same deduplication on btrfs.\n"
"\n" "\n"
"fs-root-path MUST be the root of a btrfs filesystem tree (id 5).\n" "fs-root-path MUST be the root of a btrfs filesystem tree (id 5).\n"
"Other directories will be rejected.\n" "Other directories will be rejected.\n"
"\n" "\n"
"Options:\n"
"\t-h, --help\t\tShow this help\n"
"\n"
"Optional environment variables:\n" "Optional environment variables:\n"
"\tBEESHOME\tPath to hash table and configuration files\n" "\tBEESHOME\tPath to hash table and configuration files\n"
"\t\t\t(default is .beeshome/ in the root of each filesystem).\n" "\t\t\t(default is .beeshome/ in the root of each filesystem).\n"
@ -575,7 +580,7 @@ BeesTempFile::make_copy(const BeesFileRange &src)
} }
int int
bees_main(int argc, const char **argv) bees_main(int argc, char *argv[])
{ {
set_catch_explainer([&](string s) { set_catch_explainer([&](string s) {
BEESLOG("\n\n*** EXCEPTION ***\n\t" << s << "\n***\n"); BEESLOG("\n\n*** EXCEPTION ***\n\t" << s << "\n***\n");
@ -589,14 +594,35 @@ bees_main(int argc, const char **argv)
shared_ptr<BeesContext> bc; shared_ptr<BeesContext> bc;
THROW_CHECK1(invalid_argument, argc, argc >= 0); THROW_CHECK1(invalid_argument, argc, argc >= 0);
vector<string> args(argv + 1, argv + argc);
// Parse options
int c;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{ "help", no_argument, NULL, 'h' }
};
c = getopt_long(argc, argv, "h", long_options, &option_index);
if (-1 == c) {
break;
}
switch (c) {
case 'T':
case 'h':
do_cmd_help(argv);
default:
return 2;
}
}
// Create a context and start crawlers // Create a context and start crawlers
bool did_subscription = false; bool did_subscription = false;
for (string arg : args) { while (optind < argc) {
catch_all([&]() { catch_all([&]() {
bc = make_shared<BeesContext>(bc); bc = make_shared<BeesContext>(bc);
bc->set_root_path(arg); bc->set_root_path(argv[optind++]);
did_subscription = true; did_subscription = true;
}); });
} }
@ -617,7 +643,7 @@ bees_main(int argc, const char **argv)
} }
int int
main(int argc, const char **argv) main(int argc, char *argv[])
{ {
cerr << "bees version " << BEES_VERSION << endl; cerr << "bees version " << BEES_VERSION << endl;