Determining program options

W

William Payne

Hello, I am making a very simple and crude Makefile generator, that
currently supports three different options:
--project-name=<name_of_project>
--source-files=<source_file_names_separated_by_commas>
--resource-file=<name_of_resource_file_if_any>

The first thing I do is to put all program arguments (excluding argv[0]) in
a std::vector of std::strings. Then I call a function to determine which
options were passed to the program and their values. And this is the
function I need help with, I implemented it like this:
void obtain_arguments(const std::vector<std::string>& arguments,
std::string& project_name,
std::vector<std::string>& source_files,
std::string& resource_file)
{
const std::string project_name_string = "--project-name=";
const std::string source_files_string = "--source-files=";
const std::string resource_file_string = "--resource-file=";

for(size_t i = 0; i < arguments.size(); ++i)
{
if(arguments.find(project_name_string, 0) == 0)
{
project_name = arguments.substr(project_name_string.length(),
arguments.length());
}
else if(arguments.find(source_files_string, 0) == 0)
{
std::string source_files_as_string =
arguments.substr(source_files_string.length(), arguments.length());

split_source_file_names(source_files_as_string, source_files);
}
else if(arguments.find(resource_file_string, 0) == 0)
{
resource_file = arguments.substr(resource_file_string.length(),
arguments.length());
}
}
}

Is there a better way to do this? My functions seems to work for
non-errornous (spelling) input (I will add more error checking later), but
it just feels ugly somehow. Should I do this another way or keep it as it
is?

/ William Payne
 
R

red floyd

William said:
Hello, I am making a very simple and crude Makefile generator, that
currently supports three different options:
--project-name=<name_of_project>
--source-files=<source_file_names_separated_by_commas>
--resource-file=<name_of_resource_file_if_any>

The first thing I do is to put all program arguments (excluding argv[0]) in
a std::vector of std::strings. Then I call a function to determine which
options were passed to the program and their values. And this is the
function I need help with, I implemented it like this:
void obtain_arguments(const std::vector<std::string>& arguments,
std::string& project_name,
std::vector<std::string>& source_files,
std::string& resource_file)
{
const std::string project_name_string = "--project-name=";
const std::string source_files_string = "--source-files=";
const std::string resource_file_string = "--resource-file=";

for(size_t i = 0; i < arguments.size(); ++i)
{
if(arguments.find(project_name_string, 0) == 0)
{
project_name = arguments.substr(project_name_string.length(),
arguments.length());
}
else if(arguments.find(source_files_string, 0) == 0)
{
std::string source_files_as_string =
arguments.substr(source_files_string.length(), arguments.length());

split_source_file_names(source_files_as_string, source_files);
}
else if(arguments.find(resource_file_string, 0) == 0)
{
resource_file = arguments.substr(resource_file_string.length(),
arguments.length());
}
}
}

Is there a better way to do this? My functions seems to work for
non-errornous (spelling) input (I will add more error checking later), but
it just feels ugly somehow. Should I do this another way or keep it as it
is?


Yeah. Go look at one of the GNU programming groups. I believe what you
are looking for is called "getopt_long()".
 
G

Gianni Mariani

William Payne wrote:
....
Is there a better way to do this? My functions seems to work for
non-errornous (spelling) input (I will add more error checking later), but
it just feels ugly somehow. Should I do this another way or keep it as it
is?

See this example from CommonC++.

http://www.gnu.org/software/commoncpp/docs/refman/html/cmdlineopt_8cpp-example.html

This turns into a much larger question. In light of modularity (or
separating requirements into the modules they are used) being a Good
Thing (TM), the method you propose (which is a very common approach)
breaks modularity since an unrelated piece of code need to co-operate
which leads to plug and play difficulty.

When you keep thinking about it this way, you realize that the
validation of the parameter is a property of the parameters itself and
hence the CommonC++ system is designed such that you can do exactly
that. You can derive CommandOptionXXX classes that may include the main
functionality.

The CommonC++ command option stuff is more than sufficient for may
applications, however, for really complex systems, this gets even more
interesting. One recent system I developed where "parameters" could be
derived from many different sources and different instances leads to a
far more complex model than the one shown here. .... but that's another
story.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top