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 <fstream>
9 #include <iostream>
10 #include <sstream>
11 #include <string>
12 #include <vector>
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  }
32 
33  bool readOptions(int argc, char* argv[]);
34 
35  void showHelp();
36 
37  bool
38  debug() const
39  {
40  return mDebug;
41  }
42 
43  bool
44  test() const
45  {
46  return mTest;
47  }
48 
49  std::string
50  inputFile() const
51  {
52  return mInputFile;
53  }
54 
55  private:
56  std::string mInputFile;
57  bool mDebug, mTest;
58  };
59 
60  bool
61  Options::readOptions(int argc, char* argv[])
62  {
63  bool help = false, err = false;
64 
65  for (int x = 1; x < argc && !help && !err; ++x)
66  {
67  if (argv[x][0] == '-' && argv[x][1] == '-' && argv[x][2] != 0)
68  {
69  // It's a full-word option.
70  std::string opt(argv[x] + 2);
71 
72  if (opt == "debug")
73  {
74  mDebug = true;
75  }
76  else if (opt == "test")
77  {
78  mTest = true;
79  }
80  else if (opt == "help")
81  {
82  help = true;
83  }
84  else
85  {
86  err = true;
87  cerr << "Unknown option " << argv[x] << ", use -? for help." << endl;
88  }
89  }
90  else if (argv[x][0] == '-')
91  {
92  // It's one or more option flags.
93  const char* i = argv[x] + 1;
94 
95  while (*i != 0 && !help && !err)
96  {
97  switch (*i)
98  {
99  case '?':
100  help = true;
101  break;
102 
103  case 'd':
104  mDebug = true;
105  break;
106 
107  default:
108  err = true;
109  cerr << "Uknown option flag '" << *i
110  << "', use -? for "
111  "help."
112  << endl;
113  }
114 
115  ++i;
116  }
117  }
118  else
119  {
120  // It's a parameter.
121  if (mInputFile.empty())
122  {
123  mInputFile = argv[x];
124  }
125  else
126  {
127  err = true;
128  cerr << "Too many parameters. Already had '" << mInputFile << "', found '"
129  << argv[x] << "' too. Use -? for help." << endl;
130  }
131  }
132  }
133 
134  if (help)
135  {
136  showHelp();
137  return false;
138  }
139  else if (err)
140  {
141  return false;
142  }
143  else
144  {
145  return true;
146  }
147  }
148 
149  void
151  {
152  const char* cHelpScreen =
153  "This program converts input (from an input file or stdin) from Markdown syntax\n"
154  "to HTML using the cpp-markdown library.\n"
155  "\n"
156  "Usage:\n"
157  " cpp-markdown [<option>...] [input-file]\n"
158  "\n"
159  "Available options are:\n"
160  " -?, --help Show this screen.\n"
161  " -d, --debug Show tokens instead of HTML output.\n";
162  cerr << endl << cHelpScreen << endl;
163  }
164 
165 } // namespace
166 
167 int
168 main(int argc, char* argv[])
169 {
170  Options cfg;
171 
172  if (!cfg.readOptions(argc, argv))
173  {
174  return 1;
175  }
176 
177  // if (cfg.test()) {
178  // return 1;
179  // }
180 
181  std::ifstream ifile;
182 
183  std::istream* in = &std::cin;
184 
185  if (!cfg.inputFile().empty())
186  {
187  cerr << "Reading file '" << cfg.inputFile() << "'..." << endl;
188  ifile.open(cfg.inputFile().c_str());
189 
190  if (!ifile)
191  {
192  cerr << "Error: Can't open file." << endl;
193  return 1;
194  }
195  else
196  {
197  in = &ifile;
198  }
199  }
200  else
201  {
202  cerr << "Reading standard input..." << endl;
203  }
204 
205  markdown::Document doc;
206  doc.read(*in);
207 
208  if (cfg.debug())
209  {
210  doc.writeTokens(cout);
211  }
212  else
213  {
214  doc.write(cout);
215  }
216 
217  return 0;
218 }
markdown::Document
Definition: markdown.h:24
markdown::Document::writeTokens
void writeTokens(std::ostream &)
Definition: markdown.cpp:1050
markdown.h
markdown::Document::read
bool read(const std::string &)
Definition: markdown.cpp:956
main
int main(int argc, char *argv[])
Definition: main.cpp:32
markdown::Document::write
void write(std::ostream &)
Definition: markdown.cpp:1043
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:187
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27