Program Listing for File CommandLine.ixx

Program Listing for File CommandLine.ixx#

↰ Return to documentation for file (Src/GraphicsEngineVulkan/app/CommandLine.ixx)

module;

#include <cstddef>
#include <filesystem>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>

export module kataglyphis.vulkan.command_line;

import kataglyphis.vulkan.model_file_kind;
import kataglyphis.vulkan.scene_config;

export namespace Kataglyphis {

struct StartupModelArgument
{
    std::string model;
    std::string note;
    bool tooMany = false;
};

inline constexpr std::string_view kUsage = "Usage: BeschleunigerBallett [model] [flags]";

inline bool helpListsFlagsOf(std::string_view file)
{
    const std::size_t separator = file.find_last_of("/\\");
    return file.substr(separator == std::string_view::npos ? 0U : separator + 1U) == "Main.cpp";
}

inline StartupModelArgument startupModelArgument(std::span<char *const> positional)
{
    StartupModelArgument argument;
    if (positional.empty()) { return argument; }
    if (positional.size() > 1U) {
        argument.tooMany = true;
        argument.note = "expected at most one model path, got " + std::to_string(positional.size());
        return argument;
    }

    // The argument as given, already in the code page a narrow path takes.
    const std::string given(positional.front());
    const std::filesystem::path path(given);
    std::error_code error;
    if (!std::filesystem::exists(path, error) || error) {
        argument.note = given + " does not exist";
    } else if (!std::filesystem::is_regular_file(path, error) || error) {
        argument.note = given + " is not a file";
    } else if (!isSupportedModelPath(given)) {
        argument.note = given + " is not a .gltf, .glb or .obj file";
    } else if (std::optional<std::string> model = sceneConfig::libraryPathFor(path); model.has_value()) {
        argument.model = std::move(*model);
    } else {
        argument.note = given + " has a full path the system code page cannot name";
    }
    return argument;
}

}// namespace Kataglyphis