Skip to content

Commit 99971c0

Browse files
committed
remove CLI subcmd run and repl - use "tau <spec>" and just "tau" instead
1 parent dc0863e commit 99971c0

File tree

2 files changed

+70
-138
lines changed

2 files changed

+70
-138
lines changed

external/parser

Submodule parser updated 1 file

src/main.cpp

Lines changed: 69 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -70,56 +70,6 @@ using namespace idni;
7070
using namespace idni::rewriter;
7171
using namespace idni::tau;
7272

73-
cli::commands tau_commands() {
74-
cli::commands cmds;
75-
cmds["help"] = cli::command("help",
76-
"detailed information about options");
77-
78-
auto& run = cmds["run"] = cli::command("run",
79-
"run a tau program");
80-
81-
// common options used by multiple commands
82-
auto indenting_opt = cli::option("indenting", 'I', false)
83-
.set_description("indenting of formulas");
84-
auto highlighting_opt = cli::option("highlighting", 'H', false)
85-
.set_description("syntax highlighting");
86-
auto severity_opt = cli::option("severity", 'S', "info")
87-
.set_description("severity level (trace/debug/info/error)");
88-
auto charvar_opt = cli::option("charvar", 'V', true)
89-
.set_description("charvar (enabled by default)");
90-
#ifdef DEBUG
91-
auto debug_opt = cli::option("debug", 'd', true)
92-
.set_description("debug mode");
93-
run.add_option(debug_opt);
94-
#endif // DEBUG
95-
run.add_option(severity_opt);
96-
run.add_option(indenting_opt);
97-
run.add_option(highlighting_opt);
98-
run.add_option(charvar_opt);
99-
run.add_option(cli::option("program", 'p', "@stdin")
100-
.set_description("program to run"));
101-
run.add_option(cli::option("evaluate", 'e', "")
102-
.set_description("program to be evaluated (alternative to -p)"));
103-
run.add_option(cli::option("help", 'h', false)
104-
.set_description("detailed information about run options"));
105-
auto& repl = cmds["repl"] = cli::command("repl", "Tau REPL");
106-
DBG(repl.add_option(debug_opt);)
107-
repl.add_option(severity_opt);
108-
repl.add_option(indenting_opt);
109-
repl.add_option(highlighting_opt);
110-
repl.add_option(charvar_opt);
111-
repl.add_option(cli::option("help", 'h', false)
112-
.set_description("detailed information about repl options"));
113-
repl.add_option(cli::option("evaluate", 'e', "")
114-
.set_description("repl command to evaluate"));
115-
repl.add_option(cli::option("status", 's', true)
116-
.set_description("display status"));
117-
repl.add_option(cli::option("color", 'c', true)
118-
.set_description("use colors"));
119-
120-
return cmds;
121-
}
122-
12373
cli::options tau_options() {
12474
cli::options opts;
12575
opts["help"] = cli::option("help", 'h', false)
@@ -128,50 +78,54 @@ cli::options tau_options() {
12878
.set_description("show the current Tau executable version");
12979
opts["license"] = cli::option("license", 'l', false)
13080
.set_description("show license for Tau");
131-
return opts;
132-
}
13381

134-
bool is_stdin(const string& s) { return s == "@stdin" || s == "-"; }
135-
bool is_stdout(const string& s) { return s == "@stdout"; }
136-
bool is_null(const string& s) { return s == "@null"; }
137-
int error(const string& s) {
138-
BOOST_LOG_TRIVIAL(error) << "(Error) " << s;
139-
return 1;
82+
opts["charvar"] = cli::option("charvar", 'V', true)
83+
.set_description("charvar (enabled by default)");
84+
opts["severity"] = cli::option("severity", 'S', "info")
85+
.set_description("severity level (trace/debug/info/error)");
86+
opts["indenting"] = cli::option("indenting", 'I', false)
87+
.set_description("indenting of formulas");
88+
opts["highlighting"] = cli::option("highlighting", 'H', false)
89+
.set_description("syntax highlighting");
90+
// REPL specific options
91+
opts["evaluate"] = cli::option("evaluate", 'e', "")
92+
.set_description("REPL command to evaluate");
93+
opts["status"] = cli::option("status", 's', true)
94+
.set_description("display status");
95+
opts["color"] = cli::option("color", 'c', true)
96+
.set_description("use colors");
97+
DBG(opts["debug"] = cli::option("debug", 'd', true)
98+
.set_description("debug mode");)
99+
return opts;
140100
}
141101

142-
// runs tau program or an evaluate string using input and output
143-
int run_tau(const cli::command& cmd, const vector<string>& files) {
144-
string program = cmd.get<string>("program"),
145-
e = cmd.get<string>("evaluate");
146-
if (e.size()&& program.size()&& !is_null(program) && !is_stdin(program))
147-
return error("Cannot use both --program and --evaluate");
148-
if (e.empty()) {
149-
if (files.size()) program = files[0];
150-
if (is_null(program)) return error("Program cannot be null");
151-
if (is_stdin(program)) {
152-
std::ostringstream oss;
153-
oss << std::cin.rdbuf(), e = oss.str();
154-
} else {
155-
std::ifstream ifs(program,
156-
std::ios::binary | std::ios::ate);
157-
if (!ifs) return error("Cannot open file " + program);
158-
auto l = ifs.tellg();
159-
e.resize(l), ifs.seekg(0), ifs.read(&e[0], l);
160-
}
102+
int error(const string& s) {BOOST_LOG_TRIVIAL(error)<< "(Error) "<< s;return 1;}
103+
104+
int run_tau_spec(string spec_file, bool charvar) {
105+
string src = "";
106+
if (spec_file == "-") {
107+
std::ostringstream oss;
108+
oss << std::cin.rdbuf(), src = oss.str();
109+
} else {
110+
std::ifstream ifs(spec_file, std::ios::binary | std::ios::ate);
111+
if (!ifs) return error("Cannot open file " + spec_file);
112+
auto l = ifs.tellg();
113+
src.resize(l), ifs.seekg(0), ifs.read(&src[0], l);
161114
}
162-
163-
repl_evaluator<bdd_binding> re({ .print_memory_store = false,
164-
.error_quits = true,
165-
.charvar = cmd.get<bool>("charvar"),
166-
.repl_running = false });
167-
if (e.empty()) return 0;
168-
if (auto status = re.eval(e); status) return status;
115+
if (src.empty()) return 0;
116+
repl_evaluator<bdd_binding> re({
117+
.print_memory_store = false,
118+
.error_quits = true,
119+
.charvar = charvar,
120+
.repl_running = false
121+
});
122+
if (auto status = re.eval(src); status) return status;
169123
return re.eval("run %");
170124
}
171125

172126
void welcome() {
173127
BOOST_LOG_TRIVIAL(info) << "Welcome to the Tau Language Framework Alpha"
174-
<< " version 0.7 (" << compile_date << " build "
128+
<< " version " <<TAU_VERSION<< " (" << compile_date << " build "
175129
<< GIT_COMMIT_HASH << ") by IDNI AG. "
176130
<< "This product is protected by patents and copyright. "
177131
<< "By using this product, you agree to the license terms. "
@@ -188,72 +142,50 @@ int main(int argc, char** argv) {
188142
vector<string> args;
189143
for (int i = 0; i < argc; i++) args.push_back(argv[i]);
190144

191-
cli cl("tau", args, tau_commands(), "repl", tau_options());
192-
cl.set_description("Tau language");
193-
cl.set_default_command_when_files("run");
194-
145+
cli cl("tau", args, {}, "", tau_options());
146+
cl.set_help_header("Usage: tau [ <specification file> ]");
147+
195148
if (cl.process_args() != 0) return cl.status();
196-
197149
auto opts = cl.get_processed_options();
198-
auto cmd = cl.get_processed_command();
199150
auto files = cl.get_files();
200151

201-
// error if command is invalid
202-
if (!cmd.ok()) {
203-
if (cmd.name() == "repl" && files.size())
204-
return error("repl command does not accept files");
205-
return cl.error("invalid command", true);
206-
}
207-
// if --help/-h option is true, print help end exit
208-
if (cmd.name() == "help" || opts["help"].get<bool>())
209-
return cl.help(), 0;
210-
211-
// if --version/-v option is true, print version and exit
152+
if (opts["help"].get<bool>()) return cl.help(), 0;
212153
if (opts["version"].get<bool>())
213154
return std::cout << "Tau version: " << version, 0;
214-
215-
// if --license/-l option is true, print license and exit
216155
if (opts["license"].get<bool>()) return std::cout << license, 0;
217156

218-
// if cmd's --help/-h option is true, print cmd's help and exit
219-
if (cmd.get<bool>("help")) return cl.help(cmd), 0;
157+
std::string sevstr = opts["severity"].get<string>();
158+
boost::log::trivial::severity_level sev =
159+
sevstr == "error" ? boost::log::trivial::error :
160+
sevstr == "trace" ? boost::log::trivial::trace :
161+
sevstr == "debug" ? boost::log::trivial::debug :
162+
boost::log::trivial::info;
163+
164+
pretty_printer_highlighting = opts["highlighting"].get<bool>();
165+
pretty_printer_indenting = opts["indenting"].get<bool>();
220166

221-
// set charvar
222-
bool charvar = cmd.get<bool>("charvar");
167+
bool charvar = opts["charvar"].get<bool>();
223168
std::set<std::string> guards{ charvar ? "charvar" : "var" };
224169
tau_parser::instance().get_grammar().set_enabled_productions(guards);
225170
bdd_parser::instance().get_grammar().set_enabled_productions(guards);
226171

227-
pretty_printer_highlighting = cmd.get<bool>("highlighting");
228-
pretty_printer_indenting = cmd.get<bool>("indenting");
172+
// spec provided, run it
173+
if (files.size()) return run_tau_spec(files.front(), charvar);
229174

230-
std::string sevstr = cmd.get<string>("severity");
231-
boost::log::trivial::severity_level sev =
232-
sevstr == "error" ? boost::log::trivial::error :
233-
sevstr == "trace" ? boost::log::trivial::trace :
234-
sevstr == "debug" ? boost::log::trivial::debug :
235-
boost::log::trivial::info;
236-
237-
// repl command
238-
if (cmd.name() == "repl") {
239-
string e = cmd.get<string>("evaluate");
240-
repl_evaluator<bdd_binding> re({
241-
.status = cmd.get<bool>("status"),
242-
.colors = cmd.get<bool>("color"),
243-
.charvar = charvar,
175+
// REPL
176+
repl_evaluator<bdd_binding> re({
177+
.status = opts["status"].get<bool>(),
178+
.colors = opts["color"].get<bool>(),
179+
.charvar = charvar,
244180
#ifdef DEBUG
245-
.debug_repl = cmd.get<bool>("debug"),
181+
.debug_repl = opts["debug"].get<bool>(),
246182
#endif // DEBUG
247-
.severity = sev
248-
});
249-
if (e.size()) return re.eval(e), 0;
250-
repl<decltype(re)> r(re, "tau> ", ".tau_history");
251-
welcome();
252-
re.prompt();
253-
return r.run();
254-
}
255-
256-
// run command
257-
if (cmd.name() == "run") return run_tau(cmd, files);
258-
return 0;
183+
.severity = sev
184+
});
185+
string e = opts["evaluate"].get<string>();
186+
if (e.size()) return re.eval(e), 0;
187+
repl<decltype(re)> r(re, "tau> ", ".tau_history");
188+
welcome();
189+
re.prompt();
190+
return r.run();
259191
}

0 commit comments

Comments
 (0)