-
argv processing
Getting bogged down on finishing my first serious prog.
I have created a class to hold all command line args, constants, program variables (perhaps globals and constants better describes it):
Code:
class c_program_variables
{
public:
string m_testname;
// output filenames
string m_s_filename_timout_piped;
string m_s_filename_timout_evt;
string m_s_filename_timout_err;
// Input file names
string m_s_filename_comout;
string m_s_filename_runout;
// error terminates run, warning written to m_s_filename_timout_err
bool m_b_error_flag;
bool m_b_warning_flag;
int m_i_95_percentile; // additional percentile output req'd
int m_i_response_precision;
bool m_b_timout_evt_reqd; // output formatted timeout
bool m_b_timout_piped_reqd; // output piped timout for spreadsheet input
// timestamp limits input by arguments
static time_t d_starttime; // = 0;
time_t d_endtime; // = 159999999;
// timestamps used to store min and max in comout
time_t d_earliesttime; // = 159999999;
time_t d_latesttime; // = 0;
// methods
c_program_variables::c_program_variables (int argc, char *argv);
}
I want the constructor to process argc, argv and set the other various global type information.
Qs
1. Is this a good idea?
2. How do I usefully pass *argv[] into my constructor?
Any help greatly appreciated. Nigel
-
Re: argv processing
Is it a good idea? That's up to you. I've found it helpful to do something like this occasionally.
How do you pass argv[]? It looks like you've already done it. What's the problem?
Code:
main(int argc, char *argv[])
{
c_program_variables cpv(argc, argv);
}
c_program_variables cpv::c_program_variables cpv(int argc, char *argv[])
{
for( int i=0; i<argc; ++i )
/* use argv[i] */
}
-
Re: argv processing
It's all relative. I personally wouldn't do something like this because it gives the appearance of OOP, when it's not. If you want something to simply encapsulate data, use a struct. If you have a good reason for using a class, however, then go ahead and use one. As for functions which parse argv, I think that's a very good idea because it keeps the code cleaner and more modular by making a function instead of dumping everything into main.
-
Re: argv processing
Thanks very much guys. Now it works just passing the basic variables:
In main:
c_program_variables ci_program_variables(argc, argv);
My prototype is:
c_program_variables::c_program_variables (int argc, char **argw);
Yup agree that it is like playing at OOP and that was my concern. Certainly the method keeps it tidy and easy to maintain (I hope).
At the mo I keep all my general gardening functions in a seperate file "Odds_and_sods.h" sort of thing and I guess therefore without the class the struct and the function would possibly get seperated. I guess you only know when you leave it for a while and have to return to bugfix, extend or whatever.
But this all started as a test to see if C++ should replace the C bits in our loadtest toolbox and so far it looks great. But that's my opinion and I never used C.