[Scummvm-cvs-logs] SF.net SVN: scummvm:[42878] tools/branches/gsoc2009-gui

remere at users.sourceforge.net remere at users.sourceforge.net
Wed Jul 29 04:09:02 CEST 2009


Revision: 42878
          http://scummvm.svn.sourceforge.net/scummvm/?rev=42878&view=rev
Author:   remere
Date:     2009-07-29 02:09:01 +0000 (Wed, 29 Jul 2009)

Log Message:
-----------
*Greatly increased robustness of parsing arguments, error messages should now make sense in most cases.
*Tool specific arguments are now entered before the filename, as intended.

Modified Paths:
--------------
    tools/branches/gsoc2009-gui/compress.cpp
    tools/branches/gsoc2009-gui/tool.cpp
    tools/branches/gsoc2009-gui/tools_cli.cpp

Modified: tools/branches/gsoc2009-gui/compress.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress.cpp	2009-07-29 02:05:56 UTC (rev 42877)
+++ tools/branches/gsoc2009-gui/compress.cpp	2009-07-29 02:09:01 UTC (rev 42878)
@@ -757,6 +757,8 @@
 	return true;
 }
 
+#include <iostream>
+
 bool CompressionTool::processOggParms() {
 	while (_arguments_parsed < _arguments.size()) {
 		std::string arg = _arguments[_arguments_parsed];
@@ -766,6 +768,7 @@
 			if (_arguments_parsed >= _arguments.size())
 				throw ToolException("Could not parse command line options, expected value after -b");
 			oggparms.nominalBitr = atoi(_arguments[_arguments_parsed].c_str());
+			std::cout << "Parsed b=" << oggparms.nominalBitr << "\n";
 
 			if ((oggparms.nominalBitr % 8) != 0) {
 				oggparms.nominalBitr -= oggparms.nominalBitr % 8;
@@ -899,8 +902,6 @@
 	++_arguments_parsed;
 
 	// Need workaround to be sign-correct
-	int arg = (int)_arguments_parsed;
-
 	switch (_format) {
 	case AUDIO_MP3:
 		tempEncoded = TEMP_MP3;
@@ -920,7 +921,6 @@
 	default: // cannot occur but we check anyway to avoid compiler warnings
 		throw ToolException("Unknown audio format, should be impossible!");
 	}
-	_arguments_parsed = (size_t)arg;
 }
 
 std::string CompressionTool::getHelp() const {

Modified: tools/branches/gsoc2009-gui/tool.cpp
===================================================================
--- tools/branches/gsoc2009-gui/tool.cpp	2009-07-29 02:05:56 UTC (rev 42877)
+++ tools/branches/gsoc2009-gui/tool.cpp	2009-07-29 02:09:01 UTC (rev 42878)
@@ -22,6 +22,8 @@
 
 
 #include <stdarg.h>
+#include <iostream>
+#include <sstream>
 
 #include "util.h"
 #include "tool.h"
@@ -85,9 +87,14 @@
 	// Read tool specific arguments
 	parseExtraArguments();
 
+	if (_arguments.size() && _arguments[_arguments_parsed][0] == '-') {
+		std::string s = "Possibly ignored option " + _arguments[_arguments_parsed] + ".";
+		print(s.c_str());
+	}
+
 	// Read input files from CLI
 	for(ToolInputs::iterator iter = _inputPaths.begin(); iter != _inputPaths.end(); ++iter) {
-		if(_arguments_parsed > _inputPaths.size()) {
+		if(_arguments.size() - _arguments_parsed < _inputPaths.size()) {
 			print("Too few input files!");
 			return -2;
 		}
@@ -106,8 +113,14 @@
 		iter->path = in;
 	}
 
-	if(_arguments_parsed < _arguments.size()) {
-		print("Too many input files!");
+	// We should have parsed all arguments by now
+	if(_arguments_parsed < _arguments.size() - _inputPaths.size()) {
+		std::ostringstream os;
+		os << "Too many inputs files ( ";
+		while (_arguments_parsed < _arguments.size())
+			os << "'" << _arguments[_arguments_parsed++] << "' ";
+		os << ")\n";
+		print(os.str().c_str());
 		return -2;
 	}
 

Modified: tools/branches/gsoc2009-gui/tools_cli.cpp
===================================================================
--- tools/branches/gsoc2009-gui/tools_cli.cpp	2009-07-29 02:05:56 UTC (rev 42877)
+++ tools/branches/gsoc2009-gui/tools_cli.cpp	2009-07-29 02:09:01 UTC (rev 42878)
@@ -21,6 +21,7 @@
  */
 
 #include <iostream>
+#include <algorithm>
 
 #include "tools_cli.h"
 
@@ -80,29 +81,64 @@
 	} else if (option == "--list" || option == "-l") {
 		printTools();
 	} else {
-		// Allow user to the narrow choices
-		if(option == "compress") {
+		ToolList choices;
+		std::deque<std::string>::reverse_iterator reader = arguments.rbegin();
+		std::deque<std::string>::iterator hint_arg;
+		std::string infile;
+		
+		hint_arg = std::find(arguments.begin(), arguments.end(), "compress");
+		if (hint_arg != arguments.end()) {
 			type = TOOLTYPE_COMPRESSION;
-			arguments.pop_front();
-		} else if(option == "extract") {
-			type = TOOLTYPE_EXTRACTION;
-			arguments.pop_front();
+		} else {
+			hint_arg = std::find(arguments.begin(), arguments.end(), "extract");
+			if (hint_arg != arguments.end())
+				type = TOOLTYPE_EXTRACTION;
 		}
 
-		// Only possible if compress/extract was parsed
-		if (arguments.empty())
-			std::cout << "\tExpected more arguments after '" << option << "'\n";
+		while (reader != arguments.rend()) {
+			//std::cout << "Checking backarg " << *reader << "\n";
+			if (hint_arg != arguments.end() && *reader == *hint_arg) {
+				//std::cout << "It is the hint! begin anew" << "\n";
+				// It seems hint_arg was an input file, start over but with generic file type
+				type = TOOLTYPE_ALL;
+				reader = arguments.rbegin();
+				hint_arg = arguments.end();
+				continue;
+			}
 
+			// It must be a filename now
+			choices = inspectInput(type, *reader);
+			
+			// If anything matched, we stop
+			if (choices.size() > 0) {
+				infile = *reader;
+				break;
+			}
+			++reader;
+		}
+		if (hint_arg != arguments.end()) {
+			// Remove hint as it's not used after this, and can't be in tool CLI
+			arguments.erase(hint_arg);
+
+			// Only possible if compress/extract was parsed
+			if (arguments.empty())
+				// compress was the arg removed so...
+				std::cout << "\tExpected more arguments after '" << option << "'\n";
+		}
+
+		// This should never happen, as args are only removed if we used compress|extract
+		assert(arguments.empty() == false);
+
 		// Find out what tools take this file as input
-		ToolList choices = inspectInput(type, arguments.front());
 		Tool *tool = NULL;
 
 		if (choices.empty()) {
 			std::cout << "\tNo tool could parse input file '" << arguments.front() << "', use --list to list all available tools and --tool to force running the correct one.\n";
-			std::cout << "\tIf you intended to specify tool-specific options, do so AFTER the input file.\n\n";
-			std::cout << "\tExample: tools_cli monster.sou --vorbis\n";
 			return 0;
 		} else if (choices.size() > 1) {
+			if (infile.size() && infile[0] == '-')
+				std::cout << "\tWARNING: Input file '" << infile << "' looks like an argument, is this what you wanted?\n";
+
 			std::cout << "\tMultiple tools accept this input:\n\n";
 
 			// Present a list of possible tools
@@ -111,7 +147,7 @@
 				std::cout << "\t" << i << ") " << (*choice)->getName() << "\n";
 			}
 
-			std::cout << "Which tool to use: ";
+			std::cout << "Which tool to use ('q' to abort): ";
 			i = 0;
 			while(true) {
 
@@ -122,13 +158,15 @@
 				if(std::cin && i >= 1 && (size_t)i < choices.size())
 					break;
 
-				std::cout << "Invalid input, try again: ";
-
 				// Clear any error flags
 				std::cin.clear();
+				
+				std::string q;
+				std::cin >> q;
+				if (q == "q" || q == "exit" || q == "quit" || q == "abort")
+					return 0;
 
-				// Skip invalid input characters
-				std::cin.ignore(1000, '\n');
+				std::cout << "Invalid input, try again: ";
 			}
 
 			// Account for the fact arrays start at 0
@@ -152,9 +190,8 @@
 		"\tScumm VM Tools master interface\n" <<
 		"\n" <<
 		"\tCommon use:\n" <<
-		"\ttools [--tool <tool name>] [compression options] [-o output directory] [tool args] <input args>\n" <<
-		"\ttools [extract|compress] <input args> [tool args]\n" <<
-		"\tNote that on the second form, tool arguments are specified AFTER the input file.\n" <<
+		"\ttools [--tool <tool name>] [tool-specific options] [-o <output directory>] <input files>\n" <<
+		"\ttools [tool-specific option] [extract|compress] <input files>\n" <<
 		"\n" <<
 		"\tOther Options:\n" <<
 		"\t--help\tDisplay this text\n" <<


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list