473,508 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining program options

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[i].find(project_name_string, 0) == 0)
{
project_name = arguments[i].substr(project_name_string.length(),
arguments[i].length());
}
else if(arguments[i].find(source_files_string, 0) == 0)
{
std::string source_files_as_string =
arguments[i].substr(source_files_string.length(), arguments[i].length());

split_source_file_names(source_files_as_string, source_files);
}
else if(arguments[i].find(resource_file_string, 0) == 0)
{
resource_file = arguments[i].substr(resource_file_string.length(),
arguments[i].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
Jul 22 '05 #1
2 1268
William Payne wrote:
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[i].find(project_name_string, 0) == 0)
{
project_name = arguments[i].substr(project_name_string.length(),
arguments[i].length());
}
else if(arguments[i].find(source_files_string, 0) == 0)
{
std::string source_files_as_string =
arguments[i].substr(source_files_string.length(), arguments[i].length());

split_source_file_names(source_files_as_string, source_files);
}
else if(arguments[i].find(resource_file_string, 0) == 0)
{
resource_file = arguments[i].substr(resource_file_string.length(),
arguments[i].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()".

Jul 22 '05 #2
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/commoncp...p-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.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14146
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
2
1586
by: Lazareth S. Link | last post by:
Hiya to whoever might stumble across this. I've recently taken up learning how to script/program in python. I've made my first program, a simple fahrenheit-celsius converter, both ways. For...
12
2439
by: johny smith | last post by:
I am trying to figure out a way to print the address of what called a certain function once inside the function. I am assuming that this information is on the stack somewhere. But can someone...
2
11388
by: Brian Worth | last post by:
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0 was able to shut down or restart the (windows XP) machine using a series of API calls. (Getlasterror, GetCurrentProcess,...
1
1351
by: Phil Galey | last post by:
In VB.NET, when you use shell or have a process object point to and run a separate application that is installed on the machine, is there a way of determining the path to the executable? I've been...
7
1531
by: Leo Breebaart | last post by:
I have another question where I am not so much looking for a solution but rather hoping to get some feedback on *which* solutions people here consider good Pythonic ways to approach a issue. ...
0
1370
by: ashishbathini | last post by:
Hi guys here is my problem ... this is the source code I Have , honestly I hav no idea how it works bcos its too complicated for me .... but my problem is ... i hav a freq comonent in it .......
9
3833
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
1
5985
by: ElPresidente | last post by:
Hi everyone, I had a macro I wrote in VBA which created several dozen spreadsheets based off a master spreadsheet. Code runs smoothly in VBA, but trying to port it over to VB caused significant...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.