[thin_delta] read in the snaps and dev

This commit is contained in:
Joe Thornber 2014-06-09 13:26:55 +01:00
parent b03fa373a1
commit e851b35954
2 changed files with 95 additions and 11 deletions

View File

@ -12,7 +12,7 @@ Feature: thin_delta
Then it should pass with:
"""
Usage: thin_delta [options]
Usage: thin_delta [options] --snap1 <snap> --snap2 <snap> <device or file>
Options:
{-h|--help}
{-V|--version}
@ -22,7 +22,7 @@ Feature: thin_delta
When I run `thin_delta -h`
Then it should pass with:
"""
Usage: thin_delta [options]
Usage: thin_delta [options] --snap1 <snap> --snap2 <snap> <device or file>
Options:
{-h|--help}
{-V|--version}
@ -31,3 +31,24 @@ Feature: thin_delta
Scenario: Unrecognised option should cause failure
When I run `thin_delta --unleash-the-hedeghogs`
Then it should fail
Scenario: --snap1 must be specified
When I run `thin_delta --snap2 45 foo`
Then it should fail with:
"""
--snap1 not specified.
"""
Scenario: --snap2 must be specified
When I run `thin_delta --snap1 45 foo`
Then it should fail with:
"""
--snap2 not specified.
"""
Scenario: device must be specified
When I run `thin_delta --snap1 45 --snap2 50`
Then it should fail with:
"""
No input device provided.
"""

View File

@ -1,3 +1,5 @@
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
#include <getopt.h>
#include <iostream>
#include <libgen.h>
@ -9,41 +11,102 @@ using namespace std;
//----------------------------------------------------------------
namespace {
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl;
}
class application {
public:
application(string const &cmd)
: cmd_(cmd) {
}
void usage(ostream &out) {
out << "Usage: " << cmd_ << " [options] --snap1 <snap> --snap2 <snap> <device or file>" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl;
}
void die(string const &msg) {
cerr << msg << endl;
usage(cerr);
exit(1);
}
unsigned parse_snap(string const &str) {
try {
return boost::lexical_cast<unsigned>(str);
} catch (...) {
ostringstream out;
out << "Couldn't parse snapshot designator: '" << str << "'";
die(out.str());
}
return 0; // never get here
}
private:
string cmd_;
};
struct flags {
boost::optional<string> dev;
boost::optional<unsigned> snap1;
boost::optional<unsigned> snap2;
};
}
//----------------------------------------------------------------
// FIXME: add metadata snap switch
int main(int argc, char **argv)
{
int c;
flags fs;
application app(basename(argv[0]));
char const shortopts[] = "hV";
option const longopts[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' }
{ "version", no_argument, NULL, 'V' },
{ "snap1", required_argument, NULL, 1 },
{ "snap2", required_argument, NULL, 2 },
};
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (c) {
case 'h':
usage(cout, basename(argv[0]));
app.usage(cout);
return 0;
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
case 1:
fs.snap1 = app.parse_snap(optarg);
break;
case 2:
fs.snap2 = app.parse_snap(optarg);
break;
default:
usage(cerr, basename(argv[0]));
app.usage(cerr);
return 1;
}
}
if (argc == optind)
app.die("No input device provided.");
else
fs.dev = argv[optind];
if (!fs.snap1)
app.die("--snap1 not specified.");
if (!fs.snap2)
app.die("--snap2 not specified.");
return 0;
}