CommandLineInfo.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*=========================================================================
  2. Program: WXDialog - wxWidgets X-platform GUI Front-End for CMake
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Author: Jorgen Bodde
  8. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  9. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  10. This software is distributed WITHOUT ANY WARRANTY; without even
  11. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE. See the above copyright notices for more information.
  13. =========================================================================*/
  14. // For compilers that support precompilation, includes "wx/wx.h".
  15. #include "wx/wxprec.h"
  16. #ifdef __BORLANDC__
  17. #pragma hdrstop
  18. #endif
  19. #ifndef WX_PRECOMP
  20. #include "wx/wx.h"
  21. #endif
  22. #include "CommandLineInfo.h"
  23. #include "cmSystemTools.h"
  24. ///////////////////////////////////////////////////////////////
  25. // cmCommandLineInfo
  26. cmCommandLineInfo::cmCommandLineInfo()
  27. {
  28. m_WhereSource = _("");
  29. m_WhereBuild = _("");
  30. m_AdvancedValues = false;
  31. m_GeneratorChoiceString.Empty();
  32. m_LastUnknownParameter = "";
  33. m_ValidArguments = "";
  34. m_ExitAfterLoad = false;
  35. }
  36. ///////////////////////////////////////////////////////////////
  37. cmCommandLineInfo::~cmCommandLineInfo()
  38. {
  39. }
  40. ///////////////////////////////////////////////////////////////
  41. bool cmCommandLineInfo::ParseCommandLine(int argc, char* argv[])
  42. {
  43. bool result = true;
  44. wxString cachePath;
  45. // parse all commands
  46. int cc = 1;
  47. if(argc < cc)
  48. return true; // no command line options
  49. while(cc < argc)
  50. {
  51. wxString arg = argv[cc];
  52. // if we have a switch
  53. if(arg.Len() > 1 && arg.GetChar(0) == '-')
  54. {
  55. int next_argc = ParseSwitch(argv, cc, argc);
  56. if(next_argc > 0)
  57. cc += next_argc;
  58. else
  59. return false; // sorry error while parsing
  60. }
  61. else
  62. {
  63. // gather all what is left
  64. for(int leftcc = cc; leftcc < argc; leftcc++)
  65. {
  66. if(cc != leftcc)
  67. m_WhereBuild << _(" ");
  68. m_WhereBuild << argv[leftcc];
  69. }
  70. break;
  71. }
  72. }
  73. m_ExecutablePath = cmSystemTools::GetFilenamePath(argv[0]).c_str();
  74. return true;
  75. }
  76. ///////////////////////////////////////////////////////////////
  77. int cmCommandLineInfo::GetBoolValue(const wxString& v) {
  78. wxString value = v.Lower();
  79. if (!value.Cmp("1") ||
  80. !value.Cmp("on") ||
  81. !value.Cmp("true") ||
  82. !value.Cmp("yes"))
  83. {
  84. // true
  85. return 1;
  86. }
  87. else if (!value.Cmp("0") ||
  88. !value.Cmp("off") ||
  89. !value.Cmp("false") ||
  90. !value.Cmp("no"))
  91. {
  92. // false
  93. return -1;
  94. }
  95. // not recognised
  96. return 0;
  97. }
  98. ///////////////////////////////////////////////////////////////
  99. // Parse param
  100. size_t cmCommandLineInfo::ParseSwitch(char **argv, int arg_index, int argc)
  101. {
  102. wxString param = argv[arg_index];
  103. // we need this for a switch, at least 2
  104. if(param.Len() > 1)
  105. {
  106. // determine switch type
  107. switch (param.GetChar(1))
  108. {
  109. case 'G':
  110. // when it's G<.....> we split else we take the
  111. // other argc
  112. if(param.Len() > 2)
  113. {
  114. m_GeneratorChoiceString = GetStringParam(param.Mid(2));
  115. return 1; // one arg is passed
  116. }
  117. else
  118. {
  119. if((arg_index+1) < argc)
  120. {
  121. m_GeneratorChoiceString = GetStringParam(wxString(argv[arg_index+1]));
  122. return 2; // two args are passed
  123. }
  124. }
  125. // no luck
  126. return 0;
  127. case 'Q':
  128. m_ExitAfterLoad = true;
  129. return 1;
  130. // unknown param
  131. default:
  132. break;
  133. }
  134. }
  135. // error, unrecognised or too small arg
  136. return 0;
  137. }
  138. // When the string param given has string quotes around it
  139. // we remove them and we pass back the string. If not, we
  140. // simply pass back the string as-is
  141. wxString cmCommandLineInfo::GetStringParam(const wxString &str)
  142. {
  143. wxString mystr = str.Strip(wxString::both);
  144. // if we have only one (or no chars return the current string)
  145. if(mystr.Len() < 2)
  146. return str;
  147. // if we have quotes
  148. if(mystr.GetChar(0) == '\"' && mystr.Last() == '\"')
  149. {
  150. // when we only have 2 in size, return empty string
  151. if(mystr.Len() == 2)
  152. return wxEmptyString;
  153. // now remove the outer and inner, and return
  154. return mystr.Mid(1, mystr.Len()-1);
  155. }
  156. return str;
  157. }