Porytiles
Loading...
Searching...
No Matches
command_dump_tileset_config.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
13
14#include "command.hpp"
15#include "option.hpp"
17
18class DumpTilesetConfigCommand final : public Command {
19 public:
20 explicit DumpTilesetConfigCommand(CLI::App &parent_app)
21 : Command{parent_app, command_name, command_desc, command_group}
22 {
23 CLI::App &cmd = get_app();
24 cmd.add_option("<tileset-name>", tileset_name_, "Name of the tileset to dump config for")->required();
25 cmd.add_flag(
26 "--allow-missing-tileset",
27 allow_missing_tileset_,
28 "Dump config even when the tileset does not exist, to preview what a new tileset would inherit.");
29 project_root_opt_.RegisterOpt(cmd);
30 porytiles::register_config_options(cmd, cli_storage_);
31 }
32
33 private:
34 void Run() override
35 {
36 using namespace porytiles;
37
38 TilesetCommandEnv env{project_root_opt_.project_root(), cli_storage_};
39
40 // Env failures report through the unfiltered stderr diagnostics: the filtered diagnostics handle is not built
41 // until initialize() succeeds.
42 const auto env_result = env.initialize(tileset_name_);
43 if (!env_result.has_value()) {
44 const auto env_fail_result = ChainableResult<void>{
45 FormattableError{"Failed to dump config for tileset '{}'.", FormatParam{tileset_name_, Style::bold}},
46 env_result};
47 env.stderr_diag.fatal(env_fail_result);
48 throw CLI::RuntimeError{1};
49 }
50
51 // Verify the tileset exists before dumping: a misspelled name would otherwise dump a plausible-looking
52 // chain of project-level values. --allow-missing-tileset skips the check for previewing the config a
53 // not-yet-created tileset would inherit.
54 if (!allow_missing_tileset_) {
55 ProjectTilesetMetadataProvider metadata_provider{env.project_root, env.text_formatter, env.diag.get()};
56 if (!metadata_provider.exists(tileset_name_)) {
57 const auto not_found_err = ChainableResult<void>{FormattableError{
58 "Tileset '{}' does not exist. Pass '{}' to dump its config anyway.",
59 FormatParam{tileset_name_, Style::bold},
60 FormatParam{"--allow-missing-tileset", Style::bold}}};
61 env.diag->fatal(not_found_err);
62 throw CLI::RuntimeError{1};
63 }
64 }
65
66 env.config.dump_config(std::cout, ConfigScopeType::tileset, tileset_name_);
67 }
68
69 static constexpr auto command_name = "dump-tileset-config";
70 static constexpr auto command_desc = "Dump the full configuration provenance chain for a tileset.";
71 static constexpr auto command_group = "UTILITIES";
72 std::string tileset_name_;
73 bool allow_missing_tileset_{false};
74 OptProjectRoot project_root_opt_;
75 porytiles::CliOptionStorage cli_storage_;
76};
Command is an abstract class that provides basic command functionality for the Porytiles CLI driver.
Definition command.hpp:16
CLI::App & get_app() const
Definition command.hpp:42
DumpTilesetConfigCommand(CLI::App &parent_app)
std::filesystem::path project_root() const
Definition option.hpp:58
void RegisterOpt(CLI::App &app) override
Definition option.hpp:46
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:57
Provides a pokeemerald project filesystem-based implementation for TilesetMetadataProvider.
Bootstrap class so every tileset command can share common config and diagnostic setup.
void register_config_options(CLI::App &app, CliOptionStorage &storage)
Registers all config options with a CLI11 App.
Storage struct for CLI option values.