1 /*
2    mkvpropedit -- utility for editing properties of existing Matroska files
3 
4    Distributed under the GPL v2
5    see the file COPYING for details
6    or visit https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7 
8    Written by Moritz Bunkus <moritz@bunkus.org>.
9 */
10 
11 #pragma once
12 
13 #include "common/common_pch.h"
14 
15 #include "common/translation.h"
16 
17 namespace mtx::cli {
18 
19 constexpr auto INDENT_DEFAULT = -1;
20 
21 using parser_cb_t = std::function<void(void)>;
22 
23 class parser_c {
24 protected:
25   struct option_t {
26     enum option_type_e {
27       ot_option,
28       ot_section_header,
29       ot_information,
30       ot_informational_option,
31     };
32 
33     option_type_e m_type;
34     std::string m_spec, m_name;
35     translatable_string_c m_description;
36     parser_cb_t m_callback;
37     bool m_needs_arg;
38     int m_indent;
39 
40     option_t();
41     option_t(option_type_e type, translatable_string_c description, int indent = INDENT_DEFAULT);
42     option_t(std::string spec, translatable_string_c description, parser_cb_t callback, bool needs_arg);
43     option_t(std::string name, translatable_string_c description);
44 
45     std::string format_text();
46   };
47 
48   enum hook_type_e {
49     ht_common_options_parsed,
50     ht_unknown_option,
51   };
52 
53   std::map<std::string, option_t> m_option_map;
54   std::vector<option_t> m_options;
55   std::vector<std::string> m_args;
56 
57   std::string m_current_arg, m_next_arg;
58 
59   std::map<hook_type_e, std::vector<parser_cb_t>> m_hooks;
60 
61   bool m_no_common_cli_args;
62 
63 protected:
64   parser_c(std::vector<std::string> args);
65 
66   void add_option(std::string const &spec, parser_cb_t const &callback, translatable_string_c description);
67   void add_informational_option(std::string const &name, translatable_string_c description);
68   void add_section_header(translatable_string_c const &title, int indent = INDENT_DEFAULT);
69   void add_information(translatable_string_c const &information, int indent = INDENT_DEFAULT);
70   void add_separator();
71   void add_common_options();
72 
73   void parse_args();
74   void set_usage();
75 
76   void dummy_callback();
77 
78   void add_hook(hook_type_e hook_type, parser_cb_t const &callback);
79   bool run_hooks(hook_type_e hook_type);
80 };
81 
82 }
83