ENH: Added support for non-verbose mode output from running a command. This can be used when it is expected that the command may fail.

This commit is contained in:
Brad King 2001-07-23 11:53:52 -04:00
parent dc87e1906d
commit 572ecc9b8a
2 changed files with 24 additions and 10 deletions

View File

@ -805,7 +805,8 @@ bool cmSystemTools::IsOff(const char* val)
bool cmSystemTools::RunCommand(const char* command, bool cmSystemTools::RunCommand(const char* command,
std::string& output) std::string& output,
bool verbose)
{ {
const int BUFFER_SIZE = 4096; const int BUFFER_SIZE = 4096;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
@ -820,12 +821,15 @@ bool cmSystemTools::RunCommand(const char* command,
std::ifstream fin(tempFile.c_str()); std::ifstream fin(tempFile.c_str());
if(!fin) if(!fin)
{ {
std::string errormsg = "RunCommand produced no output: command: \""; if(verbose)
errormsg += command; {
errormsg += "\""; std::string errormsg = "RunCommand produced no output: command: \"";
errormsg += "\nOutput file: "; errormsg += command;
errormsg += tempFile; errormsg += "\"";
cmSystemTools::Error(errormsg.c_str()); errormsg += "\nOutput file: ";
errormsg += tempFile;
cmSystemTools::Error(errormsg.c_str());
}
fin.close(); fin.close();
cmSystemTools::RemoveFile(tempFile.c_str()); cmSystemTools::RemoveFile(tempFile.c_str());
return false; return false;
@ -839,7 +843,10 @@ bool cmSystemTools::RunCommand(const char* command,
cmSystemTools::RemoveFile(tempFile.c_str()); cmSystemTools::RemoveFile(tempFile.c_str());
return true; return true;
#else #else
std::cout << "running " << command << std::endl; if(verbose)
{
std::cout << "running " << command << std::endl;
}
FILE* cpipe = popen(command, "r"); FILE* cpipe = popen(command, "r");
if(!cpipe) if(!cpipe)
{ {
@ -848,7 +855,10 @@ bool cmSystemTools::RunCommand(const char* command,
fgets(buffer, BUFFER_SIZE, cpipe); fgets(buffer, BUFFER_SIZE, cpipe);
while(!feof(cpipe)) while(!feof(cpipe))
{ {
std::cout << buffer << std::flush; if(verbose)
{
std::cout << buffer << std::flush;
}
output += buffer; output += buffer;
fgets(buffer, BUFFER_SIZE, cpipe); fgets(buffer, BUFFER_SIZE, cpipe);
} }

View File

@ -245,8 +245,12 @@ public:
* Run an executable command and put the stdout in output. * Run an executable command and put the stdout in output.
* A temporary file is created in the binaryDir for storing the * A temporary file is created in the binaryDir for storing the
* output because windows does not have popen. * output because windows does not have popen.
*
* If verbose is false, no user-viewable output from the program
* being run will be generated.
*/ */
static bool RunCommand(const char* command, std::string& output); static bool RunCommand(const char* command, std::string& output,
bool verbose = true);
///! Generate a temporary file name ///! Generate a temporary file name
static std::string TemporaryFileName(); static std::string TemporaryFileName();