You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.9KB

  1. /************************************************************************/
  2. /*! \class CommandLine
  3. \brief Command-line option parser.
  4. Copyright (c) 2005 Robin Davies.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  18. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. /************************************************************************/
  23. #include ".\stdopt.h"
  24. using namespace stdopt;
  25. CommandLine::CommandLine()
  26. {
  27. }
  28. void CommandLine::ProcessCommandLine(int argc, char**argv)
  29. {
  30. std::vector<std::string> cmdline;
  31. for (int i = 0; i < argc; ++i) {
  32. cmdline.push_back(argv[i]);
  33. }
  34. ProcessCommandLine(cmdline);
  35. }
  36. const CommandLine::COptionHandlerBase*CommandLine::GetOptionHandler(const std::string &name) const
  37. {
  38. // Return excact matches only.
  39. for (size_t i = 0; i < optionHandlers.size(); ++i)
  40. {
  41. if (optionHandlers[i]->getName() == name) {
  42. return (optionHandlers[i]);
  43. }
  44. }
  45. return NULL;
  46. }
  47. void CommandLine::ProcessCommandLine(const std::vector<std::string>& cmdline)
  48. {
  49. for (size_t i = 1; i < cmdline.size(); ++i)
  50. {
  51. if (cmdline[i].length() != 0 && cmdline[i][0] == L'/' || (cmdline[i][0] == '-')) {
  52. std::string arg = cmdline[i].substr(1);
  53. const COptionHandlerBase *pHandler = GetOptionHandler(arg);
  54. if (pHandler == NULL) {
  55. throw CommandLineException(std::string("Unknown option: ") + arg);
  56. }
  57. if (pHandler->HasArgument())
  58. {
  59. std::string strArg;
  60. if (i+1 < cmdline.size()) {
  61. ++i;
  62. strArg = cmdline[i];
  63. }
  64. pHandler->Process(strArg.c_str());
  65. } else {
  66. pHandler->Process(NULL);
  67. }
  68. } else {
  69. args.push_back(cmdline[i]);
  70. }
  71. }
  72. }
  73. CommandLine::~CommandLine(void)
  74. {
  75. for (size_t i = 0; i < optionHandlers.size(); ++i)
  76. {
  77. delete optionHandlers[i];
  78. }
  79. optionHandlers.resize(0);
  80. }