www.pudn.com > CmdLine-src.zip > cmdlinetest.cpp


//-------------------------------------------------------------------------------------------- 
//   Copyright (c) 2003-2004 Klaus H. Probst [kprobst@vbbox.com] 
// 
//   This software is provided 'as-is', without any express or implied warranty. In no event 
//   will the authors be held liable for any damages arising from the use of this software. 
// 
//   Permission is granted to anyone to use this software for any purpose, including 
//   commercial applications, and to alter it and redistribute it freely, subject to the 
//   following restrictions: 
// 
//   ~ The origin of this software must not be misrepresented; you must not claim that you 
//     wrote the original software. If you use this software in a product, an acknowledgment 
//     in the product documentation would be appreciated but is not required. 
//   ~ Altered source versions must be plainly marked as such, and must not be misrepresented 
//     as being the original software. 
//   ~ This notice may not be removed or altered from any source distribution. 
//-------------------------------------------------------------------------------------------- 
// 
//  cmdlinetest.cpp 
// 
//  A series of test cases for the CommandLine parser class. 
//   
//  To get this working simply add it to a console app in VS6 or VS.NET. 
// 
//-------------------------------------------------------------------------------------------- 
 
// NOTE: Custom command lines are not supported 
// on 9x systems or for ANSI builds under NT/2K/XP 
#define UNICODE 
 
// Note: cmdline.h will #define _UNICODE (which is used by the CRT) if 
// UNICODE is also defined. But if you #include any of the CRT headers 
// (stdlib.h, tchar.h, etc.) you must #define it yourself. 
//#define _UNICODE 
 
#define _WIN32_WINNT 0x0500             // Change as needed 
 
#include  
#include  
#include  
#include  
 
// Standard test case 
void TestCase1() 
{ 
 
	TCHAR* szCommandLine = _T("pos1 pos2 /a /abc /b:c /d @markedarg /e:\"one two\" /f:g /h afterh"); 
 
	CommandLine cmdline(true, szCommandLine, '/', ':'); 
 
	_tprintf(_T("Full command line:\n  %s\n\n"), szCommandLine); 
 
	_tprintf(_T("Argument count: %i\n"), cmdline.Count());				// 11 
	 
	_tprintf(_T("Switch 'a' exists: %s\n"), cmdline.SwitchExists(_T("a")) ? _T("yes") : _T("no"));			// 'yes' 
	_tprintf(_T("Switch 'abx' exists: %s\n"), cmdline.SwitchExists(_T("abx")) ? _T("yes") : _T("no"));		// 'no' 
	_tprintf(_T("Switch 'abc' exists: %s\n"), cmdline.SwitchExists(_T("abc")) ? _T("yes") : _T("no"));		// 'yes' 
	_tprintf(_T("Switch 'x' exists: %s\n"), cmdline.SwitchExists(_T("x")) ? _T("yes") : _T("no"));			// 'no' 
 
	_tprintf(_T("Switch 'a' has value: %s\n"), cmdline.SwitchHasValue(_T("a")) ? _T("yes") : _T("no"));		// 'no' 
	_tprintf(_T("Switch 'b' has value: %s\n"), cmdline.SwitchHasValue(_T("b")) ? _T("yes") : _T("no"));		// 'yes' 
 
	_tprintf(_T("Path: %s\n"), cmdline.Path());							// dummy placeholder 
	_tprintf(_T("Command: %s\n"), cmdline.Command());					// 'pos1' 
 
	_tprintf(_T("Argument count: %i\n"), cmdline.ArgumentCount());	// 2 
 
	_tprintf(_T("Positional 1: %s\n"), cmdline.Argument(1));			// 'pos1' 
	_tprintf(_T("Positional 2: %s\n"), cmdline.Argument(2));			// 'pos2' 
	_tprintf(_T("Positional 3: %s\n"), cmdline.Argument(3));			// NULL 
 
	TCHAR szValue[MAX_PATH]; 
 
	if (!cmdline.SwitchValue(_T("b"), szValue)) 
		lstrcpy(szValue, _T("UNDEFINED")); 
 
	_tprintf(_T("Value of 'b': %s\n"), szValue);									// 'c' 
 
	if (!cmdline.SwitchValue(_T("d"), szValue)) 
		lstrcpy(szValue, _T("UNDEFINED")); 
 
	_tprintf(_T("Value of 'd': %s\n"), szValue);									// 'UNDEFINED' 
 
	if (!cmdline.SwitchValue(_T("e"), szValue)) 
		lstrcpy(szValue, _T("UNDEFINED")); 
 
	_tprintf(_T("Value of 'e': %s\n"), szValue);									// 'one two' 
 
	if (!cmdline.SwitchValue(_T("f"), szValue)) 
		lstrcpy(szValue, _T("UNDEFINED")); 
 
	_tprintf(_T("Value of 'f': %s\n"), szValue);									// 'g' 
 
	_tprintf(_T("Argument afer 'h': %s\n"), cmdline.ArgumentAfter(_T("h")));		// 'afterh' 
	_tprintf(_T("Argument afer 'f': %s\n"), cmdline.ArgumentAfter(_T("f")));		// NULL 
 
 
	_tprintf(_T("Argument marked with '@': %s\n"), cmdline.MarkedArgument('@'));	// 'markedarg' 
 
	_tprintf(_T("Argument at position 4: %s\n"), cmdline[4]);						// '/abc' 
	_tprintf(_T("Argument at position 12: %s\n"), cmdline[12]);						// NULL 
 
} 
 
// Positional arguments 
void TestCase2() 
{ 
 
	TCHAR* szCommandLine = _T("one -t -k two three four"); 
 
	CommandLine cmdline(true, szCommandLine, '-', ':');			// The value delimiter here is not used. 
 
	_tprintf(_T("Full command line:\n  %s\n\n"), szCommandLine); 
 
	_tprintf(_T("Switch 't' exists: %s\n"), cmdline.SwitchExists(_T("t")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'k' exists: %s\n"), cmdline.SwitchExists(_T("k")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'm' exists: %s\n"), cmdline.SwitchExists(_T("m")) ? _T("yes") : _T("no")); 
 
	int nPos = cmdline.LastSwitch(); 
	_tprintf(_T("Last switch: %i\n"), nPos); 
 
	_tprintf(_T("Argument count: %i\n"), cmdline.ArgumentCount()); 
	 
	int nCount = 0; 
	LPCTSTR arg = NULL; 
 
	// 'one' is not included in the enumeration 
	while (1) 
	{ 
		arg = cmdline.NextArgument(&nPos); 
 
		if (arg) 
			_tprintf(_T("  Argument %i: %s (at %i)\n"), nCount++, arg, nPos); 
		else 
			break; 
	 
	} 
 
} 
 
// LastSwitch(), positional arguments and marked argument 
void TestCase3() 
{ 
 
	TCHAR* szCommandLine = _T("one /a bcd /e /f #two three four /g"); 
 
	CommandLine cmdline(true, szCommandLine, '/', ':'); 
 
	_tprintf(_T("Full command line:\n  %s\n\n"), szCommandLine); 
 
	_tprintf(_T("Switch 'a' exists: %s\n"), cmdline.SwitchExists(_T("a")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'a' has value: %s\n"), cmdline.SwitchHasValue(_T("a")) ? _T("yes") : _T("no")); 
 
	// Doesn't exist, so returns false. 
	_tprintf(_T("Switch 'x' has value: %s\n"), cmdline.SwitchHasValue(_T("x")) ? _T("yes") : _T("no")); 
 
	_tprintf(_T("Argument afer 'a': %s\n"), cmdline.ArgumentAfter(_T("a"))); 
 
	_tprintf(_T("Marked argument: %s\n"), cmdline.MarkedArgument('#')); 
 
	int nPos = cmdline.LastSwitch(); 
	_tprintf(_T("Last switch: %i\n"), nPos); 
	_tprintf(_T("Total count: %i\n"), cmdline.Count()); 
 
	_tprintf(_T("Argument count: %i\n"), cmdline.ArgumentCount()); 
	_tprintf(_T("Command: %s\n"), cmdline.Command()); 
	 
	nPos = 1;			// Don't include the 'command' in the enumeration 
	int nCount = 0; 
	LPCTSTR arg = NULL; 
 
	while (1) 
	{ 
		arg = cmdline.NextArgument(&nPos); 
 
		if (arg) 
			_tprintf(_T("  Argument %i: %s (at %i)\n"), nCount++, arg, nPos); 
		else 
			break; 
	 
	} 
 
} 
 
// Alternative argument name support 
void TestCase4() 
{ 
 
	TCHAR* szCommandLine = _T("-azure -b:wow -blue -dodgerblue"); 
 
	CommandLine cmdline(true, szCommandLine, '-', ':'); 
 
	_tprintf(_T("Full command line:\n  %s\n\n"), szCommandLine); 
 
	const TCHAR* args1[] = {_T("a"), _T("azure")}; 
	const TCHAR* args2[] = {_T("b"), _T("blue")}; 
	const TCHAR* args3[] = {_T("c"), _T("chartreuse ")}; 
 
	TCHAR szBind1[32], szBind2[32], szBind3[32]; 
	 
	if (cmdline.SwitchExists(args1, 2, szBind1) == -1) 
		lstrcpy(szBind1, _T("")); 
 
	if (cmdline.SwitchExists(args2, 2, szBind2) == -1) 
		lstrcpy(szBind2, _T("")); 
 
	if (cmdline.SwitchExists(args3, 2, szBind3) == -1) 
		lstrcpy(szBind3, _T("")); 
 
	_tprintf(_T("Argument list test (%s, %s) matched: %s\n"), args1[0], args1[1], szBind1); 
	_tprintf(_T("Argument list test (%s, %s) matched: %s\n"), args2[0], args2[1], szBind2); 
	_tprintf(_T("Argument list test (%s, %s) matched: %s\n"), args3[0], args3[1], szBind3); 
	 
	TCHAR szVal[32]; 
 
	cmdline.SwitchValue(szBind2, szVal); 
	_tprintf(_T("\nValue of matched argument '%s': %s\n\n"), szBind2, szVal); 
 
	_tprintf(_T("Switch 'd' exists: %s\n"), cmdline.SwitchExists(_T("d")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'dodgerblue' exists: %s\n"), cmdline.SwitchExists(_T("dodgerblue")) ? _T("yes") : _T("no")); 
 
} 
 
// Switch values 
void TestCase5() 
{ 
	TCHAR* szCommandLine = _T("/a:b /c:2 /d:\"a value\" /efg:3.4 @C:\\AFile.txt /h \"C:\\Long Path\\A File.ext\""); 
 
	CommandLine cmdline(true, szCommandLine, '/', ':'); 
 
	_tprintf(_T("Full command line:\n  %s\n\n"), szCommandLine); 
 
	_tprintf(_T("Switch 'a' has value: %s\n"), cmdline.SwitchHasValue(_T("a")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'b' has value: %s\n"), cmdline.SwitchHasValue(_T("b")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'c' has value: %s\n"), cmdline.SwitchHasValue(_T("c")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'd' has value: %s\n"), cmdline.SwitchHasValue(_T("d")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'e' has value: %s\n"), cmdline.SwitchHasValue(_T("e")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'efg' has value: %s\n"), cmdline.SwitchHasValue(_T("efg")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Switch 'h' has value: %s\n"), cmdline.SwitchHasValue(_T("h")) ? _T("yes") : _T("no")); 
 
	_tprintf(_T("Value of 'a' is 'x': %s\n"), cmdline.SwitchValueIs(_T("a"), _T("x")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Value of 'a' is 'b': %s\n"), cmdline.SwitchValueIs(_T("a"), _T("b")) ? _T("yes") : _T("no")); 
	_tprintf(_T("Value of 'c' is '3': %s\n"), cmdline.SwitchValueIs(_T("c"), 3) ? _T("yes") : _T("no")); 
	_tprintf(_T("Value of 'c' is '2': %s\n"), cmdline.SwitchValueIs(_T("c"), 2) ? _T("yes") : _T("no")); 
 
	TCHAR szVal[MAX_PATH]; 
	 
	cmdline.SwitchValue(_T("d"), szVal); 
	_tprintf(_T("Value of 'd': %s\n"), szVal); 
 
	double dVal = 0; 
 
	cmdline.SwitchValue(_T("efg"), &dVal); 
	_tprintf(_T("Value of 'efg': %g\n"), dVal); 
 
	_tprintf(_T("Value of marked argument: %s\n"), cmdline.MarkedArgument('@')); 
 
	_tprintf(_T("Argument after 'h': %s\n"), cmdline.ArgumentAfter(_T("h"))); 
 
} 
 
// Raw parser 
void TestCase6() 
{ 
 
	// Empty command line. Count() returns zero. 
	CommandLine cmdline1(true, _T(""), '/', ':'); 
	CommandLine cmdline2(true, _T(""), '-', 0x20); 
 
	TCHAR nameBuffer[CMDLINE_MAX_SWITCH]; 
	TCHAR valueBuffer[CMDLINE_MAX_VALUE]; 
	bool result = false; 
	bool hasValue = false; 
 
	TCHAR* rawStrings1[] = { _T("/r:one"), _T("/r one"), _T("/r"), _T("r"), _T("-r")}; 
	TCHAR* rawStrings2[] = { _T("-r one"), _T("-r:one"), _T("-r \"one two\""), _T("r"), _T("/r")}; 
 
	TCHAR** strings[] = {rawStrings1, rawStrings2}; 
	CommandLine* parsers[] = {&cmdline1, &cmdline2}; 
 
	const int stringCount = 5; 
	const int testCount = 2; 
 
	for (int n = 0; n < testCount; n++) 
	{ 
		_tprintf(_T("Test strings %i\n"), n + 1); 
		 
		for (int i = 0; i < stringCount; i++) 
		{ 
			lstrcpy(nameBuffer, _T("")); 
			lstrcpy(valueBuffer, _T("")); 
 
			_tprintf(_T(" [%i] Raw string: %s\n"), i + 1, strings[n][i]); 
			result = parsers[n]->Parse(strings[n][i], nameBuffer, hasValue, valueBuffer); 
 
			if (result) 
			{ 
				_tprintf(_T("  Name: %s | Has value: %s | Value: %s\n\n"),  
						nameBuffer,  
						hasValue == true ? _T("yes") : _T("no"),  
						hasValue == true ? valueBuffer : _T("n/a")); 
			} 
			else 
			{ 
				_tprintf(_T("  The string could not be parsed as a switch.\n\n")); 
			} 
 
		} 
 
	} 
 
 
 
} 
 
// Initializing value marker with 0x0 
void TestCase7() 
{ 
	TCHAR* szCommandLine = _T("-a b -c de"); 
 
	CommandLine cmdline(true, szCommandLine, '-', 0x0); 
 
	TCHAR szValue[MAX_PATH]; 
	 
    _tprintf(_T("Full command line:\n  %s\n\n"), szCommandLine); 
 
	if (!cmdline.SwitchValue(_T("a"), szValue)) 
		lstrcpy(szValue, _T("UNDEFINED")); 
 
	_tprintf(_T("Value of 'a': %s\n"), szValue); 
	_tprintf(_T("Argument afer 'a': %s\n"), cmdline.ArgumentAfter(_T("a"))); 
 
 
	if (!cmdline.SwitchValue(_T("c"), szValue)) 
		lstrcpy(szValue, _T("UNDEFINED")); 
 
	_tprintf(_T("Value of 'c': %s\n"), szValue); 
	_tprintf(_T("Argument afer 'c': %s\n"), cmdline.ArgumentAfter(_T("c"))); 
 
 
} 
 
// IndexesOf, ValueOf 
void TestCase8() 
{ 
 
	TCHAR* szCommandLine = _T("-a:b -x one -c -x two -d -x three -e -x four"); 
 
	CommandLine cmdline(true, szCommandLine, '-', ':'); 
 
	_tprintf(_T("Full command line:\n  %s\n\n"), szCommandLine); 
 
	int* indexes = NULL; 
 
	int count = cmdline.CountOf(_T("x")); 
 
	if (count) 
	{ 
		int* indexes = new int[count]; 
		cmdline.IndexesOf(_T("x"), indexes); 
 
		_tprintf(_T("%i instances of switch x were found.\n\n"), count); 
 
		for (int i = 0; i < count; i++) 
		{ 
			TCHAR value[CMDLINE_MAX_VALUE]; 
            // Note that we specify that the value is an undelimited argument 
			if (cmdline.ValueOf(indexes[i], value, true)) 
			{ 
				_tprintf(_T("Instance %i of switch x at position %i: %s\n"), i + 1, indexes[i], value); 
			} 
			else 
			{ 
				_tprintf(_T("Instance %i of switch x at position %i has no value\n"), i + 1, indexes[i]); 
			} 
 
		} 
 
		delete[] indexes; 
 
	} 
	else 
	{ 
		_tprintf(_T("No instances of switch x were found.\n\n")); 
	} 
 
 
} 
 
typedef void (__cdecl FAR* TEST_CASE) (void); 
typedef TEST_CASE LPTEST_CASE; 
 
static LPTEST_CASE test_cases[] =  
{ 
	static_cast(&TestCase1), 
	static_cast(&TestCase2), 
	static_cast(&TestCase3), 
	static_cast(&TestCase4), 
	static_cast(&TestCase5), 
	static_cast(&TestCase6), 
	static_cast(&TestCase7), 
	static_cast(&TestCase8), 
}; 
 
 
static int case_count = (sizeof(test_cases) / sizeof(test_cases[0])); 
 
int _tmain(int argc, TCHAR* argv[]) 
{ 
 
	while (1) 
	{ 
 
		_tprintf(_T("\nEnter case number to run, or 'x' to exit: ")); 
		int n = _getch(); 
 
		_putch(n); 
 
		_tprintf(_T("\n")); 
 
		if (n == 'x' || n == 'X') 
			break; 
 
		if (_istdigit(n)) 
		{ 
 
			int nCase = n - 48;			// hmmmm. 
 
			if (nCase > 0 && nCase <= case_count) 
			{ 
				LPTEST_CASE test_case = test_cases[nCase-1]; 
				_tprintf(_T("Running test case %i\n\n"), nCase); 
				 
				(test_case)(); 
				 
				_tprintf(_T("\n\n--------------------------------\n\n")); 
			} 
			else 
			{ 
				_tprintf(_T("Invalid selection.\n")); 
			} 
 
		} 
		else 
		{ 
			_tprintf(_T("Invalid selection.\n")); 
		} 
 
	} 
 
	return 0; 
 
}