main.cpp
Go to the documentation of this file.
1 
2 /*
3  Copyright (c) 2009 by Chad Nelson
4  Released under the MIT License.
5  See the provided LICENSE.TXT file for details.
6 */
7 
8 #include <iostream>
9 #include <sstream>
10 #include <fstream>
11 #include <vector>
12 #include <string>
13 
14 #include <boost/optional.hpp>
15 //#include <boost/regex.hpp> // For the 'test' option
16 
17 #include "markdown.h"
18 
19 using std::cerr;
20 using std::cout;
21 using std::endl;
22 
23 namespace
24 {
25 
26  class Options
27  {
28  public:
29  Options(): mDebug(false), mTest(false) { }
30 
31  bool readOptions(int argc, char* argv[]);
32 
33  void showHelp();
34 
35  bool debug() const
36  {
37  return mDebug;
38  }
39  bool test() const
40  {
41  return mTest;
42  }
43  std::string inputFile() const
44  {
45  return mInputFile;
46  }
47 
48  private:
49  std::string mInputFile;
50  bool mDebug, mTest;
51  };
52 
53  bool Options::readOptions(int argc, char* argv[])
54  {
55  bool help = false, err = false;
56 
57  for (int x = 1; x < argc && !help && !err; ++x)
58  {
59  if (argv[x][0] == '-' && argv[x][1] == '-' && argv[x][2] != 0)
60  {
61  // It's a full-word option.
62  std::string opt(argv[x] + 2);
63 
64  if (opt == "debug")
65  {
66  mDebug = true;
67  }
68  else if (opt == "test")
69  {
70  mTest = true;
71  }
72  else if (opt == "help")
73  {
74  help = true;
75  }
76  else
77  {
78  err = true;
79  cerr << "Unknown option " << argv[x] << ", use -? for help."
80  << endl;
81  }
82  }
83  else if (argv[x][0] == '-')
84  {
85  // It's one or more option flags.
86  const char* i = argv[x] + 1;
87 
88  while (*i != 0 && !help && !err)
89  {
90  switch (*i)
91  {
92  case '?':
93  help = true;
94  break;
95 
96  case 'd':
97  mDebug = true;
98  break;
99 
100  default:
101  err = true;
102  cerr << "Uknown option flag '" << *i << "', use -? for "
103  "help." << endl;
104  }
105 
106  ++i;
107  }
108  }
109  else
110  {
111  // It's a parameter.
112  if (mInputFile.empty())
113  {
114  mInputFile = argv[x];
115  }
116  else
117  {
118  err = true;
119  cerr << "Too many parameters. Already had '" << mInputFile
120  << "', found '" << argv[x] << "' too. Use -? for help."
121  << endl;
122  }
123  }
124  }
125 
126  if (help)
127  {
128  showHelp();
129  return false;
130  }
131  else if (err)
132  {
133  return false;
134  }
135  else
136  {
137  return true;
138  }
139  }
140 
141  void Options::showHelp()
142  {
143  const char* cHelpScreen =
144  "This program converts input (from an input file or stdin) from Markdown syntax\n"
145  "to HTML using the cpp-markdown library.\n"
146  "\n"
147  "Usage:\n"
148  " cpp-markdown [<option>...] [input-file]\n"
149  "\n"
150  "Available options are:\n"
151  " -?, --help Show this screen.\n"
152  " -d, --debug Show tokens instead of HTML output.\n";
153  cerr << endl << cHelpScreen << endl;
154  }
155 
156 } // namespace
157 
158 int main(int argc, char* argv[])
159 {
160  Options cfg;
161 
162  if (!cfg.readOptions(argc, argv))
163  {
164  return 1;
165  }
166 
167  // if (cfg.test()) {
168  // return 1;
169  // }
170 
171  std::ifstream ifile;
172 
173  std::istream* in = &std::cin;
174 
175  if (!cfg.inputFile().empty())
176  {
177  cerr << "Reading file '" << cfg.inputFile() << "'..." << endl;
178  ifile.open(cfg.inputFile().c_str());
179 
180  if (!ifile)
181  {
182  cerr << "Error: Can't open file." << endl;
183  return 1;
184  }
185  else
186  {
187  in = &ifile;
188  }
189  }
190  else
191  {
192  cerr << "Reading standard input..." << endl;
193  }
194 
195  markdown::Document doc;
196  doc.read(*in);
197 
198  if (cfg.debug())
199  {
200  doc.writeTokens(cout);
201  }
202  else
203  {
204  doc.write(cout);
205  }
206 
207  return 0;
208 }
markdown::Document
Definition: markdown.h:24
markdown::Document::writeTokens
void writeTokens(std::ostream &)
Definition: markdown.cpp:997
markdown.h
markdown::Document::read
bool read(const std::string &)
Definition: markdown.cpp:906
main
int main(int argc, char *argv[])
Definition: main.cpp:31
markdown::Document::write
void write(std::ostream &)
Definition: markdown.cpp:991
armarx::ApplicationOptions::showHelp
void showHelp(ApplicationPtr application, ArmarXDummyManagerPtr dummyManager, Options options, Ice::PropertiesPtr properties, std::ostream &out=std::cout)
Prints help according to the format selection in options.
Definition: ApplicationOptions.cpp:169