ArgsParser.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/String.h>
  9. #include <AK/Vector.h>
  10. #include <stdio.h>
  11. namespace Core {
  12. class ArgsParser {
  13. public:
  14. ArgsParser();
  15. enum class Required {
  16. Yes,
  17. No
  18. };
  19. enum class FailureBehavior {
  20. PrintUsageAndExit,
  21. PrintUsage,
  22. Exit,
  23. Ignore,
  24. };
  25. struct Option {
  26. bool requires_argument { true };
  27. const char* help_string { nullptr };
  28. const char* long_name { nullptr };
  29. char short_name { 0 };
  30. const char* value_name { nullptr };
  31. Function<bool(const char*)> accept_value;
  32. String name_for_display() const
  33. {
  34. if (long_name)
  35. return String::formatted("--{}", long_name);
  36. return String::formatted("-{:c}", short_name);
  37. }
  38. };
  39. struct Arg {
  40. const char* help_string { nullptr };
  41. const char* name { nullptr };
  42. int min_values { 0 };
  43. int max_values { 1 };
  44. Function<bool(const char*)> accept_value;
  45. };
  46. bool parse(int argc, char* const* argv, FailureBehavior failure_behavior = FailureBehavior::PrintUsageAndExit);
  47. // *Without* trailing newline!
  48. void set_general_help(const char* help_string) { m_general_help = help_string; };
  49. void set_stop_on_first_non_option(bool stop_on_first_non_option) { m_stop_on_first_non_option = stop_on_first_non_option; }
  50. void print_usage(FILE*, const char* argv0);
  51. void add_option(Option&&);
  52. void add_option(bool& value, const char* help_string, const char* long_name, char short_name);
  53. void add_option(const char*& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  54. void add_option(String& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  55. void add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
  56. void add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  57. void add_option(double& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
  58. void add_positional_argument(Arg&&);
  59. void add_positional_argument(const char*& value, const char* help_string, const char* name, Required required = Required::Yes);
  60. void add_positional_argument(String& value, const char* help_string, const char* name, Required required = Required::Yes);
  61. void add_positional_argument(StringView& value, char const* help_string, char const* name, Required required = Required::Yes);
  62. void add_positional_argument(int& value, const char* help_string, const char* name, Required required = Required::Yes);
  63. void add_positional_argument(double& value, const char* help_string, const char* name, Required required = Required::Yes);
  64. void add_positional_argument(Vector<const char*>& value, const char* help_string, const char* name, Required required = Required::Yes);
  65. void add_positional_argument(Vector<String>& value, const char* help_string, const char* name, Required required = Required::Yes);
  66. private:
  67. Vector<Option> m_options;
  68. Vector<Arg> m_positional_args;
  69. bool m_show_help { false };
  70. const char* m_general_help { nullptr };
  71. bool m_stop_on_first_non_option { false };
  72. };
  73. }