7
7
8
8
namespace {
9
9
struct Repl {
10
- const std::string_view EXIT_TOKEN{" exit" };
11
10
bool verbose{}; // toggled on with '-v' and off with '-q'
12
11
bool is_running{true };
13
12
13
+ auto print_help () -> void {
14
+ std::cout << " Usage: [OPTION] | [EXPRESSION]\n " ;
15
+ std::cout << " \n Description:\n\n " ;
16
+ std::cout << std::format (" {:<15} {}\n " , " -h, --help" , " Display this help message, providing information about available options." );
17
+ std::cout << std::format (" {:<15} {}\n " , " -v" , " Toggle verbose mode to control the level of detail in the output." );
18
+ std::cout << std::format (" {:<15} {}\n " , " exit" , " Terminate the REPL input loop." );
19
+ }
20
+
14
21
auto start () -> bool {
15
22
while (is_running) {
16
23
std::string text{};
17
- std::cout << std::format (" [{}] > " , verbose ? " verbose" : " quiet " );
24
+ std::cout << std::format (" {} > " , verbose ? " [ verbose] " : " " );
18
25
std::getline (std::cin, text);
19
26
// run kalcy on input expression
20
- if (!run (text)) { return EXIT_FAILURE; }
27
+ if (!text.empty ()) {
28
+ if (!run (text)) { return EXIT_FAILURE; }
29
+ } else {
30
+ print_help ();
31
+ }
21
32
}
22
33
// print epilogue
23
34
std::cout << std::format (" \n ^^ kalcy v{}\n " , kalcy::version_v);
@@ -26,12 +37,13 @@ struct Repl {
26
37
27
38
auto run (std::string_view const text) -> bool {
28
39
try {
29
- // return false when the user enters the exit token
30
- if (text == EXIT_TOKEN) {
40
+ if (text == " exit" ) {
31
41
is_running = false ;
32
42
} else if (text == " -v" ) {
33
43
verbose = !verbose;
34
- } else {
44
+ } else if (text == " -h" || text == " -help" ) {
45
+ print_help ();
46
+ } else {
35
47
// parse text into an expression
36
48
auto expr = kalcy::Parser{}.parse (text);
37
49
assert (expr != nullptr );
@@ -53,8 +65,8 @@ struct Repl {
53
65
};
54
66
} // namespace
55
67
56
- auto main (int argc, char ** argv ) -> int {
68
+ auto main () -> int {
57
69
58
70
Repl repl{};
59
- return repl.start ();
71
+ repl.start ();
60
72
}
0 commit comments