[thin_delta] -h/--help

This commit is contained in:
Joe Thornber 2014-06-09 10:48:29 +01:00
parent 87fa6dc079
commit 83f2cce597
2 changed files with 40 additions and 2 deletions

View File

@ -6,3 +6,24 @@ Feature: thin_delta
Scenario: print version (--version flag) Scenario: print version (--version flag)
When I run `thin_delta --version` When I run `thin_delta --version`
Then it should pass with version Then it should pass with version
Scenario: print help
When I run `thin_delta --help`
Then it should pass with:
"""
Usage: thin_delta [options]
Options:
{-h|--help}
{-V|--version}
"""
Scenario: print help
When I run `thin_delta -h`
Then it should pass with:
"""
Usage: thin_delta [options]
Options:
{-h|--help}
{-V|--version}
"""

View File

@ -1,5 +1,6 @@
#include <getopt.h> #include <getopt.h>
#include <iostream> #include <iostream>
#include <libgen.h>
#include "version.h" #include "version.h"
@ -7,16 +8,32 @@ using namespace std;
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace {
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl;
}
}
//----------------------------------------------------------------
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int c; int c;
char const shortopts[] = "V"; char const shortopts[] = "hV";
option const longopts[] = { option const longopts[] = {
{ "version", no_argument, NULL, 'V'} { "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' }
}; };
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':
usage(cout, basename(argv[0]));
return 0;
case 'V': case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl; cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0; return 0;