-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-write argument parsing for testability
- Loading branch information
1 parent
471cf29
commit 3c92cdd
Showing
6 changed files
with
333 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Copyright - See the COPYRIGHT that is included with this distribution. | ||
* pvxs is distributed subject to a Software License Agreement found | ||
* in file LICENSE that is included with this distribution. | ||
*/ | ||
|
||
#include "cliutil.h" | ||
|
||
namespace pvxs { | ||
|
||
bool operator==(const ArgVal& rhs, const ArgVal& lhs) { | ||
return rhs.defined==lhs.defined && rhs.value==lhs.value; | ||
} | ||
|
||
GetOpt::GetOpt(int argc, char *argv[], const char *spec) | ||
:argv0("<program name>") | ||
{ | ||
if(argc>=1) | ||
argv0 = argv[0]; | ||
|
||
bool allpos = false; // after "--", treat all remaining as positional | ||
for(int i=1; i<argc; i++) { | ||
const char * arg = argv[i]; | ||
if(!allpos && arg[0]=='-') { | ||
arg++; | ||
|
||
if(arg[1]=='-') { | ||
if(arg[2]=='\0') { // "--" | ||
allpos = true; | ||
continue; | ||
} | ||
// "--..." not supported | ||
arguments.emplace_back(-1, argv[i]); | ||
return; | ||
} | ||
// process as short args | ||
|
||
for(; *arg; arg++) { | ||
for(auto s=spec; *s; s++) { | ||
if(*s==*arg) { // match | ||
if(s[1]==':') { // need arg value | ||
if(arg[1]=='\0') { // "-a", "value" | ||
if(i+1==argc) { | ||
// oops. no value | ||
arguments.emplace_back('?', nullptr); | ||
return; | ||
} | ||
arguments.emplace_back(*arg, argv[i+1]); | ||
i++; | ||
|
||
} else { | ||
// "-avalue" | ||
arguments.emplace_back(*arg, &arg[1]); | ||
} | ||
goto nextarg; | ||
|
||
} else { // flag | ||
arguments.emplace_back(*s, nullptr); | ||
// continue scanning for more flags. eg. "-vv" | ||
goto nextchar; | ||
} | ||
} else { | ||
if(s[1]==':') | ||
s++; | ||
} | ||
} | ||
// unrecognized | ||
arguments.emplace_back(-1, nullptr); | ||
return; | ||
nextchar: | ||
(void)0; // need a statement after label... | ||
} | ||
nextarg: | ||
(void)0; | ||
|
||
} else { | ||
positional.push_back(arg); | ||
} | ||
success = true; | ||
} | ||
} | ||
|
||
} // namespace pvxs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright - See the COPYRIGHT that is included with this distribution. | ||
* pvxs is distributed subject to a Software License Agreement found | ||
* in file LICENSE that is included with this distribution. | ||
*/ | ||
|
||
#ifndef CLIUTIL_H | ||
#define CLIUTIL_H | ||
|
||
#include <vector> | ||
#include <string> | ||
#include <map> // for std::pair | ||
|
||
namespace pvxs { | ||
|
||
struct ArgVal { | ||
std::string value; | ||
bool defined = false; | ||
|
||
ArgVal() = default; | ||
ArgVal(std::nullptr_t) {} | ||
ArgVal(const std::string& value) :value(value), defined(true) {} | ||
ArgVal(const char* value) :value(value), defined(true) {} | ||
}; | ||
|
||
bool operator==(const ArgVal& rhs, const ArgVal& lhs); | ||
|
||
struct GetOpt { | ||
GetOpt(int argc, char *argv[], const char *spec); | ||
|
||
const char *argv0; | ||
std::vector<std::string> positional; | ||
std::vector<std::pair<char, ArgVal>> arguments; | ||
bool success = false; | ||
}; | ||
|
||
} // namespace pvxs | ||
|
||
#endif // CLIUTIL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* Copyright - See the COPYRIGHT that is included with this distribution. | ||
* pvxs is distributed subject to a Software License Agreement found | ||
* in file LICENSE that is included with this distribution. | ||
*/ | ||
|
||
#include <vector> | ||
#include <initializer_list> | ||
|
||
#include <string.h> | ||
|
||
#include <pvxs/unittest.h> | ||
#include <pvxs/util.h> | ||
|
||
#include <testMain.h> | ||
#include <epicsUnitTest.h> | ||
#include <dbDefs.h> | ||
|
||
#include "cliutil.h" | ||
|
||
namespace pvxs { namespace detail { | ||
|
||
template<typename E> | ||
struct test_print<std::vector<E>> { | ||
template<class C> | ||
static inline void op(C& strm, const std::vector<E>& v) { | ||
bool first = true; | ||
for(auto& e : v) { | ||
if(first) { | ||
first = false; | ||
} else { | ||
strm<<' '; | ||
} | ||
test_print<E>::op(strm, e); | ||
} | ||
} | ||
}; | ||
|
||
template<> | ||
struct test_print<std::pair<char, ArgVal>> { | ||
template<class C> | ||
static inline void op(C& strm, const std::pair<char, ArgVal>& v) { | ||
strm<<'-'<<v.first; | ||
if(v.second.defined) | ||
strm<<" "<<pvxs::escape(v.second.value); | ||
} | ||
}; | ||
|
||
}} // namespace::detail | ||
|
||
MAIN(testcliutil) { | ||
testPlan(15); | ||
|
||
{ | ||
testDiag("case @%d", __LINE__); | ||
const char* argv[] = {"exe", "-v", "-a", "Aa", "hello"}; | ||
pvxs::GetOpt opts(NELEMENTS(argv), const_cast<char**>(argv), "a:v"); | ||
testEq(opts.argv0, "exe"); | ||
decltype (opts.arguments) arguments({{'v', nullptr}, {'a',"Aa"}}); | ||
testArrEq(opts.arguments, arguments); | ||
decltype (opts.positional) positional({"hello"}); | ||
testArrEq(opts.positional, positional); | ||
} | ||
|
||
{ | ||
testDiag("case @%d", __LINE__); | ||
const char* argv[] = {"exe", "-v", "-a", "Aa", "hello"}; | ||
pvxs::GetOpt opts(NELEMENTS(argv), const_cast<char**>(argv), "va:"); | ||
testEq(opts.argv0, "exe"); | ||
decltype (opts.arguments) arguments({{'v', nullptr}, {'a',"Aa"}}); | ||
testArrEq(opts.arguments, arguments); | ||
decltype (opts.positional) positional({"hello"}); | ||
testArrEq(opts.positional, positional); | ||
} | ||
|
||
{ | ||
testDiag("case @%d", __LINE__); | ||
const char* argv[] = {"exe", "-v", "hello", "-aAa"}; | ||
pvxs::GetOpt opts(NELEMENTS(argv), const_cast<char**>(argv), "va:"); | ||
testEq(opts.argv0, "exe"); | ||
decltype (opts.arguments) arguments({{'v', nullptr}, {'a',"Aa"}}); | ||
testArrEq(opts.arguments, arguments); | ||
decltype (opts.positional) positional({"hello"}); | ||
testArrEq(opts.positional, positional); | ||
} | ||
|
||
{ | ||
testDiag("case @%d", __LINE__); | ||
const char* argv[] = {"exe", "-v", "hello", "-a"}; // missing value | ||
pvxs::GetOpt opts(NELEMENTS(argv), const_cast<char**>(argv), "va:"); | ||
testEq(opts.argv0, "exe"); | ||
decltype (opts.arguments) arguments({{'v', nullptr}, {'?',nullptr}}); | ||
testArrEq(opts.arguments, arguments); | ||
decltype (opts.positional) positional({"hello"}); | ||
testArrEq(opts.positional, positional); | ||
} | ||
|
||
{ | ||
testDiag("case @%d", __LINE__); | ||
const char* argv[] = {"exe", "-vvaTest", "hello"}; | ||
pvxs::GetOpt opts(NELEMENTS(argv), const_cast<char**>(argv), "va:"); | ||
testEq(opts.argv0, "exe"); | ||
decltype (opts.arguments) arguments({{'v', nullptr}, {'v', nullptr}, {'a', "Test"}}); | ||
testArrEq(opts.arguments, arguments); | ||
decltype (opts.positional) positional({"hello"}); | ||
testArrEq(opts.positional, positional); | ||
} | ||
|
||
return testDone(); | ||
} |
Oops, something went wrong.