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
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