MTMS main
Modern Turing Machine Simulator
Loading...
Searching...
No Matches
program.cpp
Go to the documentation of this file.
1
10#include "program.hpp"
11
12#include <cstdlib>
13#include <filesystem>
14#include <print>
15#include <vector>
16
18{
19 std::print(
20 "Usage: mtms-cli [-h|--help] -c <CONFIG> -i <INPUT> [-b|--batch] [-o <OUTPUT>]\n"
21 "Try 'mtms-cli --help' for more information.\n"
22 );
23}
24
26{
27 std::print(
28 "mtms-cli -- a Modern Turing Machine Simulator for CLI.\n\n"
29 "Usage: mtms-cli [options]\n\n"
30 "OPTIONS:\n"
31 " -c, --config <path> Path to the machine configuration TOML file (Required)\n"
32 " -i, --input <string> The input string to load onto Tape 0 (Required)\n"
33 " -b, --batch Run in non-interactive batch mode (Outputs raw summary result)\n"
34 " -o, --output <path> Export the loaded/modified configuration back to a TOML file\n"
35 " -h, --help Display this help instructions menu\n\n"
36 "EXAMPLES:\n"
37 " mtms-cli --config examples/bin_inc.toml --input \"1011\" --batch\n"
38 " mtms-cli -c examples/bin_inc.toml -i \"0111\" -b\n"
39 );
40}
41
42void Program::parse_args(int argc, char *argv[], Program::Options &options)
43{
44 std::vector<std::string_view> args(argv + 1, argv + argc);
45
46 for (auto it = args.begin(), end = args.end(); it != end && !options.show_help; ++it)
47 {
48 if (*it == "-h" || *it == "--help")
49 {
50 options.show_help = true;
51 return;
52 }
53
54 if (*it == "-b" || *it == "--batch")
55 {
56 options.batch_exec = true;
57 continue;
58 }
59
60 if (*it == "-c" || *it == "--config")
61 {
62 if (++it == end)
63 {
64 options.err_msg = "Missing config file parameter.";
65 options.err_code = EXIT_FAILURE;
66 return;
67 }
68
69 std::filesystem::path path = *it;
70 if (path.extension() != ".toml")
71 {
72 options.err_code = EXIT_FAILURE;
73 options.err_msg = "Config file must be a TOML file.\n";
74 return;
75 }
76 options.config_file = *it;
77 continue;
78 }
79
80 if (*it == "-o" || *it == "--output")
81 {
82 if (++it == end)
83 {
84 options.err_msg = "Missing output file parameter.";
85 options.err_code = EXIT_FAILURE;
86 return;
87 }
88
89 std::filesystem::path path = *it;
90 if (path.extension() != ".toml")
91 {
92 path.replace_extension(".toml");
93 }
94 options.output_file = path.string();
95 continue;
96 }
97
98 if (*it == "-i" || *it == "--input")
99 {
100 if (++it == end)
101 {
102 options.err_msg = "Missing input string parameter.";
103 options.err_code = EXIT_FAILURE;
104 return;
105 }
106 options.input = *it;
107 continue;
108 }
109
110 options.err_msg = "Unknown or invalid parameter option rule constraint '" +
111 std::string(*it) + "' passed.";
112 options.err_code = EXIT_FAILURE;
113 return;
114 }
115
116 if (!options.show_help && (options.config_file.empty() || options.input.empty()))
117 {
118 options.err_code = EXIT_FAILURE;
119 options.err_msg = "Missing mandatory options (--config and --input are "
120 "required).";
121 return;
122 }
123}
void parse_args(int argc, char *argv[], Program::Options &options)
Definition program.cpp:42
void print_help()
Definition program.cpp:25
void print_usage()
Definition program.cpp:17
Dedicated header for the main CLI functions.
std::string config_file
Definition program.hpp:21
std::string input
Definition program.hpp:23
std::string output_file
Definition program.hpp:22
std::string err_msg
Definition program.hpp:20