Porytiles
Loading...
Searching...
No Matches
command.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <string>
5
6#include <CLI/CLI.hpp>
7
9
10#include "./option.hpp"
11#include "./option_group.hpp"
12
15class Command {
16 public:
17 virtual ~Command() = default;
18
19 Command(CLI::App &parent_app, const std::string &name, const std::string &desc, const std::string &group)
20 : app_(nullptr) {
21 if (name.empty()) {
22 porytiles::Panic("Command name cannot be empty.");
23 }
24
25 app_ = parent_app.add_subcommand(name, desc);
26 porytiles::AssertOrPanic(app_ != nullptr, "CLI::App::add_subcommand returned nullptr for: " + name);
27
28 if (!group.empty()) {
29 app_->group(group);
30 }
31
32 app_->callback([this] { this->Run(); });
33 }
34
35 // Prevent copy/move semantics
36 Command(const Command &) = delete;
37 Command &operator=(const Command &) = delete;
38 Command(Command &&) = delete;
39 Command &operator=(Command &&) = delete;
40
41 [[nodiscard]] CLI::App &get_app() const {
42 if (app_ == nullptr) {
43 porytiles::Panic("app_ should have been initialized by the constructor");
44 }
45 return *app_;
46 }
47
48 protected:
49 virtual void Run() = 0;
50
51 private:
52 CLI::App *app_;
53};
54
55class CompileTilesetCommand final : public Command {
56 public:
57 explicit CompileTilesetCommand(CLI::App &parent_app)
58 : Command{parent_app, kCommandName, kCommandDesc, kCommandGroup} {
59 CLI::App &cmd = get_app();
60 fieldmap_opts_.RegisterGroup(cmd);
61 diagnostics_opts_.RegisterGroup(cmd);
62 }
63
64 void Run() override {
65 std::cout << "Compile tileset command called." << std::endl;
66 }
67
68 private:
69 static constexpr auto kCommandName = "compile-tileset";
70 static constexpr auto kCommandDesc = "Compile a Porytiles-format tileset to a Porymap-format tileset.";
71 static constexpr auto kCommandGroup = "COMMANDS";
72
73 OptGroupFieldmap fieldmap_opts_;
74 OptGroupDiagnostics diagnostics_opts_;
75};
76
77class CompileLayoutCommand final : public Command {
78 public:
79 explicit CompileLayoutCommand(CLI::App &parent_app)
80 : Command{parent_app, kCommandName, kCommandDesc, kCommandGroup} {
81 CLI::App &cmd = get_app();
82 fieldmap_opts_.RegisterGroup(cmd);
83 diagnostics_opts_.RegisterGroup(cmd);
84 }
85
86 void Run() override {
87 std::cout << "Compile layout command called." << std::endl;
88 }
89
90 private:
91 static constexpr auto kCommandName = "compile-layout";
92 static constexpr auto kCommandDesc = "Compile a Porytiles-format layout to a Porymap-format layout.";
93 static constexpr auto kCommandGroup = "COMMANDS";
94
95 OptGroupFieldmap fieldmap_opts_;
96 OptGroupDiagnostics diagnostics_opts_;
97};
98
99class CompileSpritesheetCommand final : public Command {
100 public:
101 explicit CompileSpritesheetCommand(CLI::App &parent_app)
102 : Command{parent_app, kCommandName, kCommandDesc, kCommandGroup} {
103 CLI::App &cmd = get_app();
104 fieldmap_opts_.RegisterGroup(cmd);
105 diagnostics_opts_.RegisterGroup(cmd);
106 }
107
108 void Run() override {
109 std::cout << "Compile spritesheet command called." << std::endl;
110 }
111
112 private:
113 static constexpr auto kCommandName = "compile-spritesheet";
114 static constexpr auto kCommandDesc = "Compile a Porytiles-format spritesheet to a Porymap-format spritesheet.";
115 static constexpr auto kCommandGroup = "COMMANDS";
116
117 OptGroupFieldmap fieldmap_opts_;
118 OptGroupDiagnostics diagnostics_opts_;
119};
120
121class DecompileTilesetCommand final : public Command {
122 public:
123 explicit DecompileTilesetCommand(CLI::App &parent_app)
124 : Command{parent_app, kCommandName, kCommandDesc, kCommandGroup} {
125 CLI::App &cmd = get_app();
126 fieldmap_opts_.RegisterGroup(cmd);
127 diagnostics_opts_.RegisterGroup(cmd);
128 }
129
130 void Run() override {
131 std::cout << "Decompile tileset command called." << std::endl;
132 }
133
134 private:
135 static constexpr auto kCommandName = "decompile-tileset";
136 static constexpr auto kCommandDesc = "Decompile a Porymap-format tileset back to a Porytiles-format tileset.";
137 static constexpr auto kCommandGroup = "COMMANDS";
138
139 OptGroupFieldmap fieldmap_opts_;
140 OptGroupDiagnostics diagnostics_opts_;
141};
142
143class DecompileLayoutCommand final : public Command {
144 public:
145 explicit DecompileLayoutCommand(CLI::App &parent_app)
146 : Command{parent_app, kCommandName, kCommandDesc, kCommandGroup} {
147 CLI::App &cmd = get_app();
148 fieldmap_opts_.RegisterGroup(cmd);
149 diagnostics_opts_.RegisterGroup(cmd);
150 }
151
152 void Run() override {
153 std::cout << "Decompile layout command called." << std::endl;
154 }
155
156 private:
157 static constexpr auto kCommandName = "decompile-layout";
158 static constexpr auto kCommandDesc = "Decompile a Porymap-format layout back to a Porytiles-format layout.";
159 static constexpr auto kCommandGroup = "COMMANDS";
160
161 OptGroupFieldmap fieldmap_opts_;
162 OptGroupDiagnostics diagnostics_opts_;
163};
164
165class ReduceBitDepthCommand final : public Command {
166 public:
167 explicit ReduceBitDepthCommand(CLI::App &parent_app)
168 : Command{parent_app, kCommandName, kCommandDesc, kCommandGroup} {}
169
170 void Run() override {
171 std::cout << "Reduce bit depth command called." << std::endl;
172 }
173
174 private:
175 static constexpr auto kCommandName = "reduce-bit-depth";
176 static constexpr auto kCommandDesc = "Reduce bit depth for given input assets.";
177 static constexpr auto kCommandGroup = "COMMANDS";
178};
Command is an abstract class that provides basic command functionality for the Porytiles CLI driver.
Definition command.hpp:15
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:19
CLI::App & get_app() const
Definition command.hpp:41
Command & operator=(Command &&)=delete
CompileLayoutCommand(CLI::App &parent_app)
Definition command.hpp:79
void Run() override
Definition command.hpp:86
void Run() override
Definition command.hpp:108
CompileSpritesheetCommand(CLI::App &parent_app)
Definition command.hpp:101
void Run() override
Definition command.hpp:64
CompileTilesetCommand(CLI::App &parent_app)
Definition command.hpp:57
DecompileLayoutCommand(CLI::App &parent_app)
Definition command.hpp:145
void Run() override
Definition command.hpp:152
void Run() override
Definition command.hpp:130
DecompileTilesetCommand(CLI::App &parent_app)
Definition command.hpp:123
void RegisterGroup(CLI::App &app) override
void RegisterGroup(CLI::App &app) override
ReduceBitDepthCommand(CLI::App &parent_app)
Definition command.hpp:167
void Run() override
Definition command.hpp:170
void Panic(const StringViewSourceLoc &s) noexcept
Definition panic.hpp:31
void AssertOrPanic(const bool condition, const StringViewSourceLoc &s)
Definition panic.hpp:36