ArgsParser.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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::formatted("--{}", long_name);
  50. return String::formatted("-{: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. bool parse(int argc, char** argv, bool exit_on_failure = true);
  61. // *Without* trailing newline!
  62. void set_general_help(const char* help_string) { m_general_help = help_string; };
  63. void print_usage(FILE*, const char* argv0);
  64. void add_option(Option&&);
  65. void add_option(bool& value, const char* help_string, const char* long_name, char short_name);
  66. void add_option(const char*& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  67. void add_option(String& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  68. void add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  69. void add_option(double& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  70. void add_positional_argument(Arg&&);
  71. void add_positional_argument(const char*& value, const char* help_string, const char* name, Required required = Required::Yes);
  72. void add_positional_argument(String& value, const char* help_string, const char* name, Required required = Required::Yes);
  73. void add_positional_argument(int& value, const char* help_string, const char* name, Required required = Required::Yes);
  74. void add_positional_argument(double& value, const char* help_string, const char* name, Required required = Required::Yes);
  75. void add_positional_argument(Vector<const char*>& value, const char* help_string, const char* name, Required required = Required::Yes);
  76. private:
  77. Vector<Option> m_options;
  78. Vector<Arg> m_positional_args;
  79. bool m_show_help { false };
  80. const char* m_general_help { nullptr };
  81. };
  82. }