mirror of
https://github.com/sametersoylu/argument-parser.git
synced 2026-04-13 03:41:18 +00:00
feat: Introduce core argument parsing framework with platform-specific parsers, conventions, and type-based value parsing.
This commit is contained in:
@@ -10,15 +10,10 @@ namespace argument_parser::conventions::implementations {
|
|||||||
public:
|
public:
|
||||||
parsed_argument get_argument(std::string const &raw) const override;
|
parsed_argument get_argument(std::string const &raw) const override;
|
||||||
std::string extract_value(std::string const & /*raw*/) const override;
|
std::string extract_value(std::string const & /*raw*/) const override;
|
||||||
|
|
||||||
bool requires_next_token() const override;
|
bool requires_next_token() const override;
|
||||||
|
|
||||||
std::string name() const override;
|
std::string name() const override;
|
||||||
|
|
||||||
std::string short_prec() const override;
|
std::string short_prec() const override;
|
||||||
|
|
||||||
std::string long_prec() const override;
|
std::string long_prec() const override;
|
||||||
|
|
||||||
static gnu_argument_convention instance;
|
static gnu_argument_convention instance;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "base_convention.hpp"
|
#include "base_convention.hpp"
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#ifndef WINDOWS_ARGUMENT_CONVENTION_HPP
|
#ifndef WINDOWS_ARGUMENT_CONVENTION_HPP
|
||||||
#define WINDOWS_ARGUMENT_CONVENTION_HPP
|
#define WINDOWS_ARGUMENT_CONVENTION_HPP
|
||||||
@@ -13,17 +12,11 @@ namespace argument_parser::conventions::implementations {
|
|||||||
class windows_argument_convention : public base_convention {
|
class windows_argument_convention : public base_convention {
|
||||||
public:
|
public:
|
||||||
explicit windows_argument_convention(bool accept_dash = true);
|
explicit windows_argument_convention(bool accept_dash = true);
|
||||||
|
|
||||||
parsed_argument get_argument(std::string const &raw) const override;
|
parsed_argument get_argument(std::string const &raw) const override;
|
||||||
|
|
||||||
std::string extract_value(std::string const & /*raw*/) const override;
|
std::string extract_value(std::string const & /*raw*/) const override;
|
||||||
|
|
||||||
bool requires_next_token() const override;
|
bool requires_next_token() const override;
|
||||||
|
|
||||||
std::string name() const override;
|
std::string name() const override;
|
||||||
|
|
||||||
std::string short_prec() const override;
|
std::string short_prec() const override;
|
||||||
|
|
||||||
std::string long_prec() const override;
|
std::string long_prec() const override;
|
||||||
static windows_argument_convention instance;
|
static windows_argument_convention instance;
|
||||||
|
|
||||||
|
|||||||
@@ -3,51 +3,32 @@
|
|||||||
#define PARSING_TRAITS_HPP
|
#define PARSING_TRAITS_HPP
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
namespace argument_parser::parsing_traits {
|
namespace argument_parser::parsing_traits {
|
||||||
template <typename T_>
|
template <typename T_> struct parser_trait {
|
||||||
struct parser_trait {
|
|
||||||
using type = T_;
|
using type = T_;
|
||||||
static T_ parse(const std::string &input);
|
static T_ parse(const std::string &input);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <> struct parser_trait<std::string> {
|
||||||
struct parser_trait<std::string> {
|
static std::string parse(const std::string &input);
|
||||||
static std::string parse(const std::string& input) {
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template <> struct parser_trait<bool> {
|
||||||
struct parser_trait<bool> {
|
static bool parse(const std::string &input);
|
||||||
static bool parse(const std::string& input) {
|
|
||||||
if (input == "t" || input == "true" || input == "1") return true;
|
|
||||||
if (input == "f" || input == "false" || input == "0") return false;
|
|
||||||
throw std::runtime_error("Invalid boolean value: " + input);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <> struct parser_trait<int> {
|
||||||
struct parser_trait<int> {
|
static int parse(const std::string &input);
|
||||||
static int parse(const std::string& input) {
|
|
||||||
return std::stoi(input);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <> struct parser_trait<float> {
|
||||||
struct parser_trait<float> {
|
static float parse(const std::string &input);
|
||||||
static float parse(const std::string& input) {
|
|
||||||
return std::stof(input);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <> struct parser_trait<double> {
|
||||||
struct parser_trait<double> {
|
static double parse(const std::string &input);
|
||||||
static double parse(const std::string& input) {
|
|
||||||
return std::stod(input);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
} // namespace argument_parser::parsing_traits
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -3,23 +3,15 @@
|
|||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#ifndef LINUX_PARSER_HPP
|
#ifndef LINUX_PARSER_HPP
|
||||||
#define LINUX_PARSER_HPP
|
#define LINUX_PARSER_HPP
|
||||||
#include <fstream>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <argument_parser.hpp>
|
#include <argument_parser.hpp>
|
||||||
|
|
||||||
namespace argument_parser {
|
namespace argument_parser {
|
||||||
class linux_parser : public base_parser {
|
class linux_parser : public base_parser {
|
||||||
public:
|
public:
|
||||||
linux_parser() {
|
linux_parser();
|
||||||
std::ifstream command_line_file{"/proc/self/cmdline"};
|
|
||||||
std::getline(command_line_file, program_name, '\0');
|
|
||||||
for(std::string line; std::getline(command_line_file, line, '\0');) {
|
|
||||||
parsed_arguments.emplace_back(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
} // namespace argument_parser
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
@@ -1,61 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <argument_parser.hpp>
|
#include <argument_parser.hpp>
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <windows.h>
|
|
||||||
#include <shellapi.h>
|
|
||||||
|
|
||||||
namespace argument_parser {
|
namespace argument_parser {
|
||||||
|
|
||||||
class windows_parser : public base_parser {
|
class windows_parser : public base_parser {
|
||||||
public:
|
public:
|
||||||
windows_parser() {
|
windows_parser();
|
||||||
int nArgs = 0;
|
|
||||||
LPWSTR* raw = ::CommandLineToArgvW(::GetCommandLineW(), &nArgs);
|
|
||||||
if (!raw) {
|
|
||||||
throw std::runtime_error("CommandLineToArgvW failed (" +
|
|
||||||
std::to_string(::GetLastError()) + ")");
|
|
||||||
}
|
|
||||||
std::unique_ptr<LPWSTR, void(*)(LPWSTR*)> argvW{ raw, [](LPWSTR* p){ if (p) ::LocalFree(p); } };
|
|
||||||
|
|
||||||
if (nArgs <= 0) {
|
|
||||||
throw std::runtime_error("No command line arguments found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
std::wstring w0 = argvW.get()[0];
|
|
||||||
auto pos = w0.find_last_of(L"\\/");
|
|
||||||
std::wstring base = (pos == std::wstring::npos) ? w0 : w0.substr(pos + 1);
|
|
||||||
program_name = utf8_from_wstring(base);
|
|
||||||
}
|
|
||||||
|
|
||||||
parsed_arguments.reserve(static_cast<size_t>(nArgs > 0 ? nArgs - 1 : 0));
|
|
||||||
for (int i = 1; i < nArgs; ++i) {
|
|
||||||
parsed_arguments.emplace_back(utf8_from_wstring(argvW.get()[i]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static std::string utf8_from_wstring(const std::wstring& w) {
|
|
||||||
if (w.empty()) return {};
|
|
||||||
int needed = ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
|
|
||||||
w.c_str(), -1, nullptr, 0, nullptr, nullptr);
|
|
||||||
if (needed <= 0) {
|
|
||||||
throw std::runtime_error("WideCharToMultiByte sizing failed (" +
|
|
||||||
std::to_string(::GetLastError()) + ")");
|
|
||||||
}
|
|
||||||
std::string out;
|
|
||||||
out.resize(static_cast<size_t>(needed - 1));
|
|
||||||
int written = ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
|
|
||||||
w.c_str(), -1, out.data(), needed - 1, nullptr, nullptr);
|
|
||||||
if (written <= 0) {
|
|
||||||
throw std::runtime_error("WideCharToMultiByte convert failed (" +
|
|
||||||
std::to_string(::GetLastError()) + ")");
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
} // namespace argument_parser
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
87
src/main.cpp
87
src/main.cpp
@@ -2,21 +2,19 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#define ALLOW_DASH_FOR_WINDOWS 0
|
#define ALLOW_DASH_FOR_WINDOWS 0
|
||||||
|
|
||||||
#include <parser_v2.hpp>
|
|
||||||
#include <argparse>
|
#include <argparse>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <parser_v2.hpp>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
int x, y;
|
int x, y;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template <> struct argument_parser::parsing_traits::parser_trait<Point> {
|
||||||
struct argument_parser::parsing_traits::parser_trait<Point> {
|
|
||||||
static Point parse(const std::string &input) {
|
static Point parse(const std::string &input) {
|
||||||
auto comma_pos = input.find(',');
|
auto comma_pos = input.find(',');
|
||||||
if (comma_pos == std::string::npos) {
|
if (comma_pos == std::string::npos) {
|
||||||
@@ -28,15 +26,13 @@ struct argument_parser::parsing_traits::parser_trait<Point> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template <> struct argument_parser::parsing_traits::parser_trait<std::regex> {
|
||||||
struct argument_parser::parsing_traits::parser_trait<std::regex> {
|
|
||||||
static std::regex parse(const std::string &input) {
|
static std::regex parse(const std::string &input) {
|
||||||
return std::regex(input);
|
return std::regex(input);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template <> struct argument_parser::parsing_traits::parser_trait<std::vector<int>> {
|
||||||
struct argument_parser::parsing_traits::parser_trait<std::vector<int>> {
|
|
||||||
static std::vector<int> parse(const std::string &input) {
|
static std::vector<int> parse(const std::string &input) {
|
||||||
std::vector<int> result;
|
std::vector<int> result;
|
||||||
std::stringstream ss(input);
|
std::stringstream ss(input);
|
||||||
@@ -48,44 +44,17 @@ struct argument_parser::parsing_traits::parser_trait<std::vector<int>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct argument_parser::parsing_traits::parser_trait<std::vector<std::string>> {
|
|
||||||
static std::vector<std::string> parse(const std::string& input) {
|
|
||||||
std::vector<std::string> result;
|
|
||||||
auto copyInput = input;
|
|
||||||
if (input.starts_with("[")) {
|
|
||||||
copyInput = input.substr(1, input.size());
|
|
||||||
if (copyInput.ends_with("]")) {
|
|
||||||
copyInput = copyInput.substr(0, copyInput.size() - 1);
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error("Invalid vector<string> format. Expected closing ']'.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::stringstream ss(copyInput);
|
|
||||||
std::string item;
|
|
||||||
while (std::getline(ss, item, ',')) {
|
|
||||||
result.push_back(item);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::initializer_list<argument_parser::conventions::convention const *const> conventions = {
|
const std::initializer_list<argument_parser::conventions::convention const *const> conventions = {
|
||||||
&argument_parser::conventions::gnu_argument_convention,
|
&argument_parser::conventions::gnu_argument_convention,
|
||||||
&argument_parser::conventions::gnu_equal_argument_convention,
|
&argument_parser::conventions::gnu_equal_argument_convention,
|
||||||
&argument_parser::conventions::windows_argument_convention,
|
&argument_parser::conventions::windows_argument_convention,
|
||||||
&argument_parser::conventions::windows_equal_argument_convention
|
&argument_parser::conventions::windows_equal_argument_convention};
|
||||||
};
|
|
||||||
|
|
||||||
const auto echo = argument_parser::helpers::make_parametered_action<std::string>([](std::string const& text) {
|
const auto echo = argument_parser::helpers::make_parametered_action<std::string>(
|
||||||
std::cout << text << std::endl;
|
[](std::string const &text) { std::cout << text << std::endl; });
|
||||||
});
|
|
||||||
|
|
||||||
|
const auto echo_point = argument_parser::helpers::make_parametered_action<Point>(
|
||||||
const auto echo_point = argument_parser::helpers::make_parametered_action<Point>([](Point const& point) {
|
[](Point const &point) { std::cout << "Point(" << point.x << ", " << point.y << ")" << std::endl; });
|
||||||
std::cout << "Point(" << point.x << ", " << point.y << ")" << std::endl;
|
|
||||||
});
|
|
||||||
|
|
||||||
const auto cat = argument_parser::helpers::make_parametered_action<std::string>([](std::string const &file_name) {
|
const auto cat = argument_parser::helpers::make_parametered_action<std::string>([](std::string const &file_name) {
|
||||||
std::ifstream file(file_name);
|
std::ifstream file(file_name);
|
||||||
@@ -138,7 +107,6 @@ void run_grep(argument_parser::base_parser const& parser) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int v1Examples() {
|
int v1Examples() {
|
||||||
|
|
||||||
auto parser = argument_parser::parser{};
|
auto parser = argument_parser::parser{};
|
||||||
@@ -148,9 +116,9 @@ int v1Examples() {
|
|||||||
parser.add_argument<std::string>("f", "file", "File to grep, required only if using grep", false);
|
parser.add_argument<std::string>("f", "file", "File to grep, required only if using grep", false);
|
||||||
parser.add_argument<std::regex>("g", "grep", "Grep pattern, required only if using grep", false);
|
parser.add_argument<std::regex>("g", "grep", "Grep pattern, required only if using grep", false);
|
||||||
parser.add_argument("c", "cat", "Prints the content of the file", cat, false);
|
parser.add_argument("c", "cat", "Prints the content of the file", cat, false);
|
||||||
parser.add_argument("h", "help", "Displays this help text.", argument_parser::helpers::make_non_parametered_action([&parser]{
|
parser.add_argument(
|
||||||
parser.display_help(conventions);
|
"h", "help", "Displays this help text.",
|
||||||
}), false);
|
argument_parser::helpers::make_non_parametered_action([&parser] { parser.display_help(conventions); }), false);
|
||||||
|
|
||||||
parser.add_argument<Point>("p", "point", "Test point", false);
|
parser.add_argument<Point>("p", "point", "Test point", false);
|
||||||
|
|
||||||
@@ -165,7 +133,6 @@ int v1Examples() {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
auto test = parser.get_optional<std::vector<int>>("test");
|
auto test = parser.get_optional<std::vector<int>>("test");
|
||||||
if (test) {
|
if (test) {
|
||||||
for (auto const &item : test.value()) {
|
for (auto const &item : test.value()) {
|
||||||
@@ -184,37 +151,27 @@ int v1Examples() {
|
|||||||
|
|
||||||
int v2Examples() {
|
int v2Examples() {
|
||||||
using namespace argument_parser::v2::flags;
|
using namespace argument_parser::v2::flags;
|
||||||
argument_parser::v2::macos_parser parser;
|
argument_parser::v2::base_parser parser;
|
||||||
|
|
||||||
|
parser.add_argument<std::string>({{ShortArgument, "e"}, {LongArgument, "echo"}, {Action, echo}});
|
||||||
|
|
||||||
|
parser.add_argument<Point>({{ShortArgument, "ep"}, {LongArgument, "echo-point"}, {Action, echo_point}});
|
||||||
|
|
||||||
parser.add_argument<std::string>({
|
parser.add_argument<std::string>({
|
||||||
{ ShortArgument, "e" },
|
// stores string for f/file flag
|
||||||
{ LongArgument, "echo" },
|
|
||||||
{ Action, echo }
|
|
||||||
});
|
|
||||||
|
|
||||||
parser.add_argument<Point>({
|
|
||||||
{ ShortArgument, "ep" },
|
|
||||||
{ LongArgument, "echo-point" },
|
|
||||||
{ Action, echo_point }
|
|
||||||
});
|
|
||||||
|
|
||||||
parser.add_argument<std::string>({ // stores string for f/file flag
|
|
||||||
{ShortArgument, "f"},
|
{ShortArgument, "f"},
|
||||||
{LongArgument, "file"},
|
{LongArgument, "file"},
|
||||||
// if no action, falls to store operation with given type.
|
// if no action, falls to store operation with given type.
|
||||||
});
|
});
|
||||||
|
|
||||||
parser.add_argument<std::regex>({ // stores string for g/grep flag
|
parser.add_argument<std::regex>({
|
||||||
|
// stores string for g/grep flag
|
||||||
{ShortArgument, "g"},
|
{ShortArgument, "g"},
|
||||||
{LongArgument, "grep"},
|
{LongArgument, "grep"},
|
||||||
// same as 'file' flag
|
// same as 'file' flag
|
||||||
});
|
});
|
||||||
|
|
||||||
parser.add_argument<std::string>({
|
parser.add_argument<std::string>({{ShortArgument, "c"}, {LongArgument, "cat"}, {Action, cat}});
|
||||||
{ ShortArgument, "c" },
|
|
||||||
{ LongArgument, "cat" },
|
|
||||||
{ Action, cat }
|
|
||||||
});
|
|
||||||
|
|
||||||
parser.add_argument<Point>({
|
parser.add_argument<Point>({
|
||||||
// { ShortArgument, "sp" }, // now if ShortArgument or LongArgument is missing, it will use it for the other.
|
// { ShortArgument, "sp" }, // now if ShortArgument or LongArgument is missing, it will use it for the other.
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "windows_argument_convention.hpp"
|
#include "windows_argument_convention.hpp"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace argument_parser::conventions::implementations {
|
namespace argument_parser::conventions::implementations {
|
||||||
windows_argument_convention::windows_argument_convention(bool accept_dash) : accept_dash_(accept_dash) {}
|
windows_argument_convention::windows_argument_convention(bool accept_dash) : accept_dash_(accept_dash) {}
|
||||||
|
|||||||
28
src/source/parser/parsing_traits/traits.cpp
Normal file
28
src/source/parser/parsing_traits/traits.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include "traits.hpp"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace argument_parser::parsing_traits {
|
||||||
|
std::string parser_trait<std::string>::parse(const std::string &input) {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool parser_trait<bool>::parse(const std::string &input) {
|
||||||
|
if (input == "t" || input == "true" || input == "1")
|
||||||
|
return true;
|
||||||
|
if (input == "f" || input == "false" || input == "0")
|
||||||
|
return false;
|
||||||
|
throw std::runtime_error("Invalid boolean value: " + input);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parser_trait<int>::parse(const std::string &input) {
|
||||||
|
return std::stoi(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
float parser_trait<float>::parse(const std::string &input) {
|
||||||
|
return std::stof(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
double parser_trait<double>::parse(const std::string &input) {
|
||||||
|
return std::stod(input);
|
||||||
|
}
|
||||||
|
} // namespace argument_parser::parsing_traits
|
||||||
18
src/source/parser/platform_parsers/linux_parser.cpp
Normal file
18
src/source/parser/platform_parsers/linux_parser.cpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifdef __linux__
|
||||||
|
|
||||||
|
#include "linux_parser.hpp"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace argument_parser {
|
||||||
|
linux_parser::linux_parser() {
|
||||||
|
std::ifstream command_line_file{"/proc/self/cmdline"};
|
||||||
|
std::getline(command_line_file, program_name, '\0');
|
||||||
|
for (std::string line; std::getline(command_line_file, line, '\0');) {
|
||||||
|
parsed_arguments.emplace_back(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace argument_parser
|
||||||
|
|
||||||
|
#endif
|
||||||
42
src/source/parser/platform_parsers/windows_parser.cpp
Normal file
42
src/source/parser/platform_parsers/windows_parser.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#ifdef _WIN32
|
||||||
|
|
||||||
|
#include "windows_parser.hpp"
|
||||||
|
|
||||||
|
#include <Windows.h>
|
||||||
|
#include <shellapi.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string utf8_from_wstring(const std::wstring &w) {
|
||||||
|
if (w.empty())
|
||||||
|
return {};
|
||||||
|
int needed = ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, w.c_str(), -1, nullptr, 0, nullptr, nullptr);
|
||||||
|
if (needed <= 0) {
|
||||||
|
throw std::runtime_error("WideCharToMultiByte sizing failed (" + std::to_string(::GetLastError()) + ")");
|
||||||
|
}
|
||||||
|
std::string out;
|
||||||
|
out.resize(static_cast<size_t>(needed - 1));
|
||||||
|
int written =
|
||||||
|
::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, w.c_str(), -1, out.data(), needed - 1, nullptr, nullptr);
|
||||||
|
if (written <= 0) {
|
||||||
|
throw std::runtime_error("WideCharToMultiByte convert failed (" + std::to_string(::GetLastError()) + ")");
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace argument_parser {
|
||||||
|
windows_parser::windows_parser() {
|
||||||
|
LPWSTR *argv_w;
|
||||||
|
int argc_w;
|
||||||
|
argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
|
||||||
|
if (argv_w == nullptr) {
|
||||||
|
throw std::runtime_error("CommandLineToArgvW failed");
|
||||||
|
}
|
||||||
|
for (int i = 0; i < argc_w; i++) {
|
||||||
|
std::string arg = utf8_from_wstring(argv_w[i]);
|
||||||
|
parsed_arguments.emplace_back(arg);
|
||||||
|
}
|
||||||
|
LocalFree(argv_w);
|
||||||
|
}
|
||||||
|
} // namespace argument_parser
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user