00001
00014 #include <sstream>
00015 #include <dlrUtilities/stringManipulation.h>
00016 #include <dlrUtilities/optionDescription.h>
00017
00018 namespace dlr {
00019
00020 namespace utilities {
00021
00022
00023 OptionDescription::
00024 OptionDescription()
00025 : m_name(),
00026 m_shortAppearance(),
00027 m_longAppearance(),
00028 m_requiresValue(),
00029 m_allowPartialMatch(),
00030 m_docString(),
00031 m_defaultValue()
00032 {
00033
00034 }
00035
00036
00037
00038 OptionDescription::
00039 OptionDescription(const std::string& name,
00040 const std::string& shortAppearance,
00041 const std::string& longAppearance,
00042 bool requiresValue,
00043 bool allowPartialMatch,
00044 const std::string& docString,
00045 const std::string& defaultValue)
00046 : m_name(name),
00047 m_shortAppearance(shortAppearance),
00048 m_longAppearance(longAppearance),
00049 m_requiresValue(requiresValue),
00050 m_allowPartialMatch(allowPartialMatch),
00051 m_docString(docString),
00052 m_defaultValue(defaultValue)
00053 {
00054
00055 }
00056
00057
00058
00059 OptionDescription::
00060 ~OptionDescription()
00061 {
00062
00063 }
00064
00065
00066 size_t
00067 OptionDescription::
00068 getMatchLength(const std::string& argument,
00069 bool& isShortMatch) const
00070 {
00071 if(m_shortAppearance.size() != 0) {
00072 if((argument.size() == m_shortAppearance.size())
00073 && (argument == m_shortAppearance)) {
00074 isShortMatch = true;
00075 return argument.size();
00076 }
00077 if((argument.size() > m_shortAppearance.size())
00078 && (argument.compare(0, m_shortAppearance.size(), m_shortAppearance)
00079 == 0)) {
00080 isShortMatch = true;
00081 return m_shortAppearance.size();
00082 }
00083 }
00084 if(m_longAppearance.size() != 0) {
00085
00086 std::vector<std::string> argumentParts =
00087 splitString(argument, "=", true, 1);
00088 if(argumentParts[0].size() == m_longAppearance.size()
00089 && argumentParts[0] == m_longAppearance) {
00090 if(argumentParts.size() == 1) {
00091 isShortMatch = false;
00092 return m_longAppearance.size();
00093 } else {
00094 isShortMatch = false;
00095 return m_longAppearance.size() + 1;
00096 }
00097 }
00098 if(m_allowPartialMatch
00099 && argumentParts[0].size() > 0
00100 && argumentParts[0].size() < m_longAppearance.size()
00101 && (m_longAppearance.compare(0, argumentParts[0].size(), argument)
00102 == 0)) {
00103 if(argumentParts.size() == 1) {
00104 isShortMatch = false;
00105 return argumentParts[0].size();
00106 } else {
00107 isShortMatch = false;
00108 return argumentParts[0].size() + 1;
00109 }
00110 }
00111 }
00112 isShortMatch = false;
00113 return 0;
00114 }
00115
00116
00117 std::string
00118 OptionDescription::
00119 getOptionDoc() const
00120 {
00121 std::string indentString = " ";
00122 std::ostringstream docStream;
00123 if(m_shortAppearance != "") {
00124 if(m_requiresValue) {
00125 docStream << m_shortAppearance << " " << m_name;
00126 } else {
00127 docStream << m_shortAppearance;
00128 }
00129 }
00130 if(m_shortAppearance != "" && m_longAppearance != "") {
00131 docStream << ", ";
00132 }
00133 if(m_longAppearance != "") {
00134 if(m_requiresValue) {
00135 docStream << m_longAppearance << "=" << m_name;
00136 } else {
00137 docStream << m_longAppearance;
00138 }
00139 }
00140 docStream << "\n"
00141 << wrapString(indentString + m_docString, indentString);
00142 if(m_defaultValue != "") {
00143 docStream << "\n"
00144 << wrapString(indentString + "Default value: \""
00145 + m_defaultValue + "\"", indentString);
00146 }
00147 return docStream.str();
00148 }
00149
00150
00151 bool
00152 OptionDescription::
00153 isMatch(const std::string& argument) const
00154 {
00155 bool dummy;
00156 return (this->getMatchLength(argument, dummy) != 0);
00157 }
00158
00159
00160 std::ostream&
00161 operator<<(std::ostream& stream,
00162 const OptionDescription& optionDescription)
00163 {
00164 stream << " " << optionDescription.getOptionDoc();
00165 return stream;
00166 }
00167
00168
00169 }
00170
00171 }