util/CommandLine: make non-option args accessible
The argv list must not be temporary for this, and became a class member. Change-Id: I159dd2a0d43768a7926b639f93a4ff9f2bfb7c9a
This commit is contained in:
@ -11,7 +11,7 @@ namespace fail {
|
||||
// Filter out all command line arguments that start with -Wf,
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
if (strncmp(argv[i], "-Wf,", 4) == 0) {
|
||||
this->argv.push_back(std::string(argv[i] + 4));
|
||||
this->argv.push_back(argv[i] + 4);
|
||||
|
||||
// also copy argv[argc], which equals 0
|
||||
for (int x = i + 1; x <= argc; ++x) {
|
||||
@ -42,31 +42,27 @@ namespace fail {
|
||||
option::Descriptor desc = {0, 0, 0, 0, 0, 0};
|
||||
this->options.push_back(desc);
|
||||
|
||||
// Build an argv array
|
||||
std::vector<const char *> tmp_argv;
|
||||
int argc = this->argv.size();
|
||||
for (unsigned i = 0; i < this->argv.size(); ++i)
|
||||
tmp_argv.push_back(this->argv[i].c_str());
|
||||
|
||||
// Generate the options stats
|
||||
option::Stats stats(this->options.data(), argc, tmp_argv.data());
|
||||
option::Stats stats(this->options.data(), argv.size(), argv.data());
|
||||
|
||||
if (parsed_options)
|
||||
delete[] parsed_options;
|
||||
if (parsed_buffer)
|
||||
delete[] parsed_buffer;
|
||||
if (m_parser)
|
||||
delete m_parser;
|
||||
|
||||
parsed_options = new option::Option[stats.options_max];
|
||||
parsed_buffer = new option::Option[stats.buffer_max];
|
||||
|
||||
option::Parser parse(this->options.data(), argc, tmp_argv.data(),
|
||||
m_parser = new option::Parser(this->options.data(), argv.size(), argv.data(),
|
||||
parsed_options, parsed_buffer);
|
||||
|
||||
|
||||
// Pop the terminating entry
|
||||
this->options.pop_back();
|
||||
|
||||
return !parse.error();
|
||||
return !m_parser->error();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -22,9 +22,10 @@ namespace fail {
|
||||
private:
|
||||
static CommandLine m_instance;
|
||||
|
||||
std::vector<std::string> argv;
|
||||
std::vector<const char *> argv;
|
||||
std::vector<option::Descriptor> options;
|
||||
option::Option *parsed_options, *parsed_buffer;
|
||||
option::Parser *m_parser;
|
||||
public:
|
||||
/// Handle for accessing the parsed data of an option
|
||||
typedef int option_handle;
|
||||
@ -45,7 +46,7 @@ namespace fail {
|
||||
/**
|
||||
* Add a argument manually
|
||||
*/
|
||||
void add_args(char *value) { argv.push_back(value); }
|
||||
void add_args(const char *value) { argv.push_back(value); }
|
||||
|
||||
/**
|
||||
* Add a option to the command line interface of the fail-client
|
||||
@ -89,6 +90,12 @@ namespace fail {
|
||||
int columns = getenv("COLUMNS")? atoi(getenv("COLUMNS")) : 80;
|
||||
option::printUsage(fwrite, stdout, options.data(), columns);
|
||||
}
|
||||
/**
|
||||
* Return the internal option::Parser object for further usage.
|
||||
*/
|
||||
option::Parser *parser() {
|
||||
return m_parser;
|
||||
}
|
||||
};
|
||||
|
||||
} // end of namespace
|
||||
|
||||
Reference in New Issue
Block a user