main.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: http://svn.code.sf.net/p/frepple/code/trunk/src/main.cpp $
00003   version : $LastChangedRevision: 1713 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2012-07-18 11:46:01 +0200 (Wed, 18 Jul 2012) $
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  * Copyright (C) 2007-2012 by Johan De Taeye, frePPLe bvba                 *
00010  *                                                                         *
00011  * This library is free software; you can redistribute it and/or modify it *
00012  * under the terms of the GNU Affero General Public License as published   *
00013  * by the Free Software Foundation; either version 3 of the License, or    *
00014  * (at your option) any later version.                                     *
00015  *                                                                         *
00016  * This library is distributed in the hope that it will be useful,         *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
00019  * GNU Affero General Public License for more details.                     *
00020  *                                                                         *
00021  * You should have received a copy of the GNU Affero General Public        *
00022  * License along with this program.                                        *
00023  * If not, see <http://www.gnu.org/licenses/>.                             *
00024  *                                                                         *
00025  ***************************************************************************/
00026 
00027 #include "freppleinterface.h"
00028 #include <iostream>
00029 #include <sstream>
00030 #include <cstring>
00031 #include <cstdlib>
00032 using namespace std;
00033 
00034 
00035 void usage()
00036 {
00037   cout << "\nfrePPLe v" << FreppleVersion() << " command line application\n"
00038       "\nUsage:\n"
00039       "  frepple [options] [files | directories]\n"
00040       "\nThis program reads XML input data, and executes the modeling and\n"
00041       "planning commands included in them.\n"
00042       "The XML input can be provided in the following ways:\n"
00043       "  - Passing one or more XML files and/or directories as arguments.\n"
00044       "    When a directory is specified, the application will process\n"
00045       "    all files with the extension '.xml'.\n"
00046       "  - Passing one or more Python files with the extension '.py'\n"
00047       "    The Python commands are executed in the embedded interpreter.\n"
00048       "  - When passing no file or directory arguments, input will be read\n"
00049       "    from the standard input. XML data can be piped to the application.\n"
00050       "\nOptions:\n"
00051       "  -validate -v  Validate the XML input for correctness.\n"
00052       "  -check -c     Only validate the input, without executing the content.\n"
00053       "  -? -h -help   Show these instructions.\n"
00054       "\nEnvironment: The variable FREPPLE_HOME optionally points to a\n"
00055       "     directory where the initialization files init.xml, init.py,\n"
00056       "     frepple.xsd and module libraries will be searched.\n"
00057       "\nReturn codes: 0 when successful, non-zero in case of errors\n"
00058       "\nMore information on this program: http://www.frepple.com\n\n"
00059       << endl;
00060 }
00061 
00062 
00063 int main (int argc, char *argv[])
00064 {
00065 
00066   // Storing the chosen options...
00067   bool validate = false;
00068   bool validate_only = false;
00069   bool input = false;
00070 
00071   try
00072   {
00073     // Analyze the command line arguments.
00074     for (int i = 1; i < argc; ++i)
00075     {
00076       if (argv[i][0] == '-')
00077       {
00078         // An option on the command line
00079         if (!strcmp(argv[i],"-validate") || !strcmp(argv[i],"-v"))
00080           validate = true;
00081         else if (!strcmp(argv[i],"-check") || !strcmp(argv[i],"-c"))
00082           validate_only = true;
00083         else
00084         {
00085           if (strcmp(argv[i],"-?")
00086               && strcmp(argv[i],"-h")
00087               && strcmp(argv[i],"-help"))
00088             cout << "\nError: Option '" << argv[i]
00089                 << "' not recognized." << endl;
00090           usage();
00091           return EXIT_FAILURE;
00092         }
00093       }
00094       else
00095       {
00096         // A file or directory name on the command line
00097         if (!input)
00098         {
00099           // Initialize the library if this wasn't done before
00100           FreppleInitialize(argc, argv);
00101           input = true;
00102         }
00103         if (strlen(argv[i])>=3 && !strcmp(argv[i]+strlen(argv[i])-3,".py"))
00104           // Execute as Python file
00105           FreppleReadPythonFile(argv[i]);
00106         else
00107           // Execute as XML file
00108           FreppleReadXMLFile(argv[i], validate, validate_only);
00109       }
00110     }
00111 
00112     // When no filenames are specified, we read the standard input
00113     if (!input)
00114     {
00115       FreppleInitialize(argc, argv);
00116       FreppleReadXMLFile(NULL, validate, validate_only);
00117     }
00118   }
00119   catch (const exception& e)
00120   {
00121     ostringstream ch;
00122     ch << "Error: " << e.what();
00123     FreppleLog(ch.str());
00124     return EXIT_FAILURE;
00125   }
00126   catch (...)
00127   {
00128     FreppleLog("Error: Unknown exception type");
00129     return EXIT_FAILURE;
00130   }
00131   return EXIT_SUCCESS;
00132 }

Documentation generated for frePPLe by  doxygen