CLI11 2.7.2
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
Argv_inl.hpp
1// Copyright (c) 2017-2026, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// IWYU pragma: private, include "CLI/CLI.hpp"
10
11// This include is only needed for IDEs to discover symbols
12#include "../Argv.hpp"
13
14#include "../Encoding.hpp"
15
16// [CLI11:public_includes:set]
17#include <memory>
18#include <stdexcept>
19#include <string>
20#include <vector>
21// [CLI11:public_includes:end]
22
23// [CLI11:argv_inl_includes:verbatim]
24#if defined(_WIN32)
25#if !(defined(_AMD64_) || defined(_X86_) || defined(_ARM_))
26#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || \
27 defined(_M_AMD64)
28#define _AMD64_
29#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(__i386__) || defined(_M_IX86)
30#define _X86_
31#elif defined(__arm__) || defined(_M_ARM) || defined(_M_ARMT)
32#define _ARM_
33#elif defined(__aarch64__) || defined(_M_ARM64)
34#define _ARM64_
35#elif defined(_M_ARM64EC)
36#define _ARM64EC_
37#endif
38#endif
39
40// first
41#ifndef NOMINMAX
42// if NOMINMAX is already defined we don't want to mess with that either way
43#define NOMINMAX
44#include <windef.h>
45#undef NOMINMAX
46#else
47#include <windef.h>
48#endif
49
50// second
51#include <winbase.h>
52// third
53#include <processthreadsapi.h>
54#include <shellapi.h>
55// CommandLineToArgvW lives in shell32. CMake/Bazel already link it when using the
56// CLI11 targets; this pragma covers MSVC/clang-cl consumers that only include the
57// headers (or clear CMAKE_CXX_STANDARD_LIBRARIES). See #1053 and
58// https://learn.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp#lib
59#if defined(_MSC_VER)
60#pragma comment(lib, "shell32.lib")
61#endif
62#endif
63// [CLI11:argv_inl_includes:end]
64
65namespace CLI {
66// [CLI11:argv_inl_hpp:verbatim]
67
68namespace detail {
69
70#ifdef _WIN32
71CLI11_INLINE std::vector<std::string> compute_win32_argv() {
72 std::vector<std::string> result;
73 int argc = 0;
74
75 auto deleter = [](wchar_t **ptr) { LocalFree(ptr); };
76 // NOLINTBEGIN(*-avoid-c-arrays)
77 auto wargv = std::unique_ptr<wchar_t *[], decltype(deleter)>(CommandLineToArgvW(GetCommandLineW(), &argc), deleter);
78 // NOLINTEND(*-avoid-c-arrays)
79
80 if(wargv == nullptr) {
81 throw std::runtime_error("CommandLineToArgvW failed with code " + std::to_string(GetLastError()));
82 }
83
84 result.reserve(static_cast<size_t>(argc));
85 for(size_t i = 0; i < static_cast<size_t>(argc); ++i) {
86 result.push_back(narrow(wargv[i]));
87 }
88
89 return result;
90}
91#endif
92
93} // namespace detail
94
95// [CLI11:argv_inl_hpp:end]
96} // namespace CLI