VB icon

Scan command arguments v2.0

Email
Submitted on: 8/24/2016 8:02:51 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++, UNIX C++
Views: 1338
 
     This is a small project to scan and process a command line with command flags set code is very simple and made for a beginner hope you find is helpfull.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Scan command arguments v2.0
// Description:This is a small project to scan and process a command line with command flags set code is very simple and made for a beginner hope you find is helpfull.
// By: Ben
//**************************************

// Scan and process command line arguments
// By DreamVB
#include <iostream>
using namespace std;
using std::cout;
using std::endl;
int main(int argc, char *argv[]){
	//Sample commmand line
	//app.exe -a -b FileToProcess.txt
	//Exit if no arguments
	if(argc <=1){
		return 0;
	}
	//Get part appname
	argv++;
	//Check if argc is > 1 and then scan for flag char - and process as required.
	while (argc > 1 && *(*argv)++== '-'){
		if(argv){
			//By here we can now do what ever with switchs found.
			switch(toupper(**argv)){
			case 'A':
				cout << "A Flag was found, Put your code here" << endl;
				break;
			case 'B':
				//make a beep.
				cout << (char)7;
				break;
			case 'H':
				cout << "You can put your help message here." << endl;
				exit(1);
			default:
				cout << "Unknown input argument found." << endl;
				break;
			}
		}
		//Get next
		argc--;
		argv++;
	}
	//Move back one so we can get the filename at the end of the flags
	(*argv)--;
	//Print out file name
	cout << "Filename = " << argv[0] << endl;
	system("pause");
	return 1;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.