SetFlagsFromCommandLine
looks like a fairly special function
to me, in that it seems to expect argc
and argv
as
arguments, and modifies them (presumably by removing options).
If at all possible, I would call it first, with argc
and
argv
, and do my special handling after. The modification
probably takes place in the form of shifting pointers in argv
down, which means that any solution using std::vector
will
require some additional handling after calling the function.
If you cannot change the order of handling (e.g. because
--ipc-in-channel
and its argument might be extracted by
SetFlagsFromCommandLine
), then you'll probably need something
like the following (after you've parsed your args):
std::vector<char*> tmp;
for ( auto current = argv_copy.begin();
current != argv_copy.end();
++ current ) {
tmp.push_back( current->empty ? "" : &(*current)[0] );
}
int argc_copy = tmp.size();
SetFlagsFromCommandLine( &argc_copy, &tmp[0] );
if ( argc_copy != argv_copy.size() ) {
auto dest = argv_copy.begin();
auto src = tmp.begin();
while ( dest != argv_copy.end() ) {
if ( (*src)[0] == '\0' ) {
if ( *dest != "" ) {
++ dest ;
} else {
while ( dest != argv_copy.end() && *dest != "") {
dest = argv.erase( dest );
}
}
} else {
if ( !dest->empty() && &(*dest)[0] == *src ) {
++ dest;
} else {
while( dest != argv_copy.end()
&& (dest->empty() || &(*dest)[0] != *src) ) {
dest = argv.erase( dest );
}
}
}
}
assert( argv_copy.size() == argc_copy );
}
This is just off the top of my head, and probably contains some
errors, but it gives an idea of the effort necessary if you
cannot call SetFlagsFromCommandLine
first.
EDIT:
Alternatively, and although it is a bit of a pain, you can
process argc
and argv
in exactly the same way
SetFlagsFromCommandLine
does internally, without putting them
in an std::vector
:
int dest = 1;
int source = 1;
while ( source != argc ) {
if ( inChannelParam == argv[source] == 0 && source < argc - 1 ) {
inChannelName = argv[source + 1];
source += 2;
} else if ( outChannelPara == argv[source] == 0 && source < argc - 1 ) {
outChannelName = argv[source + 1];
source += 2;
} else {
argv[dest] = argv[source];
++ dest;
++ source;
}
}
argc = dest;
SetFlagsFromCommandLine( &argc, argv );
std::vector<std::string> argv_copy( argv, argv + argc );
It's not fun dealing with char**
et al., but since you need
this format later, it's probably less of a pain than translating
multiple times between the formats.