main.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2007-2012 by Johan De Taeye, frePPLe bvba *
4  * *
5  * This library is free software; you can redistribute it and/or modify it *
6  * under the terms of the GNU Affero General Public License as published *
7  * by the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU Affero General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Affero General Public *
16  * License along with this program. *
17  * If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  ***************************************************************************/
20 
21 #include "freppleinterface.h"
22 #include <iostream>
23 #include <sstream>
24 #include <cstring>
25 #include <cstdlib>
26 using namespace std;
27 
28 
29 void usage()
30 {
31  cout << "\nfrePPLe v" << FreppleVersion() << " command line application\n"
32  "\nUsage:\n"
33  " frepple [options] [files | directories]\n"
34  "\nThis program reads XML input data, and executes the modeling and\n"
35  "planning commands included in them.\n"
36  "The XML input can be provided in the following ways:\n"
37  " - Passing one or more XML files and/or directories as arguments.\n"
38  " When a directory is specified, the application will process\n"
39  " all files with the extension '.xml'.\n"
40  " - Passing one or more Python files with the extension '.py'\n"
41  " The Python commands are executed in the embedded interpreter.\n"
42  " - When passing no file or directory arguments, input will be read\n"
43  " from the standard input. XML data can be piped to the application.\n"
44  "\nOptions:\n"
45  " -validate -v Validate the XML input for correctness.\n"
46  " -check -c Only validate the input, without executing the content.\n"
47  " -? -h -help Show these instructions.\n"
48  "\nEnvironment: The variable FREPPLE_HOME optionally points to a\n"
49  " directory where the initialization files init.xml, init.py,\n"
50  " frepple.xsd and module libraries will be searched.\n"
51  "\nReturn codes: 0 when successful, non-zero in case of errors\n"
52  "\nMore information on this program: http://www.frepple.com\n\n"
53  << endl;
54 }
55 
56 
57 int main (int argc, char *argv[])
58 {
59 
60  // Storing the chosen options...
61  bool validate = false;
62  bool validate_only = false;
63  bool input = false;
64 
65  try
66  {
67  // Analyze the command line arguments.
68  for (int i = 1; i < argc; ++i)
69  {
70  if (argv[i][0] == '-')
71  {
72  // An option on the command line
73  if (!strcmp(argv[i],"-validate") || !strcmp(argv[i],"-v"))
74  validate = true;
75  else if (!strcmp(argv[i],"-check") || !strcmp(argv[i],"-c"))
76  validate_only = true;
77  else
78  {
79  if (strcmp(argv[i],"-?")
80  && strcmp(argv[i],"-h")
81  && strcmp(argv[i],"-help"))
82  cout << "\nError: Option '" << argv[i]
83  << "' not recognized." << endl;
84  usage();
85  return EXIT_FAILURE;
86  }
87  }
88  else
89  {
90  // A file or directory name on the command line
91  if (!input)
92  {
93  // Initialize the library if this wasn't done before
94  FreppleInitialize(argc, argv);
95  input = true;
96  }
97  if (strlen(argv[i])>=3 && !strcmp(argv[i]+strlen(argv[i])-3,".py"))
98  // Execute as Python file
99  FreppleReadPythonFile(argv[i]);
100  else
101  // Execute as XML file
102  FreppleReadXMLFile(argv[i], validate, validate_only);
103  }
104  }
105 
106  // When no filenames are specified, we read the standard input
107  if (!input)
108  {
109  FreppleInitialize(argc, argv);
110  FreppleReadXMLFile(NULL, validate, validate_only);
111  }
112  }
113  catch (const exception& e)
114  {
115  ostringstream ch;
116  ch << "Error: " << e.what();
117  FreppleLog(ch.str());
118  return EXIT_FAILURE;
119  }
120  catch (...)
121  {
122  FreppleLog("Error: Unknown exception type");
123  return EXIT_FAILURE;
124  }
125  return EXIT_SUCCESS;
126 }