Xmipp  v3.23.11-Nereus
argsprinter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: J.M de la Rosa Trevin (jmdelarosa@cnb.csic.es)
3  *
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your param) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include "argsprinter.h"
27 #include "xmipp_filename.h"
28 #include "xmipp_color.h"
29 #include "xmipp_error.h"
30 
31 //------------------- PRINTER IMPLEMENTATIONS --------------------------------
33 {
34 
35  std::cerr << "token: '" << token->lexeme
36  << "' type: " << ArgToken::typeString(token->type)
37  << " line: " << token->line + 1
38  << " pos: " << token->start + 1 << std::endl;
39 }
40 
41 //--------- CONSOLE PRINTER -----------------------
42 #define COLOR(x, c) (color ? colorString(x, c) : String(x))
43 
44 ConsolePrinter::ConsolePrinter(std::ostream & out, bool color)
45 {
46  this->pOut = &out;
47  this->color = color;
48 }
49 
50 void ConsolePrinter::printProgram(const ProgramDef &program, int v)
51 {
52  //print program name and usage
53  *pOut << COLOR("PROGRAM", RED) << std::endl << " " << program.name << std::endl;
54  if (program.usageComments.size() > 0)
55  {
56  *pOut << COLOR("USAGE", RED) << std::endl;
57  for (size_t i = 0; i < program.usageComments.size(); ++i)
58  if (program.usageComments.visibility[i] <= v)
59  *pOut << " " << program.usageComments.comments[i] << std::endl;
60  }
61  //print see also
62  if (!program.seeAlso.empty())
63  {
64  *pOut << COLOR("SEE ALSO", RED) << std::endl;
65  *pOut << " " << program.seeAlso << std::endl;
66  }
67 
68  //print sections and params
69  if (program.sections.size() > 0)
70  {
71  *pOut << COLOR("OPTIONS", RED) << std::endl;
72  for (size_t i = 0; i < program.sections.size(); ++i)
73  printSection(*program.sections[i], v);
74  }
75  //print examples
76  if (program.examples.size() > 0)
77  {
78  *pOut << COLOR("EXAMPLES", RED) << std::endl;
79  for (size_t i = 0; i < program.examples.size(); ++i)
80  if (program.examples.visibility[i] <= v)
81  {
82  if (program.examples.wikiVerbatim[i])
83  *pOut << " " << COLOR(program.examples.comments[i].c_str(), BLUE) << std::endl;
84  else
85  *pOut << " " << program.examples.comments[i] << std::endl;
86  }
87 
88  }
89 }
90 
91 void ConsolePrinter::printSection(const SectionDef &section, int v)
92 {
93  if (section.visible <= v)
94  {
95  *pOut << std::endl;
96  if (section.name.length() > 0)
97  *pOut << COLOR(section.name.c_str(), RED) << std::endl;
98  for (size_t i = 0; i < section.params.size(); ++i)
99  printParam(*section.params[i], v);
100  }
101 }
102 
104 {
105  if (!requires.empty())
106  {
107  *pOut << " ( requires ";
108  for (size_t i = 0; i < requires.size(); ++i)
109  *pOut << requires[i] << " ";
110  *pOut << ")";
111  }
112 }
113 
115 {
116  if (param.visible <= v)
117  {
118  if (param.orBefore)
119  *pOut << " OR" << std::endl;
120 
121  *pOut << " ";
122  int pColor = BLUE;
123  if (!param.notOptional)
124  {
125  *pOut << "[";
126  pColor = GREEN;
127  }
128  *pOut << COLOR(param.name.c_str(), pColor);
129  //print alias
130  for (size_t i = 0; i < param.aliases.size(); ++i)
131  *pOut << ", " << param.aliases[i];
132  //print arguments
133  for (size_t i = 0; i < param.arguments.size(); ++i)
134  {
135  *pOut << " ";
136  printArgument(*param.arguments[i], v);
137  }
138  if (!param.notOptional)
139  *pOut << "]";
140 
141  printRequiresList(param.requires);
142  *pOut << std::endl;
143  printCommentList(param.comments, v);
144 
145  for (size_t i = 0; i < param.arguments.size(); ++i)
146  {
147  ArgumentDef &arg = *param.arguments[i];
148  if (!arg.subParams.empty())
149  {
150  *pOut << " where <" << arg.name << "> can be:" << std::endl;
151  for (size_t j = 0; j < arg.subParams.size(); ++j)
152  {
153  *pOut << " " << arg.subParams[j]->name;
154  for (size_t k = 0; k < arg.subParams[j]->arguments.size(); ++k)
155  {
156  *pOut << " ";
157  printArgument(*(arg.subParams[j]->arguments[k]));
158  }
159  printRequiresList(arg.subParams[j]->requires);
160  *pOut << std::endl;
161  printCommentList(arg.subParams[j]->comments, v);
162 
163  }
164  }
165  }
166 
167  }
168 }
169 
170 void ConsolePrinter::printArgument(const ArgumentDef & argument, int v)
171 {
172  *pOut << "<" << argument.name;
173  if (argument.hasDefault)
174  *pOut << "=" << argument.argDefault;
175  *pOut << ">";
176 }
177 
178 void ConsolePrinter::printCommentList(const CommentList &comments, int v)
179 {
180  for (size_t i = 0; i < comments.size(); ++i)
181  if (comments.visibility[i] <= v)
182  *pOut << " " << comments.comments[i] << std::endl;
183 }
184 
185 //------------------- TK PRINTER IMPLEMENTATIONS --------------------------------
186 
188 {
189  FileName dir = getXmippPath();
190  dir.append("/applications/scripts/program_gui/program_gui.py");
191  output = popen(dir.c_str(), "w");
192 }
193 
195 {
196  pclose(output);
197 }
198 
199 void TkPrinter::printProgram(const ProgramDef &program, int v)
200 {
201  // *pOut << "PROGRAM" << std::endl << " " << program.name << std::endl;
202  fprintf(output, "XMIPP %d.%d - %s\n", XMIPP_MAJOR, XMIPP_MINOR, program.name.c_str());
203  size_t numberOfComments = 0;
204  for (size_t i = 0; i < program.usageComments.size(); ++i)
205  if (program.usageComments.visibility[i] <= v)
206  ++numberOfComments;
207  //Send number of usage lines
208  fprintf(output, "%d\n", (int)numberOfComments);
209  if (numberOfComments > 0)
210  {
211  for (size_t i = 0; i < program.usageComments.size(); ++i)
212  if (program.usageComments.visibility[i] <= v)
213  fprintf(output, "%s\n", program.usageComments.comments[i].c_str());
214  }
215 
216  for (size_t i = 0; i < program.sections.size(); ++i)
217  printSection(*program.sections[i], v);
218 }
219 
220 void TkPrinter::printSection(const SectionDef &section, int v)
221 {
222  if (section.visible <= v)
223  {
224  //Just ignore in the GUI this section
225  if (section.name == " Common options ")
226  return;
227 
228  //if (section.name.length() > 0)
229  fprintf(output, "section = self.addSection('%s');\n", section.name.c_str());
230  bool first_group = true;
231  for (size_t i = 0; i < section.params.size(); ++i)
232  {
233  if (section.params[i]->visible <= v)
234  {
235  if (!section.params[i]->orBefore)
236  {
237  const char * single = (i < section.params.size()-1 && section.params[i+1]->orBefore) ? "False" : "True";
238 
239  if (!first_group)
240  fprintf(output, "section.addGroup(group);\n");
241  else
242  first_group = false;
243  fprintf(output, "group = ParamsGroup(section, %s);\n", single);
244 
245  }
246  printParam(*section.params[i], v);
247  }
248  }
249  //close last open group
250  if (!first_group)
251  fprintf(output, "section.addGroup(group);\n");
252  }
253 }
254 
256 {
257  if (param.visible <= v)
258  {
259  //Independent params are some kind of special ones
260  if (param.independent)
261  return;
262 
263  fprintf(output, "param = ParamWidget(group, \"%s\");\n", param.name.c_str());
264  if (param.notOptional)
265  fprintf(output, "param.notOptional = True; \n");
266  for (size_t i = 0; i < param.arguments.size(); ++i)
267  {
268  printArgument(*param.arguments[i], v);
269  }
270  //Add comments to the help
271  for (size_t i = 0; i < param.comments.size(); ++i)
272  //if (param.comments.visibility[i] <= v)
273  fprintf(output, "param.addCommentLine('''%s''');\n", param.comments.comments[i].c_str());
274  //End with options of the param
275  fprintf(output, "param.endWithOptions();\n");
276 
277  }
278 }
279 
280 void TkPrinter::printArgument(const ArgumentDef & argument, int v)
281 {
282  static String paramStr = "param";
283  fprintf(output, "%s.addOption(\"%s\", \"%s\", %d);\n",
284  paramStr.c_str(), argument.name.c_str(), argument.argDefault.c_str(), (int)argument.subParams.size());
285  if (argument.subParams.size() > 0)
286  {
287 
288  for (size_t j = 0; j < argument.subParams.size(); ++j)
289  {
290  fprintf(output, "subparam = param.addSubParam(\"%s\");\n",
291  argument.subParams[j]->name.c_str());
292  for (size_t i = 0; i < argument.subParams[j]->comments.size(); ++i)
293  fprintf(output, "subparam.addCommentLine('''%s''');\n", argument.subParams[j]->comments.comments[i].c_str());
294  paramStr = "subparam";
295  for (size_t k = 0; k < argument.subParams[j]->arguments.size(); ++k)
296  {
297  printArgument(*(argument.subParams[j]->arguments[k]));
298  }
299  paramStr = "param";
300  }
301  }
302 }
303 
304 //--------- WIKI PRINTER -----------------------
305 WikiPrinter::WikiPrinter(std::ostream & out)
306 {
307  this->pOut = &out;
308 }
309 
310 void WikiPrinter::printProgram(const ProgramDef &program, int v)
311 {
312  //print program name and usage
313  *pOut << "---+ !!" << program.name << " (v" << XMIPP_MAJOR <<"." << XMIPP_MINOR << ")" << std::endl;
314  *pOut << "%TOC%" << std::endl;
315  //print usage
316  if (program.usageComments.size() > 0)
317  {
318  *pOut << "---++ Usage" << std::endl;
319  for (size_t i = 0; i < program.usageComments.size(); ++i)
320  if (program.usageComments.wikiVerbatim[i])
321  *pOut << " <pre>" << program.usageComments.comments[i] << "</pre>\n";
322  else
323  *pOut << " " << program.usageComments.comments[i] << std::endl;
324  }
325  if (!program.seeAlso.empty())
326  {
327  *pOut << std::endl << "*See also* %BR%" << std::endl;
328  StringVector links;
329  splitString(program.seeAlso, ",", links);
330  for (size_t i = 0; i < links.size(); ++i)
331  *pOut << "[[" << links[i] << "_v" << XMIPP_MAJOR << "][" << links[i] <<"]] ";
332  *pOut << "%BR%" << std::endl;
333  }
334  //print sections and params
335  if (program.sections.size() > 0)
336  {
337  *pOut << std::endl << "*Parameters*" << std::endl;
338  for (size_t i = 0; i < program.sections.size(); ++i)
339  printSection(*program.sections[i], v);
340  }
341  //print examples
342  if (program.examples.size() > 0)
343  {
344  *pOut << "---++ Examples and notes" << std::endl;
345  bool verbatim = false;
346  for (size_t i = 0; i < program.examples.size(); ++i)
347  {
348  if (program.examples.wikiVerbatim[i])
349  {
350  if (!verbatim)
351  {
352  *pOut << "<pre>" << std::endl;
353  verbatim = true;
354  }
355  }
356  else
357  {
358  if (verbatim)
359  {
360  *pOut << "</pre>" << std::endl;
361  verbatim = false;
362  }
363  }
364  *pOut << program.examples.comments[i] << std::endl;
365  }
366  if (verbatim)
367  *pOut << "</pre>" << std::endl;
368  }
369  //print user comments
370  *pOut << "---++ User's comments" << std::endl;
371  *pOut << "%COMMENT{type=\"tableappend\"}%" << std::endl;
372 }
373 
374 void WikiPrinter::printSection(const SectionDef &section, int v)
375 {
376  if (section.name != " Common options "
377  && section.visible <= v)
378  {
379  *pOut << std::endl;
380  String name = section.name;
381  trim(name);
382  if (name.length() > 0)
383  *pOut << "_" << name << "_" << std::endl;
384  for (size_t i = 0; i < section.params.size(); ++i)
385  printParam(*section.params[i], v);
386  }
387 }
388 
390 {
391  if (!requires.empty())
392  {
393  *pOut << " ( requires ";
394  for (size_t i = 0; i < requires.size(); ++i)
395  *pOut << requires[i] << " ";
396  *pOut << ")";
397  }
398 }
399 
401 {
402  //* =%BLUE%-i [selfile] %ENDCOLOR%= This file contains all the images that are to build the 3D reconstruction
403  //* =%GREEN% -o [output file root name] %ENDCOLOR%= If you don't supply this parameter, the same as the input selection one is taken without extension. If you give, for instance, =-o art0001= the following files are created:
404  if (param.visible <= v)
405  {
406  *pOut << " $";
407 
408  if (param.orBefore)
409  *pOut << " or";
410 
411  String color = param.notOptional ? "BLUE" : "GREEN";
412 
413  *pOut << " =%" << color << "%" << param.name;
414  //print alias
415  for (size_t i = 0; i < param.aliases.size(); ++i)
416  *pOut << ", " << param.aliases[i];
417  //print arguments
418  for (size_t i = 0; i < param.arguments.size(); ++i)
419  {
420  *pOut << " ";
421  printArgument(*param.arguments[i], v);
422  }
423  *pOut << " %ENDCOLOR%=";
424  printRequiresList(param.requires);
425  *pOut <<": " ;
426  printCommentList(param.comments, v);
427 
428  if (param.comments.size() == 0)
429  *pOut << "%BR%" << std::endl;
430 
431  for (size_t i = 0; i < param.arguments.size(); ++i)
432  {
433  ArgumentDef &arg = *param.arguments[i];
434  if (!arg.subParams.empty())
435  {
436  *pOut << " where &lt;" << arg.name << "&gt; can be:" << std::endl;
437  for (size_t j = 0; j < arg.subParams.size(); ++j)
438  {
439  *pOut << " * %MAROON% " << arg.subParams[j]->name;
440  for (size_t k = 0; k < arg.subParams[j]->arguments.size(); ++k)
441  {
442  *pOut << " ";
443  printArgument(*(arg.subParams[j]->arguments[k]), v);
444  }
445  *pOut << " %ENDCOLOR%" << std::endl;
446  printRequiresList(arg.subParams[j]->requires);
447  // *pOut << std::endl;
448  printCommentList(arg.subParams[j]->comments, v);
449 
450  }
451  }
452  }
453 
454  }
455 }
456 
457 void WikiPrinter::printArgument(const ArgumentDef & argument, int v)
458 {
459  *pOut << "&lt;" << argument.name;
460  if (argument.hasDefault)
461  *pOut << "=" << argument.argDefault;
462  *pOut << "&gt;";
463 }
464 
465 void WikiPrinter::printCommentList(const CommentList &comments, int v)
466 {
467  *pOut << " ";
468  for (size_t i = 0; i < comments.size(); ++i)
469  if (comments.visibility[i] <= v)
470  *pOut << comments.comments[i] << " ";
471  *pOut << "%BR%" << std::endl;
472 }
473 
474 //------------------- PROTOCOL PRINTER IMPLEMENTATIONS --------------------------------
475 
476 bool matchArgInList(const String &argName, size_t n, const char** list)
477 {
478  for (size_t i = 0; i < n; ++i)
479  if (argName.find(list[i]) != String::npos)
480  return true;
481  return false;
482 }
483 
484 bool isArgFile(const String &argName)
485 {
486  const char* list[3] =
487  {"file", "metadata", "selfile"
488  };
489  return matchArgInList(argName, 3,
490  list);
491 }
492 
493 //------------------- AUTOCOMPLETE PRINTER IMPLEMENTATIONS --------------------------------
494 
495 
496 AutocompletePrinter::AutocompletePrinter(const char * scriptfile, bool programGui)
497 {
498  output = fopen(scriptfile, "a");
499  if (output == NULL)
500  REPORT_ERROR(ERR_IO, "Couldn't open file to write program autocomplete script");
501 }
502 
504 {
505  fclose(output);
506 }
507 
508 
510 {
511 
512  const char * progStr = program.name.c_str();
513  fprintf(output, "_%s()\n", progStr);
514  fprintf(output, "{ \n");
515  fprintf(output, "local cur prev opts base \n");
516  fprintf(output, "COMPREPLY=() \n");
517  fprintf(output, "cur=\"${COMP_WORDS[COMP_CWORD]}\" \n");
518  fprintf(output, "prev=\"${COMP_WORDS[COMP_CWORD-1]}\" \n");
519 
520  StringVector::const_iterator iter;
521  std::vector<SectionDef*>::const_iterator siter;
522  std::vector<ParamDef*>::const_iterator piter;
523 
524  String opts = "";
525  fprintf(output, "# Autocomplete options: \n");
526  fprintf(output, "case \"${prev}\" in\n");
527 
528  for (siter = program.sections.begin(); siter != program.sections.end(); siter++)
529  {
530  SectionDef &section = **siter;
531  if (section.visible < v)
532  {
533  for (piter = section.params.begin(); piter != section.params.end(); ++piter)
534  {
535  ParamDef &param = **piter;
536  opts += param.name + " ";
537  for (iter = param.aliases.begin(); iter != param.aliases.end(); iter++)
538  opts += *iter + " ";
539  printParam(param, v);
540  }
541  }
542  }
543 
544  fprintf(output, " *)\n ;;\nesac\n");
545 
546  fprintf(output, "# Options: \n");
547  fprintf(output, "opts=\"%s\" \n", opts.c_str());
548 
549  fprintf(output, "COMPREPLY=($(compgen -W \"${opts}\" -- ${cur}))\n");
550  fprintf(output, "return 0 \n");
551  fprintf(output, "} \n");
552 
553  fprintf(output, "complete -o bashdefault -o default -o filenames -F _%s %s \n",
554  progStr, progStr);
555 
556 }
557 
559 {}
560 
562 {
563  String caseStr = param.name;
564  StringVector::const_iterator iter;
565 
566  for (iter = param.aliases.begin(); iter != param.aliases.end(); iter++)
567  caseStr += " | " + *iter;
568  fprintf(output, " %s)\n", caseStr.c_str());
569 
570  if (param.arguments.size())
571  {
572  ArgumentDef &arg = *(param.arguments[0]);
573  if (!arg.subParams.empty())
574  {
575  String where_opts = "";
576  for (size_t j = 0; j < arg.subParams.size(); ++j)
577  where_opts += arg.subParams[j]->name + " ";
578  fprintf(output, " local where_opts=\"%s\"\n", where_opts.c_str());
579  fprintf(output, " COMPREPLY=( $(compgen -W \"${where_opts}\" -- ${cur}) )\n");
580  fprintf(output, " return 0\n");
581  }
582  else if (isArgFile(arg.name))
583  fprintf(output, " return 0\n");
584  }
585  else
586  fprintf(output, " COMPREPLY=()\n");
587  fprintf(output, " ;;\n");
588 }
589 
590 void AutocompletePrinter::printArgument(const ArgumentDef & argument, int v)
591 {}
592 
594 {}
595 
596 
virtual void printArgument(const ArgumentDef &argument, int v=0)
ArgTokenType type
Type of the token.
Definition: argsparser.h:75
virtual void printParam(const ParamDef &param, int v=0)
virtual void printSection(const SectionDef &section, int v=0)
virtual void printCommentList(const CommentList &comments, int v=0)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
String name
Definition: argsparser.h:152
virtual void printToken(ArgToken *token)
Definition: argsprinter.cpp:32
char * getXmippPath()
bool notOptional
Definition: argsparser.h:195
void trim(std::string &s)
Definition: text.cpp:205
bool matchArgInList(const String &argName, size_t n, const char **list)
virtual void printArgument(const ArgumentDef &argument, int v=0)
std::vector< int > visibility
Definition: comment_list.h:36
virtual void printCommentList(const CommentList &comments, int v=0)=0
bool hasDefault
Definition: argsparser.h:176
bool isArgFile(const String &argName)
Input/Output general error.
Definition: xmipp_error.h:134
virtual void printParam(const ParamDef &param, int v=0)
CommentList comments
Definition: argsparser.h:203
glob_prnt iter
std::vector< SectionDef * > sections
Definition: argsparser.h:241
void printRequiresList(StringVector requires)
static const char * typeString(ArgTokenType type)
Definition: argsparser.cpp:33
virtual void printParam(const ParamDef &param, int v=0)
std::vector< String > StringVector
Definition: xmipp_strings.h:35
int start
Definition: argsparser.h:78
#define i
void printRequiresList(StringVector requires)
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
StringVector requires
Definition: argsparser.h:205
WikiPrinter(std::ostream &out=std::cout)
virtual void printArgument(const ArgumentDef &argument, int v=0)
#define COLOR(x, c)
Definition: argsprinter.cpp:42
int visible
Definition: argsparser.h:153
std::vector< bool > wikiVerbatim
Definition: comment_list.h:37
int splitString(const String &input, const String &delimiter, StringVector &results, bool includeEmpties)
String seeAlso
Definition: argsparser.h:247
std::vector< ParamDef * > subParams
Definition: argsparser.h:175
virtual void printArgument(const ArgumentDef &argument, int v=0)
virtual void printCommentList(const CommentList &comments, int v=0)
std::vector< ArgumentDef * > arguments
Definition: argsparser.h:198
size_t size() const
for(j=1;j<=i__1;++j)
bool independent
Definition: argsparser.h:197
virtual void printCommentList(const CommentList &comments, int v=0)
virtual void printProgram(const ProgramDef &program, int v=0)
virtual void printSection(const SectionDef &section, int v=0)
#define j
#define XMIPP_MAJOR
Definition: argsprinter.h:31
String argDefault
Definition: argsparser.h:172
struct _parameter * param
AutocompletePrinter(const char *scriptfile, bool programGui=false)
StringVector comments
Definition: comment_list.h:35
std::vector< ParamDef * > params
All params defined for the program.
Definition: argsparser.h:224
virtual void printSection(const SectionDef &section, int v=0)=0
CommentList examples
examples of use
Definition: argsparser.h:243
CommentList usageComments
comments of usage
Definition: argsparser.h:242
std::string String
Definition: xmipp_strings.h:34
virtual void printParam(const ParamDef &param, int v=0)=0
virtual void printArgument(const ArgumentDef &argument, int v=0)=0
int line
line where token was found
Definition: argsparser.h:77
virtual void printProgram(const ProgramDef &program, int v=0)
virtual void printParam(const ParamDef &param, int v=0)
#define XMIPP_MINOR
Definition: argsprinter.h:32
fprintf(glob_prnt.io, "\)
ConsolePrinter(std::ostream &out=std::cout, bool color=true)
Definition: argsprinter.cpp:44
virtual ~AutocompletePrinter()
virtual void printSection(const SectionDef &section, int v=0)
Definition: argsprinter.cpp:91
virtual void printSection(const SectionDef &section, int v=0)
bool orBefore
Definition: argsparser.h:196
int * n
String lexeme
the string literal value of the token
Definition: argsparser.h:76
virtual void printProgram(const ProgramDef &program, int v=0)
virtual void printProgram(const ProgramDef &program, int v=0)
Definition: argsprinter.cpp:50
StringVector aliases
Definition: argsparser.h:204