Skip to content

Commit 5b2f844

Browse files
committed
Add -help and use instructions
1 parent 2b49695 commit 5b2f844

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

examples/repl/repl.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,28 @@
77

88
namespace {
99
struct Repl {
10-
const std::string_view EXIT_TOKEN{"exit"};
1110
bool verbose{}; // toggled on with '-v' and off with '-q'
1211
bool is_running{true};
1312

13+
auto print_help() -> void {
14+
std::cout << "Usage: [OPTION] | [EXPRESSION]\n";
15+
std::cout << "\nDescription:\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+
1421
auto start() -> bool {
1522
while (is_running) {
1623
std::string text{};
17-
std::cout << std::format("[{}] > ", verbose ? "verbose" : "quiet");
24+
std::cout << std::format("{} > ", verbose ? "[verbose]" : "");
1825
std::getline(std::cin, text);
1926
// 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+
}
2132
}
2233
// print epilogue
2334
std::cout << std::format("\n^^ kalcy v{}\n", kalcy::version_v);
@@ -26,12 +37,13 @@ struct Repl {
2637

2738
auto run(std::string_view const text) -> bool {
2839
try {
29-
// return false when the user enters the exit token
30-
if (text == EXIT_TOKEN) {
40+
if (text == "exit") {
3141
is_running = false;
3242
} else if (text == "-v") {
3343
verbose = !verbose;
34-
} else {
44+
} else if (text == "-h" || text == "-help") {
45+
print_help();
46+
} else {
3547
// parse text into an expression
3648
auto expr = kalcy::Parser{}.parse(text);
3749
assert(expr != nullptr);
@@ -53,8 +65,8 @@ struct Repl {
5365
};
5466
} // namespace
5567

56-
auto main(int argc, char** argv) -> int {
68+
auto main() -> int {
5769

5870
Repl repl{};
59-
return repl.start();
71+
repl.start();
6072
}

0 commit comments

Comments
 (0)