mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 20:23:55 +00:00
* Add unicode support tests * Add unicode parse tests * Implement #14 * Slim down Windows.h * Fix documentation comments * Fix clang-tidy and cpplint * Update README * Fix clang-tidy * Fix to_path not being available on linux * Add roundtrip encoding tests * style: pre-commit.ci fixes * Fix pre-commit.ci * Fix codacy * Exclude parse_unicode which should not contain a newline from pre-commit * Remove a test which breaks CI * Fix build in CI * Replace broken execute_with tests * Fix wide string conversions on all systems * Fix system args on apple * style: pre-commit.ci fixes * Fix some includes * Fix wrong size calculation and comments * Add guards around codecvt * Fix _Pragma not recognized on MSVC * Fix bad macro check * Fix include * Fix narrow and widen when codecvt is missing * Fix some weird bug in old MSVC * Add dependent applications to meson-build * Fix precompilation * Fix lint * Fix coverage * Update README * style: pre-commit.ci fixes * Fix lint * Fix coverage * Fix optional braces offending clang * Remove copied comments from Windows.h * Suppress flawfinder detects * Fix cmake config tests failing because of a missing lib * chore: update copyright on new files to 2023 Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> * style: pre-commit.ci fixes Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
163 lines
5.1 KiB
C++
163 lines
5.1 KiB
C++
// Copyright (c) 2017-2023, University of Cincinnati, developed by Henry Schreiner
|
|
// under NSF AWARD 1414736 and by the respective contributors.
|
|
// All rights reserved.
|
|
//
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
#pragma once
|
|
|
|
// This include is only needed for IDEs to discover symbols
|
|
#include <CLI/Argv.hpp>
|
|
|
|
#include <CLI/Encoding.hpp>
|
|
|
|
// [CLI11:public_includes:set]
|
|
#include <algorithm>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
// [CLI11:public_includes:end]
|
|
|
|
#ifdef _WIN32
|
|
#include "SlimWindowsH.hpp"
|
|
#endif // _WIN32
|
|
|
|
// [CLI11:argv_inl_includes:verbatim]
|
|
#if defined(_WIN32)
|
|
#include <processenv.h>
|
|
#include <shellapi.h>
|
|
#elif defined(__APPLE__)
|
|
#include <crt_externs.h>
|
|
#endif
|
|
// [CLI11:argv_inl_includes:end]
|
|
|
|
namespace CLI {
|
|
// [CLI11:argv_inl_hpp:verbatim]
|
|
|
|
namespace detail {
|
|
|
|
#ifdef __APPLE__
|
|
// Copy argc and argv as early as possible to avoid modification
|
|
static const std::vector<const char *> static_args = [] {
|
|
static const std::vector<std::string> static_args_as_strings = [] {
|
|
std::vector<std::string> args_as_strings;
|
|
int argc = *_NSGetArgc();
|
|
char **argv = *_NSGetArgv();
|
|
|
|
args_as_strings.reserve(static_cast<size_t>(argc));
|
|
for(size_t i = 0; i < static_cast<size_t>(argc); i++) {
|
|
args_as_strings.push_back(argv[i]);
|
|
}
|
|
|
|
return args_as_strings;
|
|
}();
|
|
|
|
std::vector<const char *> static_args_result;
|
|
static_args_result.reserve(static_args_as_strings.size());
|
|
|
|
for(const auto &arg : static_args_as_strings) {
|
|
static_args_result.push_back(arg.data());
|
|
}
|
|
|
|
return static_args_result;
|
|
}();
|
|
#endif
|
|
|
|
/// Command-line arguments, as passed in to this executable, converted to utf-8 on Windows.
|
|
CLI11_INLINE const std::vector<const char *> &args() {
|
|
// This function uses initialization via lambdas extensively to take advantage of the thread safety of static
|
|
// variable initialization [stmt.dcl.3]
|
|
|
|
#ifdef _WIN32
|
|
static const std::vector<const char *> static_args = [] {
|
|
static const std::vector<std::string> static_args_as_strings = [] {
|
|
// On Windows, take arguments from GetCommandLineW and convert them to utf-8.
|
|
std::vector<std::string> args_as_strings;
|
|
int argc = 0;
|
|
|
|
auto deleter = [](wchar_t **ptr) { LocalFree(ptr); };
|
|
// NOLINTBEGIN(*-avoid-c-arrays)
|
|
auto wargv =
|
|
std::unique_ptr<wchar_t *[], decltype(deleter)>(CommandLineToArgvW(GetCommandLineW(), &argc), deleter);
|
|
// NOLINTEND(*-avoid-c-arrays)
|
|
|
|
if(wargv == nullptr) {
|
|
throw std::runtime_error("CommandLineToArgvW failed with code " + std::to_string(GetLastError()));
|
|
}
|
|
|
|
args_as_strings.reserve(static_cast<size_t>(argc));
|
|
for(size_t i = 0; i < static_cast<size_t>(argc); ++i) {
|
|
args_as_strings.push_back(narrow(wargv[i]));
|
|
}
|
|
|
|
return args_as_strings;
|
|
}();
|
|
|
|
std::vector<const char *> static_args_result;
|
|
static_args_result.reserve(static_args_as_strings.size());
|
|
|
|
for(const auto &arg : static_args_as_strings) {
|
|
static_args_result.push_back(arg.data());
|
|
}
|
|
|
|
return static_args_result;
|
|
}();
|
|
|
|
return static_args;
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
return static_args;
|
|
|
|
#else
|
|
static const std::vector<const char *> static_args = [] {
|
|
static const std::vector<char> static_cmdline = [] {
|
|
// On posix, retrieve arguments from /proc/self/cmdline, separated by null terminators.
|
|
std::vector<char> cmdline;
|
|
|
|
auto deleter = [](FILE *f) { std::fclose(f); };
|
|
std::unique_ptr<FILE, decltype(deleter)> fp_unique(std::fopen("/proc/self/cmdline", "r"), deleter);
|
|
FILE *fp = fp_unique.get();
|
|
if(!fp) {
|
|
throw std::runtime_error("could not open /proc/self/cmdline for reading"); // LCOV_EXCL_LINE
|
|
}
|
|
|
|
size_t size = 0;
|
|
while(std::feof(fp) == 0) {
|
|
cmdline.resize(size + 128);
|
|
size += std::fread(cmdline.data() + size, 1, 128, fp);
|
|
|
|
if(std::ferror(fp) != 0) {
|
|
throw std::runtime_error("error during reading /proc/self/cmdline"); // LCOV_EXCL_LINE
|
|
}
|
|
}
|
|
cmdline.resize(size);
|
|
|
|
return cmdline;
|
|
}();
|
|
|
|
std::size_t argc = static_cast<std::size_t>(std::count(static_cmdline.begin(), static_cmdline.end(), '\0'));
|
|
std::vector<const char *> static_args_result;
|
|
static_args_result.reserve(argc);
|
|
|
|
for(auto it = static_cmdline.begin(); it != static_cmdline.end();
|
|
it = std::find(it, static_cmdline.end(), '\0') + 1) {
|
|
static_args_result.push_back(static_cmdline.data() + (it - static_cmdline.begin()));
|
|
}
|
|
|
|
return static_args_result;
|
|
}();
|
|
|
|
return static_args;
|
|
#endif
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
CLI11_INLINE const char *const *argv() { return detail::args().data(); }
|
|
CLI11_INLINE int argc() { return static_cast<int>(detail::args().size()); }
|
|
|
|
// [CLI11:argv_inl_hpp:end]
|
|
} // namespace CLI
|