ArgsParser.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Function.h>
  28. #include <AK/String.h>
  29. #include <AK/Vector.h>
  30. #include <stdio.h>
  31. namespace Core {
  32. class ArgsParser {
  33. public:
  34. ArgsParser();
  35. enum class Required {
  36. Yes,
  37. No
  38. };
  39. struct Option {
  40. bool requires_argument { true };
  41. const char* help_string { nullptr };
  42. const char* long_name { nullptr };
  43. char short_name { 0 };
  44. const char* value_name { nullptr };
  45. Function<bool(const char*)> accept_value;
  46. String name_for_display() const
  47. {
  48. if (long_name)
  49. return String::format("--%s", long_name);
  50. return String::format("-%c", short_name);
  51. }
  52. };
  53. struct Arg {
  54. const char* help_string { nullptr };
  55. const char* name { nullptr };
  56. int min_values { 0 };
  57. int max_values { 1 };
  58. Function<bool(const char*)> accept_value;
  59. };
  60. void parse(int argc, char** argv);
  61. void print_usage(FILE*, const char* argv0);
  62. void add_option(Option&&);
  63. void add_option(bool& value, const char* help_string, const char* long_name, char short_name);
  64. void add_option(const char*& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  65. void add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  66. void add_positional_argument(Arg&&);
  67. void add_positional_argument(const char*& value, const char* help_string, const char* name, Required required = Required::Yes);
  68. void add_positional_argument(int& value, const char* help_string, const char* name, Required required = Required::Yes);
  69. void add_positional_argument(Vector<const char*>& value, const char* help_string, const char* name, Required required = Required::Yes);
  70. private:
  71. Vector<Option> m_options;
  72. Vector<Arg> m_positional_args;
  73. bool m_show_help { false };
  74. };
  75. }