Switch thin_dump.cc from boost command line option parsing to getopt_long

This commit is contained in:
Heinz Mauelshagen
2011-12-14 15:55:50 +01:00
parent 78e37bb234
commit bad6aff58b
2 changed files with 31 additions and 24 deletions

View File

@@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
#include <iostream> #include <iostream>
#include <getopt.h>
#include "human_readable_format.h" #include "human_readable_format.h"
#include "metadata_dumper.h" #include "metadata_dumper.h"
@@ -50,41 +51,48 @@ namespace {
metadata_dump(md, e); metadata_dump(md, e);
} }
void usage(po::options_description const &desc) { void usage(void) {
cerr << "Usage: thin_dump [options] <metadata device or file>" << endl << endl; cerr << "Usage: thin_dump [options] <metadata device or file>" << endl << endl;
cerr << desc; cerr << "Options:" << endl;
cerr << " --help Produce help message" << endl;
cerr << " -f [ --format ] arg (=xml) Select format (human_readable|xml)" << endl;
cerr << " -i [ --input ] arg Input file" << endl;
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
po::options_description desc("Options"); int c;
desc.add_options() const char shortopts[] = "hfi";
("help", "Produce help message") string filename, format = "xml";
("format,f", po::value<string>()->default_value("xml"), "Select format (human_readable|xml)") const struct option longopts[] = {
("input,i", po::value<string>(), "Input file") { "help", no_argument, NULL, 'h'},
; { "format", required_argument, NULL, 'f' },
{ "input", required_argument, NULL, 'i'},
{ NULL, no_argument, NULL, 0 }
};
po::positional_options_description p; while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
p.add("input", -1); switch(c) {
case 'h':
po::variables_map vm; usage();
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); return 1;
po::notify(vm); case 'f':
format = optarg;
if (vm.count("help")) { break;
usage(desc); case 'i':
return 0; filename = optarg;
break;
}
} }
if (vm.count("input") != 1) { if (argc == 1 ||
cerr << "No input file provided." << endl; filename.empty()) {
usage(desc); usage();
return 1; return 1;
} }
dump(vm["input"].as<string>(), vm["format"].as<string>()); dump(filename, format);
return 0; return 0;
} }

View File

@@ -21,7 +21,6 @@
#include "metadata.h" #include "metadata.h"
#include "metadata_checker.h" #include "metadata_checker.h"
using namespace boost;
using namespace persistent_data; using namespace persistent_data;
using namespace std; using namespace std;
using namespace thin_provisioning; using namespace thin_provisioning;