The CLI is a mode of anm2ed (anm2ed info|frame|render ...), not a separate binary
On Windows it attaches to the console it was started from, and gets UTF-8 arguments like the editor does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2dbbfa5e86
commit
abb9f965d6
5 files changed
+63
-38
No files matched your search
+20
-16
@@ -1,6 +1,6 @@
|
||||
// anm2ed-cli: look at .anm2 files without the editor - a text summary, or animations drawn (with the editor's own
|
||||
// OpenGL renderer, in a hidden window) to a PNG frame, a contact sheet, a PNG sequence, a GIF or a video.
|
||||
#include "cli.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <cstdio>
|
||||
#include <format>
|
||||
@@ -17,12 +17,11 @@
|
||||
#include "render.hpp"
|
||||
#include "working_directory.hpp"
|
||||
|
||||
using namespace anm2ed;
|
||||
using namespace anm2ed::util;
|
||||
|
||||
namespace anm2ed::cli
|
||||
{
|
||||
constexpr const char* USAGE = R"(usage: anm2ed-cli <command> <file.anm2> [options]
|
||||
constexpr const char* USAGE = R"(usage: anm2ed <command> <file.anm2> [options]
|
||||
|
||||
commands:
|
||||
info animations, items, spritesheets and Isaac compatibility, as text
|
||||
@@ -75,7 +74,7 @@ options:
|
||||
return arguments.command == "render" && arguments.get("type").value_or("") == "pngs";
|
||||
}
|
||||
|
||||
std::optional<Arguments> arguments_get(int argc, char** argv)
|
||||
std::optional<Arguments> arguments_get(int argc, const char** argv)
|
||||
{
|
||||
if (argc < 3) return {};
|
||||
Arguments arguments{.command = argv[1], .file = path::from_utf8(argv[2])};
|
||||
@@ -267,18 +266,23 @@ options:
|
||||
std::println("");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
auto arguments = cli::arguments_get(argc, argv);
|
||||
if (!arguments)
|
||||
constexpr std::string_view COMMANDS[] = {"info", "frame", "render"};
|
||||
|
||||
bool is_cli(int argc, const char** argv)
|
||||
{
|
||||
std::print(stderr, "{}", cli::USAGE);
|
||||
return 2;
|
||||
return argc >= 2 && std::ranges::contains(COMMANDS, std::string_view(argv[1])) && !path::is_exist(argv[1]);
|
||||
}
|
||||
|
||||
int run(int argc, const char** argv)
|
||||
{
|
||||
auto arguments = arguments_get(argc, argv);
|
||||
if (!arguments)
|
||||
{
|
||||
std::print(stderr, "{}", USAGE);
|
||||
return 2;
|
||||
}
|
||||
if (arguments->command == "info") return info_print(*arguments);
|
||||
return draw_run(*arguments);
|
||||
}
|
||||
if (arguments->command == "info") return cli::info_print(*arguments);
|
||||
if (arguments->command == "frame" || arguments->command == "render") return cli::draw_run(*arguments);
|
||||
std::print(stderr, "{}", cli::USAGE);
|
||||
return 2;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
// The command-line mode: "anm2ed <info|frame|render> <file.anm2> [options]" looks at a file without the editor, as
|
||||
// text or drawn (by the editor's OpenGL renderer, in a hidden window) to a PNG frame, a contact sheet, PNGs, a GIF or
|
||||
// a video.
|
||||
namespace anm2ed::cli
|
||||
{
|
||||
bool is_cli(int, const char**);
|
||||
int run(int, const char**);
|
||||
}
|
||||
+25
-10
@@ -1,3 +1,4 @@
|
||||
#include "cli.hpp"
|
||||
#include "loader.hpp"
|
||||
#include "log.hpp"
|
||||
#include "sdl.hpp"
|
||||
@@ -6,11 +7,11 @@
|
||||
#ifdef _WIN32
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <shellapi.h>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -63,20 +64,34 @@ namespace anm2ed
|
||||
{
|
||||
int application_run(int argc, const char** argv)
|
||||
{
|
||||
logger.session_open(util::sdl::preferences_directory_get());
|
||||
return log_exceptions([&]()
|
||||
if (cli::is_cli(argc, argv))
|
||||
{
|
||||
Loader loader(argc, argv);
|
||||
#ifdef _WIN32
|
||||
// A GUI-subsystem build has no console of its own; print to the one it was started from.
|
||||
if (AttachConsole(ATTACH_PARENT_PROCESS))
|
||||
{
|
||||
freopen("CONOUT$", "w", stdout);
|
||||
freopen("CONOUT$", "w", stderr);
|
||||
}
|
||||
#endif
|
||||
return cli::run(argc, argv);
|
||||
}
|
||||
|
||||
if (loader.isError) return EXIT_FAILURE;
|
||||
logger.session_open(util::sdl::preferences_directory_get());
|
||||
return log_exceptions(
|
||||
[&]()
|
||||
{
|
||||
Loader loader(argc, argv);
|
||||
|
||||
State state(loader.window, loader.settings, loader.arguments);
|
||||
if (loader.isError) return EXIT_FAILURE;
|
||||
|
||||
while (!state.isQuit)
|
||||
state.loop(loader.window, loader.settings);
|
||||
State state(loader.window, loader.settings, loader.arguments);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
});
|
||||
while (!state.isQuit)
|
||||
state.loop(loader.window, loader.settings);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user