1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2018, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Knut Reinert or the FU Berlin nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: Bjoern Kahlert <Bjoern.Kahlert@fu-berlin.de>
// Author: Stephan Aiche <stephan.aiche@fu-berlin.de>
// ==========================================================================
#ifndef SEQAN_INCLUDE_SEQAN_ARG_PARSE_ARG_PARSE_CTD_SUPPORT_H_
#define SEQAN_INCLUDE_SEQAN_ARG_PARSE_ARG_PARSE_CTD_SUPPORT_H_
#include <seqan/sequence.h>
#include <seqan/arg_parse/xml_support.h>
#include <seqan/arg_parse/argument_parser.h>
#include <seqan/arg_parse/arg_parse_doc.h>
#include <fstream>
namespace seqan {
// ----------------------------------------------------------------------------
// Function _toText()
// ----------------------------------------------------------------------------
// Removes formatting (\fI, \fB, and \fP).
template <typename TSequence>
TSequence _toText(TSequence const & input)
{
TSequence buffer = xmlEscape(input);
TSequence result;
String<TSequence> openTags;
typedef typename Iterator<TSequence const, Standard>::Type TIterator;
TIterator endIt = end(input, Standard());
for (TIterator it = begin(input, Standard()); it != endIt; goNext(it))
{
if (*it == '\\')
{
// Handle escape sequence, we interpret only "\-", "\fI", and "\fB".
goNext(it);
SEQAN_ASSERT_NOT(it == endIt);
if (*it == '-')
{
appendValue(result, *it);
}
else if (*it == 'f')
{
goNext(it);
SEQAN_ASSERT_NOT(it == endIt);
if (*it == 'I')
{
appendValue(openTags, "i");
}
else if (*it == 'B')
{
appendValue(openTags, "b");
}
else if (*it == 'P')
{
SEQAN_ASSERT_NOT(empty(openTags));
eraseBack(openTags);
}
else
{
append(result, "\\f");
appendValue(result, *it);
}
}
else
{
appendValue(result, '\\');
appendValue(result, *it);
}
}
else
{
appendValue(result, *it);
}
}
return result;
}
// ----------------------------------------------------------------------------
// Function _join()
// ----------------------------------------------------------------------------
template <typename TValue>
inline std::string
_join(std::vector<TValue> const & v, std::string const & delimiter)
{
typedef typename std::vector<TValue>::const_iterator TStringSetIterator;
std::stringstream joined;
for (TStringSetIterator it = v.begin(); it != v.end(); ++it)
{
if (it != v.begin())
joined << delimiter;
joined << *it;
}
return joined.str();
}
// ----------------------------------------------------------------------------
// Function _getPrefixedOptionName()
// ----------------------------------------------------------------------------
inline std::string
_getPrefixedOptionName(ArgParseOption const & opt)
{
std::string optName = "";
if (!empty(opt.longName))
optName = "--" + opt.longName;
else
optName = "-" + opt.shortName;
return optName;
}
// ----------------------------------------------------------------------------
// Function _getOptionName()
// ----------------------------------------------------------------------------
inline std::string
_getOptionName(ArgParseOption const & opt)
{
if (!empty(opt.longName))
return opt.longName;
else
return opt.shortName;
}
// ----------------------------------------------------------------------------
// Function _getRestrictions()
// ----------------------------------------------------------------------------
inline void
_getRestrictions(std::vector<std::string> & restrictions, ArgParseArgument const & opt)
{
// we only extract non-file restrictions
if (isOutputFileArgument(opt) || isInputFileArgument(opt) || isInputPrefixArgument(opt) || isOutputPrefixArgument(opt))
{
return;
}
else if (isBooleanArgument(opt))
{
appendValue(restrictions, "true,false");
return;
}
if (length(opt.validValues) != 0)
{
for (std::vector<std::string>::const_iterator valid = opt.validValues.begin();
valid != opt.validValues.end();
++valid)
{
appendValue(restrictions, *valid);
}
}
else
{
std::string minMaxRestriction = "";
if (opt.minValue != "")
{
append(minMaxRestriction, opt.minValue);
append(minMaxRestriction, ":");
}
if (opt.maxValue != "")
{
if (minMaxRestriction == "")
append(minMaxRestriction, ":");
append(minMaxRestriction, opt.maxValue);
}
if (minMaxRestriction != "")
appendValue(restrictions, minMaxRestriction);
}
}
// ----------------------------------------------------------------------------
// Function _addValidValuesRestrictions()
// ----------------------------------------------------------------------------
inline void
_getSupportedFormats(std::vector<std::string> & supported_formats, ArgParseArgument const & opt)
{
// we check only file arguments
if (!(isOutputFileArgument(opt) || isInputFileArgument(opt) || isInputPrefixArgument(opt) || isOutputPrefixArgument(opt)))
return;
if (length(opt.validValues) != 0)
{
std::string filetype;
for (std::vector<std::string>::const_iterator valid = opt.validValues.begin();
valid != opt.validValues.end();
++valid)
{
SEQAN_ASSERT_NOT(empty(*valid));
filetype = "*";
// ensure . as separator between * and file-extension
if (value(*valid, 0) != '.')
appendValue(filetype, '.');
append(filetype, *valid);
appendValue(supported_formats, filetype);
}
}
}
// ----------------------------------------------------------------------------
// Function _includeInCTD()
// ----------------------------------------------------------------------------
/*
* returns true if this option should be included in the ctd
*/
inline bool
_includeInCTD(ArgParseOption const & opt)
{
return !(opt.longName == "help" || opt.longName == "version" || opt.longName == "write-ctd" || opt.longName == "export-help" || (opt.shortName == "" && opt.longName == ""));
}
// ----------------------------------------------------------------------------
// Function _indent()
// ----------------------------------------------------------------------------
inline std::string _indent(const int currentIndent)
{
std::string indent = "";
for (int i = 0; i < currentIndent; ++i)
indent += "\t";
return indent;
}
// ----------------------------------------------------------------------------
// Function _writeCLIElement()
// ----------------------------------------------------------------------------
inline void _writeCLIElement(std::ostream & ctdfile, int currentIndent, std::string const & optionIdentifier, std::string const & ref_name, bool isList)
{
ctdfile << _indent(currentIndent)
<< "<clielement optionIdentifier=\"" << optionIdentifier
<< "\" isList=\"" << (isList ? "true" : "false") << "\">\n";
ctdfile << _indent(currentIndent + 1) << "<mapping referenceName=\"" << ref_name << "\" />\n";
ctdfile << _indent(currentIndent) << "</clielement>\n";
}
// ----------------------------------------------------------------------------
// Function _getManual()
// ----------------------------------------------------------------------------
inline std::string _getManual(ArgumentParser const & me)
{
std::stringstream manual;
for (unsigned i = 0; i < me._description.size(); ++i)
{
manual << _toText(me._description[i]) << std::endl;
}
return manual.str();
}
// ----------------------------------------------------------------------------
// Function _toValidGKNTypeSpecifier()
// ----------------------------------------------------------------------------
// TODO(dadi): similar to _typeToString() function. Differs only in the case of bool, integer, int64 and types
//. that has underscores in them. hyphens are used instead of underscores. e.g. input-file instead
// of input_file. This should be removed and _typeToString() should be used instead once GKN is
// modified to accept the results of _typeToString() functions directly.
inline std::string _toValidGKNTypeSpecifier(ArgParseArgument const & me)
{
switch (me._argumentType)
{
case ArgParseArgument::BOOL:
return "string";
case ArgParseArgument::INTEGER:
return "int";
case ArgParseArgument::INT64:
return "int";
case ArgParseArgument::INPUT_FILE:
return "input-file";
case ArgParseArgument::OUTPUT_FILE:
return "output-file";
case ArgParseArgument::INPUT_PREFIX:
return "input-prefix";
case ArgParseArgument::OUTPUT_PREFIX:
return "output-prefix";
case ArgParseArgument::INPUT_DIRECTORY:
return "input-prefix";
case ArgParseArgument::OUTPUT_DIRECTORY:
return "output-prefix";
default:
return _typeToString(me);
}
}
// ----------------------------------------------------------------------------
// Function writeCTD()
// ----------------------------------------------------------------------------
// TODO(holtgrew): Change argument order.
/*!
* @fn ArgumentParser#writeCTD
* @headerfile <seqan/arg_parse.h>\
* @brief Export the app's interface description to a .ctd file.
*
* @signature bool writeCTD(parser[, stream]);
*
* @param[in] parser The ArgumentParser to write the CTD file for.
* @param[out] stream A <tt>std::ostream</tt> to write to. If omitted an output file with the name form the
* "write-ctd" parameter of the parser is used.
*
* @return bool <tt>true</tt> on success, <tt>false</tt> on failure.
*/
inline bool
writeCTD(ArgumentParser const & me, std::ostream & ctdfile)
{
typedef ArgumentParser::TOptionMap::const_iterator TOptionMapIterator;
typedef ArgumentParser::TArgumentMapSize TArgumentMapSize;
int currentIndent = 1;
std::string toolname(toCString(xmlEscape(getAppName(me))));
// remove "_" in the tool name and make the following letter uppercase
std::string class_name;
bool upcase = true;
for (unsigned i = 0; i < toolname.size(); ++i)
{
if (toolname[i] == '_')
{
upcase = true;
continue;
}
class_name.push_back(toolname[i]);
if (upcase)
class_name[class_name.size() - 1] = toupper(toolname[i]);
upcase = false;
}
ctdfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
ctdfile << "<tool name=\"" << class_name << "\" version=\"" << xmlEscape(getVersion(me)) << "\" docurl=\"http://www.seqan.de\" category=\"" << xmlEscape(getCategory(me)) << "\" ctdVersion=\"1.7\">\n";
ctdfile << _indent(currentIndent) << "<executableName>" << toolname << "</executableName>\n";
ctdfile << _indent(currentIndent) << "<description>" << xmlEscape(getShortDescription(me)) << "</description>\n";
ctdfile << _indent(currentIndent) << "<manual>" << xmlEscape(_getManual(me)) << "</manual>\n"; // TODO(aiche): as soon as we have a more sophisticated documentation embedded into the CmdParser, we should at this here
ctdfile << _indent(currentIndent++) << "<cli>\n";
// the unix way 1st the options
for (TOptionMapIterator optionMapIterator = me.optionMap.begin();
optionMapIterator != me.optionMap.end();
++optionMapIterator)
{
ArgParseOption const & opt = *optionMapIterator;
// exclude hidden
if (isHidden(opt))
continue;
std::string optionIdentifier = _getPrefixedOptionName(opt);
std::string refName = toolname + "." + _getOptionName(opt);
if (_includeInCTD(opt))
{
_writeCLIElement(ctdfile, currentIndent, optionIdentifier, refName, isListArgument(opt));
}
}
// add a warning to the CTD that arguments are hard to interpret by the users
if (me.argumentList.size() > 0u)
{
ctdfile << _indent(currentIndent)
<< "<!-- Following clielements are arguments."
<< " You should consider providing a help text to ease understanding. -->\n";
}
// then the arguments
for (TArgumentMapSize argIdx = 0; argIdx != me.argumentList.size(); ++argIdx)
{
// arguments do not have an option identifier
std::string optionIdentifier = "";
std::stringstream refName;
refName << toolname << "." << "argument-" << argIdx;
_writeCLIElement(ctdfile, currentIndent, optionIdentifier, refName.str(), isListArgument(me.argumentList[argIdx]));
}
ctdfile << _indent(--currentIndent) << "</cli>\n";
ctdfile << _indent(currentIndent++) << "<PARAMETERS version=\"1.6.2\" xsi:noNamespaceSchemaLocation=\"http://open-ms.sourceforge.net/schemas/Param_1_6_2.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
ctdfile << _indent(currentIndent++) << "<NODE name=\"" << toolname << "\" description=\"" << xmlEscape(getShortDescription(me)) << "\">\n";
for (TOptionMapIterator optionMapIterator = me.optionMap.begin();
optionMapIterator != me.optionMap.end();
++optionMapIterator)
{
ArgParseOption const & opt = *optionMapIterator;
// exclude help, version, etc.
if (!_includeInCTD(opt))
continue;
// exclude hidden
if (isHidden(opt))
continue;
// prefer short name for options
std::string optionName = _getOptionName(opt);
std::string type = _toValidGKNTypeSpecifier(opt);
// set up restrictions
std::vector<std::string> restrictions;
_getRestrictions(restrictions, opt);
// set up supported formats
// add *.* to supported_formats if none is specified AND the type of the argument is a
// prefix type. This is important for KNIME nodes as they require similar file types
// to connect one node to the other. In this particular case any file is aproprate
// since we are talking about prefixes.
std::vector<std::string> supported_formats;
_getSupportedFormats(supported_formats, opt);
if (empty(supported_formats) && (type=="input-prefix" || type=="output-prefix" ))
supported_formats.push_back("*.*");
ctdfile << _indent(currentIndent)
<< "<ITEM" << (isListArgument(opt) ? "LIST" : "") << " name=\"" << xmlEscape(optionName) << "\"";
// handle the value field
if (isListArgument(opt))
ctdfile << " ";
else
ctdfile << " value=\"" << xmlEscape(_join(opt.defaultValue, ",")) << "\" ";
ctdfile << "type=\"" << type << "\" "
<< "description=\"" << xmlEscape(_toText(opt._helpText)) << "\" ";
if (!empty(restrictions))
ctdfile << "restrictions=\"" << xmlEscape(_join(restrictions, ",")) << "\" ";
if (!empty(supported_formats))
ctdfile << "supported_formats=\"" << xmlEscape(_join(supported_formats, ",")) << "\" ";
ctdfile << "required=\"" << (isRequired(opt) ? "true" : "false") << "\" ";
ctdfile << "advanced=\"" << (isAdvanced(opt) ? "true" : "false") << "\" ";
// Write out tags attribute.
if (!opt.tags.empty())
{
ctdfile << "tags=\"";
for (unsigned i = 0; i < length(opt.tags); ++i)
{
if (i > 0)
ctdfile << ",";
ctdfile << opt.tags[i];
}
ctdfile << "\" ";
}
if (isListArgument(opt))
{
ctdfile << ">\n";
for (size_t i = 0; i < length(opt.defaultValue); ++i)
{
ctdfile << _indent(currentIndent + 1) << "<LISTITEM value=\"" << xmlEscape(opt.defaultValue[i]) << "\"/>\n";
}
ctdfile << _indent(currentIndent) << "</ITEMLIST>\n";
}
else
{
ctdfile << "/>\n";
}
}
for (TArgumentMapSize argIdx = 0; argIdx != me.argumentList.size(); ++argIdx)
{
ArgParseArgument arg = me.argumentList[argIdx];
// prefer short name for options
std::stringstream argumentNameStream;
argumentNameStream << "argument-" << argIdx;
std::string optionName = argumentNameStream.str();
std::string type = _toValidGKNTypeSpecifier(arg);
// set up restrictions
std::vector<std::string> restrictions;
_getRestrictions(restrictions, arg);
// set up supported formats
// add *.* to supported_formats if none is specified AND the type of the argument is a
// prefix type. This is important for KNIME nodes as they require similar file types
// to connect one node to the other. In this particular case any file is aproprate
// since we are talking about prefixes.
std::vector<std::string> supported_formats;
_getSupportedFormats(supported_formats, arg);
if (empty(supported_formats) && (type=="input-prefix" || type=="output-prefix" ))
supported_formats.push_back("*.*");
ctdfile << _indent(currentIndent)
<< "<ITEM" << (isListArgument(arg) ? "LIST" : "") << " name=\"" << xmlEscape(optionName) << "\" "
<< (isListArgument(arg) ? " " : "value=\"\" ")
<< "type=\"" << type << "\" "
<< "description=\"" << xmlEscape(_toText(arg._helpText)) << "\" "; // it will be "" in most cases but we try
if (!empty(restrictions))
ctdfile << "restrictions=\"" << xmlEscape(_join(restrictions, ",")) << "\" ";
if (!empty(supported_formats))
ctdfile << "supported_formats=\"" << xmlEscape(_join(supported_formats, ",")) << "\" ";
ctdfile << "required=\"true\" ";
ctdfile << "advanced=\"false\" ";
// Write out tags attribute.
if (!arg.tags.empty())
{
ctdfile << "tags=\"";
for (unsigned i = 0; i < length(arg.tags.size()); ++i)
{
if (i > 0)
ctdfile << ",";
ctdfile << arg.tags[i];
}
ctdfile << "\" ";
}
ctdfile << "/>\n";
}
ctdfile << _indent(--currentIndent) << "</NODE>\n";
ctdfile << _indent(--currentIndent) << "</PARAMETERS>\n";
ctdfile << "</tool>" << std::endl;
return true;
}
inline bool
writeCTD(ArgumentParser const & me)
{
// create file [appname].ctd in working directory
std::string ctdfilename;
getOptionValue(ctdfilename, me, "write-ctd");
std::ofstream ctdfile;
ctdfile.open(toCString(ctdfilename));
if (!ctdfile.is_open())
{
std::cerr << getAppName(me) << ": Unable to create ctd file: " << ctdfilename << std::endl;
return false;
}
writeCTD(me, ctdfile);
ctdfile.close();
return true;
}
} // namespace seqan
#endif // SEQAN_INCLUDE_SEQAN_ARG_PARSE_ARG_PARSE_CTD_SUPPORT_H_
|