Passing Command Line Arguments

Mar 2, 2010 at 6:55am
I need to pass command line arguments out of main. I am using a vector, but I am not sure how to sort out the contents of the vector when I get to the next class.

main.cpp
1
2
3
4
5
6
7
#include "application.h"

int main(int argc,char *argv[]){
	application app;
	vector<string> args(argv, argv + argc);
	return app.run(args);
}


application.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <boost/regex.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "time.h"
using namespace std;



class application{
private:
	//Variables
	boost::regex expression;
	string line;
	string pat;
	string replace;
	int lineNumber;
	char date[9];
	char time[9];
	
	
	void commandLine(vector<string> args){
		string  expressionTwo="";    // Expression
		string  textReplace="";	  // Replacement Text
		string  inputFile="";	  // Input File
		string  outputFile="";    // Output Directory
		for(vector<string>::iterator i = args.begin(); i != args.end(); ++i){
				if (*i == "-e") {
					expressionTwo = *i;
				}
				else if (*i == "-t") {
					textReplace = *i;
				}
				else if (*i == "-i") {
					inputFile = *i;
				}
				else if (*i == "-o") {
					outputFile = *i;
				}
				else{
					cout << "Unknown switch: " 
						 << "Please enter one of the correct parameters:\n" 
						<< "-e + \"expression\"\n-t + \"replacement Text\"\n-i + \"Input File\"\n-o + \"Onput File\"\n";
				}
			}
		cout << "Expression: " << expressionTwo << '\n';
		cout << "Replacement Text " << textReplace << '\n';
		cout << "Input File: " << inputFile << '\n';
		cout << "Output File: " << outputFile << '\n'; 
		}

	void getExpression(){
		cout << "Expression: ";
		getline(cin,pat);
		try{
			expression = pat;
		}
		catch(boost::bad_expression){
			cout << pat << " is not a valid regular expression\n";
			exit(1);
		}
	}

	void boostMatch(){
		//Define replace {FOR TESTING PURPOSES ONLY!!! REMOVE BEFORE SUBMITTING!!
		replace = "";
		_strdate_s(date);
		_strtime_s(time);
		lineNumber = 0;
		//Files to open
		//Input Files
		ifstream in("files/trff292010.csv");
			if(!in) cerr << "no file\n";
		//Output Files
		ofstream newFile("files/NEWtrff292010.csv");
		ofstream copy("files/ORIGtrff292010.csv");
		ofstream report("files/REPORT.dat", ios.app);
		lineNumber++;
		while(getline(in,line)){
			lineNumber++;
			boost::smatch matches;
			copy << line << '\n';
			if (regex_search(line, matches, expression)){
				for (int i = 0; i<matches.size(); ++i){
					report << "Time: " << time << "Date: " << date << '\n'
						<< "Line " << lineNumber <<": " << line << '\n';
					newFile << boost::regex_replace(line, expression, replace) << "\n";

				}
			}else{
				newFile << line << '\n';
			}
		}
	}

public:
	void run(vector<string> args){
		commandLine(args);
	}
};
Mar 2, 2010 at 3:44pm
Does anyone have any helpful tips to lead me in the correct direction
Mar 2, 2010 at 4:05pm
What do you mean by "sort out the vectors"? And I think in your if statements you probably want to assign them to *(i + 1), otherwise your expressions will just be the option statements. You'll need to check you can even iterate to the next element, however.
Mar 2, 2010 at 4:50pm
The vector should contain the argv and the argc. If I send the console command -e [*] it should find -e and make the string expressionTwo [*]. It doesnt it makes expressionTwo -e. It never finds the expression I enter in
Topic archived. No new replies allowed.