Skip to content

Commit

Permalink
Add CliUtf8fy
Browse files Browse the repository at this point in the history
  • Loading branch information
captainurist committed Oct 17, 2023
1 parent fdfa69d commit b2a6a41
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Bin/LodTool/LodTool.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "LodToolOptions.h"

#include "Library/Cli/CliUtf8fy.h"
#include "Library/Lod/LodReader.h"
#include "Library/LodFormats/LodFormats.h"
#include "Library/Serialization/Serialization.h"
Expand Down Expand Up @@ -41,6 +42,7 @@ int runDump(const LodToolOptions &options) {

int main(int argc, char **argv) {
try {
CliUtf8fy _(argc, argv);
LodToolOptions options = LodToolOptions::parse(argc, argv);
if (options.helpPrinted)
return 1;
Expand Down
3 changes: 2 additions & 1 deletion src/Library/Cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ set(LIBRARY_CLI_SOURCES
CliApp.cpp)

set(LIBRARY_CLI_HEADERS
CliApp.h)
CliApp.h
CliUtf8fy.h)

add_library(library_cli ${LIBRARY_CLI_SOURCES} ${LIBRARY_CLI_HEADERS})
target_link_libraries(library_cli CLI11::CLI11)
Expand Down
35 changes: 35 additions & 0 deletions src/Library/Cli/CliUtf8fy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <memory>

#include <CLI/CLI.hpp>

/**
* Utility class that converts program arguments to utf-8 on Windows, and does nothing on POSIX.
*
* Use it like this:
* ```
* int main(int argc, char **argv) {
* try {
* CliUtf8fy _(argc, argv);
* // Use argc & argv here.
* } catch (...) {
* // Your error processing here.
* }
* }
* ```
*
* Note that while `CliUtf8fy` constructor shouldn't normally throw, it can theoretically throw.
*
* Also note that `CliUtf8fy` is only needed if the program isn't using `platformMain`.
*/
class CliUtf8fy {
public:
CliUtf8fy(int /*argc*/, char **&argv) {
_app = std::make_unique<CLI::App>();
argv = _app->ensure_utf8(argv);
}

private:
std::unique_ptr<CLI::App> _app;
};

0 comments on commit b2a6a41

Please sign in to comment.