Porytiles
Loading...
Searching...
No Matches
command.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5#include "CLI/CLI.hpp"
6
9
16class Command {
17 public:
18 virtual ~Command() = default;
19
20 Command(CLI::App &parent_app, const std::string &name, const std::string &desc, const std::string &group)
21 : app_(nullptr)
22 {
23 if (name.empty()) {
24 porytiles::panic("Command name cannot be empty.");
25 }
26
27 app_ = parent_app.add_subcommand(name, desc);
28 porytiles::assert_or_panic(app_ != nullptr, "CLI::App::add_subcommand returned nullptr for: " + name);
29
30 // In CLI11, setting group to empty string hides the command from help output
31 app_->group(group);
32
33 app_->callback([this] { this->Run(); });
34 }
35
36 // Prevent copy/move semantics
37 Command(const Command &) = delete;
38 Command &operator=(const Command &) = delete;
39 Command(Command &&) = delete;
40 Command &operator=(Command &&) = delete;
41
42 [[nodiscard]] CLI::App &get_app() const
43 {
44 if (app_ == nullptr) {
45 porytiles::panic("app_ should have been initialized by the constructor");
46 }
47 return *app_;
48 }
49
50 private:
56 virtual void Run() = 0;
57
58 CLI::App *app_;
59};
Command is an abstract class that provides basic command functionality for the Porytiles CLI driver.
Definition command.hpp:16
Command & operator=(const Command &)=delete
Command(const Command &)=delete
Command(Command &&)=delete
virtual ~Command()=default
Command(CLI::App &parent_app, const std::string &name, const std::string &desc, const std::string &group)
Definition command.hpp:20
CLI::App & get_app() const
Definition command.hpp:42
Command & operator=(Command &&)=delete
std::string name
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43
void assert_or_panic(bool condition, const StringViewSourceLoc &s)
Conditionally panics if the given condition is false.
Definition panic.cpp:53