Jump to content

  • Log In with Google      Sign In   
  • Create Account

some help with command line parsing (got everything to work..sort of)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Enerjak   Members   -  Reputation: 232

Like
0Likes
Like

Posted 03 June 2013 - 01:11 AM

I decided to try my hand at command line parsing and I found a good tutorial here: http://www.cplusplus.com/articles/DEN36Up4/

 

But I tried to do this:

 

#include <iostream>
#include <string>
#include <map>
#include <fstream>
using namespace std;

typedef map<string,string> Arguments;
int main(int argc, char** argv)
{
	Arguments args;
	if(argc < 3)
	{
		cout << "ERROR: " << endl;
		return 1;
	}
	for(int i = 1; i < argc; i++)
	{
		string name = string(argv[i]);

		if(name == "--Help")
		{
			if(i + 1 < argc)
			{
				char* file = argv[i++];
				ofstream outfile;
				outfile.open(file);
				if(outfile.is_open())
				{
					outfile << "Hello Jesus." << endl;
					
					
				}
				outfile.close();
				cout << "File Created: " << endl;
			}
			
		}
	}
	return 0;
}

as you can see, i'm trying to create a file when "--Help" is used (this is just a test, the final command wont be --Help. any ideas?

 



Sponsor:

#2 bradbobak   Members   -  Reputation: 1098

Like
2Likes
Like

Posted 03 June 2013 - 02:16 AM

 You have:

 

char* file = argv[i++];

 

 If the filename is to be supplied after "--Help" perhaps you want '++i'.

 Just as a side note, the strings == is case-sensitive so '--help' wouldn't match.



#3 NightCreature83   Crossbones+   -  Reputation: 2823

Like
1Likes
Like

Posted 03 June 2013 - 05:32 AM

You might want to clarify that error you are outputing as well as just ERROR tells a user nothing of what he did.

 

Also you might want to be really careful in incrementing or decrementing your loop counter outside the loop definition as it makes errors hard to spot and could potentially allow you to index out of bounds.


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#4 Enerjak   Members   -  Reputation: 232

Like
0Likes
Like

Posted 03 June 2013 - 07:00 AM

none of that worked...........well, there has to be a way to get that to work, creating a file via command line arguments....everyone else seems to know what they are doing.



#5 SiCrane   Moderators   -  Reputation: 9594

Like
0Likes
Like

Posted 03 June 2013 - 07:06 AM

Can you be more explicit on what exactly is going wrong?

#6 rip-off   Moderators   -  Reputation: 8210

Like
0Likes
Like

Posted 03 June 2013 - 07:25 AM

Give us an example of the command line you are trying to pass, and what is (or isn't) happening.

 

Have you tried using a debugger? Or even just printing some extra statements, e.g. "Found --Hello as argument {i}", "Skipping unrecognised argument {i} {argument}" and "Created file: {filename}", along with maybe printing "Failed to open {filename}" if the file failed to open.



#7 henryksienkiewicz   Members   -  Reputation: 178

Like
0Likes
Like

Posted 03 June 2013 - 08:15 AM

Hi,
on a side note from the original problem, have you tried using boost program options?

http://www.boost.org/doc/libs/1_53_0/doc/html/program_options/tutorial.html

I haven't used it myself but it looks like it has everything what can be interesting in terms of passing command line arguments.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS