main.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.9.1/src/main.cpp $
00003   version : $LastChangedRevision: 1656 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2012-03-27 19:05:34 +0200 (Tue, 27 Mar 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 Lesser General Public License as published   *
00013  * by the Free Software Foundation; either version 2.1 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 GNU Lesser *
00019  * General Public License for more details.                                *
00020  *                                                                         *
00021  * You should have received a copy of the GNU Lesser General Public        *
00022  * License along with this library; if not, write to the Free Software     *
00023  * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 *
00024  * USA                                                                     *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 #include "freppleinterface.h"
00029 #include <iostream>
00030 #include <sstream>
00031 #include <cstring>
00032 #include <cstdlib>
00033 using namespace std;
00034 
00035 
00036 void usage()
00037 {
00038   cout << "\nfrePPLe v" << FreppleVersion() << " command line application\n"
00039       "\nUsage:\n"
00040       "  frepple [options] [files | directories]\n"
00041       "\nThis program reads XML input data, and executes the modeling and\n"
00042       "planning commands included in them.\n"
00043       "The XML input can be provided in the following ways:\n"
00044       "  - Passing one or more XML files and/or directories as arguments.\n"
00045       "    When a directory is specified, the application will process\n"
00046       "    all files with the extension '.xml'.\n"
00047       "  - Passing one or more Python files with the extension '.py'\n"
00048       "    The Python commands are executed in the embedded interpreter.\n"
00049       "  - When passing no file or directory arguments, input will be read\n"
00050       "    from the standard input. XML data can be piped to the application.\n"
00051       "\nOptions:\n"
00052       "  -validate -v  Validate the XML input for correctness.\n"
00053       "  -check -c     Only validate the input, without executing the content.\n"
00054       "  -? -h -help   Show these instructions.\n"
00055       "\nEnvironment: The variable FREPPLE_HOME optionally points to a\n"
00056       "     directory where the initialization files init.xml, init.py,\n"
00057       "     frepple.xsd and module libraries will be searched.\n"
00058       "\nReturn codes: 0 when successful, non-zero in case of errors\n"
00059       "\nMore information on this program: http://www.frepple.com\n\n"
00060       << endl;
00061 }
00062 
00063 
00064 int main (int argc, char *argv[])
00065 {
00066 
00067   // Storing the chosen options...
00068   bool validate = false;
00069   bool validate_only = false;
00070   bool input = false;
00071 
00072   try
00073   {
00074     // Analyze the command line arguments.
00075     for (int i = 1; i < argc; ++i)
00076     {
00077       if (argv[i][0] == '-')
00078       {
00079         // An option on the command line
00080         if (!strcmp(argv[i],"-validate") || !strcmp(argv[i],"-v"))
00081           validate = true;
00082         else if (!strcmp(argv[i],"-check") || !strcmp(argv[i],"-c"))
00083           validate_only = true;
00084         else
00085         {
00086           if (strcmp(argv[i],"-?")
00087               && strcmp(argv[i],"-h")
00088               && strcmp(argv[i],"-help"))
00089             cout << "\nError: Option '" << argv[i]
00090                 << "' not recognized." << endl;
00091           usage();
00092           return EXIT_FAILURE;
00093         }
00094       }
00095       else
00096       {
00097         // A file or directory name on the command line
00098         if (!input)
00099         {
00100           // Initialize the library if this wasn't done before
00101           FreppleInitialize(argc, argv);
00102           input = true;
00103         }
00104         if (strlen(argv[i])>=3 && !strcmp(argv[i]+strlen(argv[i])-3,".py"))
00105           // Execute as Python file
00106           FreppleReadPythonFile(argv[i]);
00107         else
00108           // Execute as XML file
00109           FreppleReadXMLFile(argv[i], validate, validate_only);
00110       }
00111     }
00112 
00113     // When no filenames are specified, we read the standard input
00114     if (!input)
00115     {
00116       FreppleInitialize(argc, argv);
00117       FreppleReadXMLFile(NULL, validate, validate_only);
00118     }
00119   }
00120   catch (const exception& e)
00121   {
00122     ostringstream ch;
00123     ch << "Error: " << e.what();
00124     FreppleLog(ch.str());
00125     return EXIT_FAILURE;
00126   }
00127   catch (...)
00128   {
00129     FreppleLog("Error: Unknown exception type");
00130     return EXIT_FAILURE;
00131   }
00132   return EXIT_SUCCESS;
00133 }

Documentation generated for frePPLe by  doxygen