Support {-V|--version} and adjust usage syntax

This commit is contained in:
Heinz Mauelshagen 2011-12-15 14:54:40 +01:00
parent 4e8f255e97
commit 8414bb69d8
4 changed files with 59 additions and 20 deletions

View File

@ -122,7 +122,7 @@ dnl -- Check for getopt
AC_CHECK_HEADERS(getopt.h, AC_DEFINE([HAVE_GETOPTLONG], 1, [Define to 1 if getopt_long is available.])) AC_CHECK_HEADERS(getopt.h, AC_DEFINE([HAVE_GETOPTLONG], 1, [Define to 1 if getopt_long is available.]))
################################################################################ ################################################################################
VERSION="\"`cat "$srcdir"/VERSION 2>/dev/null || echo Unknown`\"" THIN_PROVISIONING_TOOLS_VERSION="\"`cat "$srcdir"/VERSION 2>/dev/null || echo Unknown`\""
VER=`cat "$srcdir"/VERSION` VER=`cat "$srcdir"/VERSION`
RELEASE_DATE="\"`echo $VER | $SED 's/.* (//;s/).*//'`\"" RELEASE_DATE="\"`echo $VER | $SED 's/.* (//;s/).*//'`\""
@ -139,6 +139,8 @@ AC_SUBST(CXXOPTIMISE_FLAG)
AC_SUBST(INSTALL) AC_SUBST(INSTALL)
AC_SUBST(prefix) AC_SUBST(prefix)
AC_SUBST(RELEASE_DATE) AC_SUBST(RELEASE_DATE)
AC_SUBST(RELEASE_DATE)
AC_SUBST(THIN_PROVISIONING_TOOLS_VERSION)
################################################################################ ################################################################################
dnl -- First and last lines should not contain files to generate in order to dnl -- First and last lines should not contain files to generate in order to
@ -146,5 +148,6 @@ dnl -- keep utility scripts running properly
AC_CONFIG_FILES([ AC_CONFIG_FILES([
Makefile Makefile
unit-tests/Makefile.in unit-tests/Makefile.in
version.h
]) ])
AC_OUTPUT AC_OUTPUT

View File

@ -23,6 +23,7 @@
#include "metadata_dumper.h" #include "metadata_dumper.h"
#include "metadata.h" #include "metadata.h"
#include "xml_format.h" #include "xml_format.h"
#include "version.h"
using namespace persistent_data; using namespace persistent_data;
using namespace std; using namespace std;
@ -47,43 +48,48 @@ namespace {
metadata_dump(md, e); metadata_dump(md, e);
} }
void usage(void) { void usage(string const &cmd) {
cerr << "Usage: thin_dump [options] <metadata device or file>" << endl << endl; cerr << "Usage: " << cmd << " [options] {metadata device|file}" << endl << endl;
cerr << "Options:" << endl; cerr << "Options:" << endl;
cerr << " -h [ --help ] Produce help message" << endl; cerr << " {-h|--help}" << endl;
cerr << " -f [ --format ] arg (=xml) Select format (human_readable|xml)" << endl; cerr << " {-f|--format} {xml|human_readable}" << endl;
cerr << " -i [ --input ] arg Input file" << endl; cerr << " {-i|--input} {xml|human_readable} input_file" << endl;
cerr << " {-V|--version}" << endl;
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int c; int c;
const char shortopts[] = "hf:i:"; const char shortopts[] = "hf:i:V";
string filename, format = "xml"; string filename, format = "xml";
const struct option longopts[] = { const struct option longopts[] = {
{ "help", no_argument, NULL, 'h'}, { "help", no_argument, NULL, 'h'},
{ "format", required_argument, NULL, 'f' }, { "format", required_argument, NULL, 'f' },
{ "input", required_argument, NULL, 'i'}, { "input", required_argument, NULL, 'i'},
{ "version", no_argument, NULL, 'V'},
{ NULL, no_argument, NULL, 0 } { NULL, no_argument, NULL, 0 }
}; };
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) { switch(c) {
case 'h': case 'h':
usage(); usage(argv[0]);
return 1; return 0;
case 'f': case 'f':
format = optarg; format = optarg;
break; break;
case 'i': case 'i':
filename = optarg; filename = optarg;
break; break;
case 'V':
cerr << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
} }
} }
if (argc == 1) { if (argc == 1) {
usage(); usage(argv[0]);
return 1; return 1;
} }

View File

@ -17,9 +17,11 @@
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
#include <iostream> #include <iostream>
#include <getopt.h>
#include "metadata.h" #include "metadata.h"
#include "metadata_checker.h" #include "metadata_checker.h"
#include "version.h"
using namespace persistent_data; using namespace persistent_data;
using namespace std; using namespace std;
@ -39,12 +41,34 @@ namespace {
} }
void usage(string const &cmd) { void usage(string const &cmd) {
cerr << "Usage: " << cmd << " <metadata device>" << endl; cerr << "Usage: " << cmd << " {device|file}" << endl;
cerr << "Options:" << endl;
cerr << " {-h|--help}" << endl;
cerr << " {-V|--version}" << endl;
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int c;
const char shortopts[] = "hV";
const struct option longopts[] = {
{ "help", no_argument, NULL, 'h'},
{ "version", no_argument, NULL, 'V'},
{ NULL, no_argument, NULL, 0 }
};
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) {
case 'h':
usage(argv[0]);
return 0;
case 'V':
cerr << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
}
}
if (argc != 2) { if (argc != 2) {
usage(argv[0]); usage(argv[0]);
exit(1); exit(1);

View File

@ -21,6 +21,7 @@
#include "metadata.h" #include "metadata.h"
#include "restore_emitter.h" #include "restore_emitter.h"
#include "xml_format.h" #include "xml_format.h"
#include "version.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@ -51,43 +52,48 @@ namespace {
#endif #endif
} }
void usage(void) { void usage(string const &cmd) {
cerr << "Usage: thin_restore [options]" << endl << endl; cerr << "Usage: " << cmd << " [options] [file]" << endl << endl;
cerr << "Options:" << endl; cerr << "Options:" << endl;
cerr << " -h [ --help ] Produce help message" << endl; cerr << " {-h|--help}" << endl;
cerr << " -i [ --input ] arg Input file" << endl; cerr << " {-i|--input}" << endl;
cerr << " -o [ --output ] arg Output file" << endl; cerr << " {-o [ --output} output_file" << endl;
cerr << " {-V|--version}" << endl;
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int c; int c;
const char *shortopts = "hi:o:"; const char *shortopts = "hi:o:V";
string input, output; string input, output;
const struct option longopts[] = { const struct option longopts[] = {
{ "help", no_argument, NULL, 'h'}, { "help", no_argument, NULL, 'h'},
{ "input", required_argument, NULL, 'i' }, { "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o'}, { "output", required_argument, NULL, 'o'},
{ "version", no_argument, NULL, 'V'},
{ NULL, no_argument, NULL, 0 } { NULL, no_argument, NULL, 0 }
}; };
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) { switch(c) {
case 'h': case 'h':
usage(); usage(argv[0]);
return 1; return 0;
case 'i': case 'i':
input = optarg; input = optarg;
break; break;
case 'o': case 'o':
output = optarg; output = optarg;
break; break;
case 'V':
cerr << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
} }
} }
if (argc == 1) { if (argc == 1) {
usage(); usage(argv[0]);
return 1; return 1;
} }