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
18class Command {
19 public:
20 virtual ~Command() = default;
21
22 Command(CLI::App &parent_app, const std::string &name, const std::string &desc, const std::string &group)
23 : app_(nullptr)
24 {
25 if (name.empty()) {
26 porytiles::panic("Command name cannot be empty.");
27 }
28
29 app_ = parent_app.add_subcommand(name, desc);
30 porytiles::assert_or_panic(app_ != nullptr, "CLI::App::add_subcommand returned nullptr for: " + name);
31
32 // In CLI11, setting group to empty string hides the command from help output
33 app_->group(group);
34
35 app_->callback([this] { this->Run(); });
36 }
37
38 // Prevent copy/move semantics
39 Command(const Command &) = delete;
40 Command &operator=(const Command &) = delete;
41 Command(Command &&) = delete;
42 Command &operator=(Command &&) = delete;
43
44 [[nodiscard]] CLI::App &get_app() const
45 {
46 if (app_ == nullptr) {
47 porytiles::panic("app_ should have been initialized by the constructor");
48 }
49 return *app_;
50 }
51
52 protected:
53 virtual void Run() = 0;
54
55 private:
56 CLI::App *app_;
57};
Command is an abstract class that provides basic command functionality for the Porytiles CLI driver.
Definition command.hpp:18
Command & operator=(const Command &)=delete
Command(const Command &)=delete
virtual void Run()=0
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:22
CLI::App & get_app() const
Definition command.hpp:44
Command & operator=(Command &&)=delete
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