CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
Argv_inl.hpp
1// Copyright (c) 2017-2024, 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 <algorithm>
18#include <memory>
19#include <stdexcept>
20#include <string>
21#include <vector>
22// [CLI11:public_includes:end]
23
24// [CLI11:argv_inl_includes:verbatim]
25#if defined(_WIN32)
26#if !(defined(_AMD64_) || defined(_X86_) || defined(_ARM_))
27#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || \
28 defined(_M_AMD64)
29#define _AMD64_
30#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(__i386__) || defined(_M_IX86)
31#define _X86_
32#elif defined(__arm__) || defined(_M_ARM) || defined(_M_ARMT)
33#define _ARM_
34#elif defined(__aarch64__) || defined(_M_ARM64)
35#define _ARM64_
36#elif defined(_M_ARM64EC)
37#define _ARM64EC_
38#endif
39#endif
40
41// first
42#ifndef NOMINMAX
43// if NOMINMAX is already defined we don't want to mess with that either way
44#define NOMINMAX
45#include <windef.h>
46#undef NOMINMAX
47#else
48#include <windef.h>
49#endif
50
51// second
52#include <winbase.h>
53// third
54#include <processthreadsapi.h>
55#include <shellapi.h>
56#endif
57// [CLI11:argv_inl_includes:end]
58
59namespace CLI {
60// [CLI11:argv_inl_hpp:verbatim]
61
62namespace detail {
63
64#ifdef _WIN32
65CLI11_INLINE std::vector<std::string> compute_win32_argv() {
66 std::vector<std::string> result;
67 int argc = 0;
68
69 auto deleter = [](wchar_t **ptr) { LocalFree(ptr); };
70 // NOLINTBEGIN(*-avoid-c-arrays)
71 auto wargv = std::unique_ptr<wchar_t *[], decltype(deleter)>(CommandLineToArgvW(GetCommandLineW(), &argc), deleter);
72 // NOLINTEND(*-avoid-c-arrays)
73
74 if(wargv == nullptr) {
75 throw std::runtime_error("CommandLineToArgvW failed with code " + std::to_string(GetLastError()));
76 }
77
78 result.reserve(static_cast<size_t>(argc));
79 for(size_t i = 0; i < static_cast<size_t>(argc); ++i) {
80 result.push_back(narrow(wargv[i]));
81 }
82
83 return result;
84}
85#endif
86
87} // namespace detail
88
89// [CLI11:argv_inl_hpp:end]
90} // namespace CLI