cmtkCommandLineKeyToActionEnum.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 void
00035 cmtk::CommandLine::KeyToActionEnum
00036 ::PrintHelp( const size_t globalIndent ) const
00037 {
00038   std::ostringstream fmt;
00039   this->Superclass::FormatHelp( fmt );
00040 
00041   fmt << "\nSupported values: ";
00042   for ( EnumGroupBase::const_iterator it = this->m_EnumGroup->begin(); it != this->m_EnumGroup->end(); ++it )
00043     {
00044     fmt << "\"" << (*it)->m_Key.m_KeyString << "\", ";
00045     }
00046   
00047   const std::string defaultKey = this->m_EnumGroup->GetDefaultKey();
00048   if ( defaultKey.length() )
00049     {
00050     fmt << "where the default is \"" << defaultKey << "\", ";
00051     }
00052   
00053   fmt << "or use one of the following";
00054   
00055   StdErr.FormatText( fmt.str(), CommandLine::HelpTextIndent + globalIndent, StdErr.GetLineWidth(), -CommandLine::HelpTextIndent ) << "\n";  
00056   
00057   for ( EnumGroupBase::const_iterator it = this->m_EnumGroup->begin(); it != this->m_EnumGroup->end(); ++it )
00058     {
00059     (*it)->PrintHelp( globalIndent + 10 );
00060     }
00061 }
00062 
00063 void
00064 cmtk::CommandLine::KeyToActionEnum
00065 ::PrintWikiWithPrefix( const std::string& prefix ) const
00066 {
00067   this->Superclass::PrintWikiWithPrefix( prefix );
00068 
00069   StdOut << "Supported values: ";
00070   for ( EnumGroupBase::const_iterator it = this->m_EnumGroup->begin(); it != this->m_EnumGroup->end(); ++it )
00071     {
00072     StdOut << "\"<tt>" << (*it)->m_Key.m_KeyString << "</tt>\", ";
00073     }
00074   
00075   const std::string defaultKey = this->m_EnumGroup->GetDefaultKey();
00076   if ( defaultKey.length() )
00077     {
00078     StdOut << "where the default is \"" << defaultKey << "\", ";
00079     }
00080   
00081   StdOut << "or use one of the following\n";
00082   
00083   for ( EnumGroupBase::const_iterator it = this->m_EnumGroup->begin(); it != this->m_EnumGroup->end(); ++it )
00084     {
00085     (*it)->PrintWikiWithPrefix( ":" );
00086     }
00087 }
00088 
00089 mxml_node_t*
00090 cmtk::CommandLine::KeyToActionEnum
00091 ::MakeXML( mxml_node_t *const parent ) const
00092 {
00093   if ( ! (this->m_Properties & PROPS_NOXML) )
00094     {
00095     mxml_node_t *node = mxmlNewElement( parent, "string-enumeration" );
00096     
00097     mxml_node_t* defaultElement = mxmlNewElement( node, "default" );
00098     mxmlNewText( defaultElement, 0, this->m_EnumGroup->GetDefaultKey().c_str() );
00099     
00100     for ( EnumGroupBase::const_iterator it = this->m_EnumGroup->begin(); it != this->m_EnumGroup->end(); ++it )
00101       {      
00102       mxml_node_t* element = mxmlNewElement( node, "element" );
00103       mxmlNewText( element, 0, (*it)->m_Key.m_KeyString.c_str() );
00104       }
00105     
00106     return this->Superclass::MakeXML( node );
00107     }
00108   return NULL;
00109 }
00110 
00111 bool
00112 cmtk::CommandLine::KeyToActionEnum
00113 ::MatchAndExecute( const std::string& key, const size_t argc, const char* argv[], size_t& index )
00114 {
00115   if ( this->MatchLongOption( std::string( key ) ) )
00116     {
00117     // check if optional argument matches suboption
00118     if ( this->m_EnumGroup )
00119       {      
00120       for ( EnumGroupBase::iterator it = this->m_EnumGroup->begin(); it != this->m_EnumGroup->end(); ++it )
00121         {
00122         size_t ii = index+1;
00123         if ( (*it)->MatchAndExecute( argv[ii], argc, argv, ii ) )
00124           {
00125           index = ii;
00126           return true;
00127           }
00128         }
00129       }
00130     }
00131 
00132   // also check for direct matches with the suboptions
00133   if ( this->m_EnumGroup )
00134     {
00135     for ( EnumGroupBase::iterator it = this->m_EnumGroup->begin(); it != this->m_EnumGroup->end(); ++it )
00136       {
00137       if ( (*it)->MatchAndExecute( key, argc, argv, index ) )
00138         {
00139         return true;
00140         }
00141       }
00142     }
00143   
00144   return false;
00145 }
00146 
00147 bool
00148 cmtk::CommandLine::KeyToActionEnum
00149 ::MatchAndExecute( const char keyChar, const size_t argc, const char* argv[], size_t& index )
00150 {
00151   // check if optional argument matches suboption
00152   for ( EnumGroupBase::iterator it = this->m_EnumGroup->begin(); it != this->m_EnumGroup->end(); ++it )
00153     {
00154     size_t ii = index+1;
00155     if ( (*it)->MatchAndExecute( argv[ii], argc, argv, ii ) )
00156       {
00157       index = ii;
00158       return true;
00159       }
00160     }
00161   
00162   // also check for direct matches with the suboptions
00163   for ( EnumGroupBase::iterator it = this->m_EnumGroup->begin(); it != this->m_EnumGroup->end(); ++it )
00164     {
00165     if ( (*it)->MatchAndExecute( keyChar, argc, argv, index ) )
00166       {
00167       return true;
00168       }
00169     }
00170 
00171   return false;
00172 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines