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 porytiles2::panic("Command name cannot be empty.");
27 }
28
29 app_ = parent_app.add_subcommand(name, desc);
30 porytiles2::assert_or_panic(app_ != nullptr, "CLI::App::add_subcommand returned nullptr for: " + name);
31
32 if (!group.empty()) {
33 app_->group(group);
34 }
35
36 app_->callback([this] { this->Run(); });
37 }
38
39 // Prevent copy/move semantics
40 Command(const Command &) = delete;
41 Command &operator=(const Command &) = delete;
42 Command(Command &&) = delete;
43 Command &operator=(Command &&) = delete;
44
45 [[nodiscard]] CLI::App &get_app() const
46 {
47 if (app_ == nullptr) {
48 porytiles2::panic("app_ should have been initialized by the constructor");
49 }
50 return *app_;
51 }
52
53 protected:
54 virtual void Run() = 0;
55
56 private:
57 CLI::App *app_;
58};
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:45
Command & operator=(Command &&)=delete
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.hpp:53
void assert_or_panic(const bool condition, const StringViewSourceLoc &s)
Conditionally panics if the given condition is false.
Definition panic.hpp:70