cmtkCommandLineKeyToAction.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //  Copyright 2004-2009 SRI International
00005 //
00006 //  This file is part of the Computational Morphometry Toolkit.
00007 //
00008 //  http://www.nitrc.org/projects/cmtk/
00009 //
00010 //  The Computational Morphometry Toolkit is free software: you can
00011 //  redistribute it and/or modify it under the terms of the GNU General Public
00012 //  License as published by the Free Software Foundation, either version 3 of
00013 //  the License, or (at your option) any later version.
00014 //
00015 //  The Computational Morphometry Toolkit is distributed in the hope that it
00016 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00017 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU General Public License along
00021 //  with the Computational Morphometry Toolkit.  If not, see
00022 //  <http://www.gnu.org/licenses/>.
00023 //
00024 //  $Revision: 2022 $
00025 //
00026 //  $LastChangedDate: 2010-07-21 15:26:03 -0700 (Wed, 21 Jul 2010) $
00027 //
00028 //  $LastChangedBy: torstenrohlfing $
00029 //
00030 */
00031 
00032 #include "cmtkCommandLine.h"
00033 
00034 #include <sstream>
00035 
00036 
00037 void
00038 cmtk::CommandLine::KeyToAction
00039 ::SetProperties( const long int properties )
00040 {
00041   this->m_Properties = properties;
00042 }
00043 
00044 long int
00045 cmtk::CommandLine::KeyToAction
00046 ::GetProperties() const
00047 {
00048   return this->m_Properties;
00049 }
00050 
00051 mxml_node_t*
00052 cmtk::CommandLine::KeyToAction
00053 ::MakeXML( mxml_node_t *const node ) const
00054 {
00055   if ( ! (this->m_Properties & PROPS_NOXML) )
00056     {
00057     // for some reason Slicer does not accept long options that contain hyphens ("-"), so we replace them.
00058     std::string xmlKeyStr = this->m_Key.m_KeyString;
00059     for ( size_t i = 0; i < xmlKeyStr.length(); ++i )
00060       {
00061       if ( xmlKeyStr[i] == '-' )
00062         xmlKeyStr[i] = '_';
00063       }
00064     
00065     if ( this->m_Comment.length() )
00066       {
00067       mxmlNewText( mxmlNewElement( node, "description" ), 0, this->m_Comment.c_str() );
00068       }
00069     
00070     if ( this->m_Key.m_KeyString.length() )
00071       {
00072       mxmlNewText( mxmlNewElement( node, "name" ), 0, xmlKeyStr.c_str() );
00073       mxmlNewText( mxmlNewElement( node, "label" ), 0, xmlKeyStr.c_str() );
00074       }
00075     if ( this->m_Key.m_KeyChar )
00076       {
00077       const char keyStr[] = { '-', this->m_Key.m_KeyChar, 0 };
00078       mxmlNewText( mxmlNewElement( node, "flag" ), 0, keyStr );
00079       }
00080     if ( this->m_Key.m_KeyString.length() )
00081       {
00082       mxmlNewText( mxmlNewElement( node, "longflag" ), 0, (std::string( "--" ) + xmlKeyStr).c_str() );
00083       }
00084     
00085     return node;
00086     }
00087   return NULL;
00088 }
00089 
00090 void
00091 cmtk::CommandLine::KeyToAction
00092 ::FormatHelp( std::ostringstream& fmt ) const
00093 {
00094   const std::string& typeInfo = this->GetActionTypeInfo();
00095   if ( this->m_Key.m_KeyString.size() )
00096     {
00097     fmt << "--" << this->m_Key.m_KeyString;
00098     if ( typeInfo.length() )
00099       {
00100       fmt << " " << typeInfo;
00101       }
00102     }
00103 
00104   if ( this->m_Key.m_KeyChar && this->m_Key.m_KeyString.size() )
00105     {
00106     fmt << " / ";
00107     }
00108 
00109   if ( this->m_Key.m_KeyChar )
00110     {
00111     fmt << "-" << this->m_Key.m_KeyChar;
00112     if ( typeInfo.length() )
00113       {
00114       fmt << " " << typeInfo;
00115       }
00116     }
00117 
00118   if ( fmt.str().length() > static_cast<size_t>( CommandLine::HelpTextIndent-2 ) )
00119     fmt << "\n";
00120   else
00121     {
00122     while ( fmt.str().length() < static_cast<size_t>( CommandLine::HelpTextIndent ) )
00123       fmt << " ";
00124     }
00125   
00126   if ( this->m_Comment.length() )
00127     {
00128     fmt << this->m_Comment;
00129     }
00130 }
00131 
00132 void
00133 cmtk::CommandLine::KeyToAction
00134 ::PrintWikiWithPrefix( const std::string& prefix ) const
00135 {
00136   const std::string& typeInfo = this->GetActionTypeInfo();
00137 
00138   StdOut << prefix << "; ";
00139   if ( this->m_Key.m_KeyString.size() )
00140     {
00141     StdOut << "<tt>--" << this->m_Key.m_KeyString << "</tt>";
00142     if ( typeInfo.length() )
00143       {
00144       StdOut << " <tt>" << typeInfo << "</tt>";
00145       }
00146     }
00147 
00148   if ( this->m_Key.m_KeyChar && this->m_Key.m_KeyString.size() )
00149     {
00150     StdOut << " / ";
00151     }
00152 
00153   if ( this->m_Key.m_KeyChar )
00154     {
00155     StdOut << "<tt>-" << this->m_Key.m_KeyChar << "</tt>";
00156     if ( typeInfo.length() )
00157       {
00158       StdOut << " <tt>" << typeInfo << "</tt>";
00159       }
00160     }
00161 
00162   StdOut << " : ";
00163   if ( this->m_Comment.length() )
00164     {
00165     StdOut << this->m_Comment;
00166     }
00167 }
00168 
00169 bool
00170 cmtk::CommandLine::KeyToAction
00171 ::MatchLongOption( const std::string& key ) const
00172 {
00173   if ( key.length() != this->m_Key.m_KeyString.length() )
00174     return false;
00175   
00176   for ( size_t i = 0; i < key.length(); ++i )
00177     {
00178     if ( (key[i] == '-' || key[i] == '_') && (this->m_Key.m_KeyString[i] == '-' || this->m_Key.m_KeyString[i] == '_') )
00179       continue;
00180 
00181     if ( key[i] != this->m_Key.m_KeyString[i] )
00182       return false;
00183     }
00184   return true;
00185 }
00186 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines