-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathArgumentValidation.h
More file actions
87 lines (64 loc) · 3.01 KB
/
Copy pathArgumentValidation.h
File metadata and controls
87 lines (64 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
ArgumentValidation.h
Abstract:
Declaration of argument validation functions.
--*/
#pragma once
#include "Exceptions.h"
#include "ContainerModel.h"
#include "InspectModel.h"
#include <string>
#include <tuple>
#include <vector>
#include <charconv>
#include <wslc.h>
#include <string.hpp>
#include "Localization.h"
using namespace wsl::windows::wslc::models;
namespace wsl::windows::wslc::validation {
template <typename T>
void ValidateIntegerFromString(
const std::vector<std::wstring>& values, const std::wstring& argName, const std::function<bool(T)>& validate = [](T) {
return true;
})
{
for (const auto& value : values)
{
std::ignore = GetIntegerFromString<T>(value, argName, validate);
}
}
template <typename T>
T GetIntegerFromString(
const std::wstring& value, const std::wstring& argName = {}, const std::function<bool(T)>& validate = [](T) { return true; })
{
std::string narrowValue = wsl::windows::common::string::WideToMultiByte(value);
T convertedValue{};
const char* begin = narrowValue.c_str();
const char* end = begin + narrowValue.size();
auto result = std::from_chars(begin, end, convertedValue);
// Reject conversion errors and partial parses (e.g. "1.5", "9abc")
if (result.ec != std::errc() || result.ptr != end || !validate(convertedValue))
{
throw ArgumentException(wsl::shared::Localization::WSLCCLI_InvalidIntegerArgumentError(argName, value));
}
return convertedValue;
}
void ValidateWSLCSignalFromString(const std::vector<std::wstring>& values, const std::wstring& argName);
WSLCSignal GetWSLCSignalFromString(const std::wstring& input, const std::wstring& argName = {});
void ValidateMemorySize(const std::vector<std::wstring>& values, const std::wstring& argName);
int64_t GetMemorySizeFromString(const std::wstring& input, const std::wstring& argName = {});
void ValidateNanoCpus(const std::vector<std::wstring>& values, const std::wstring& argName);
int64_t GetNanoCpusFromString(const std::wstring& input, const std::wstring& argName = {});
void ValidateUlimit(const std::vector<std::wstring>& values, const std::wstring& argName);
std::tuple<std::string, int64_t, int64_t> ParseUlimit(const std::wstring& input, const std::wstring& argName = {});
void ValidateFormatTypeFromString(const std::vector<std::wstring>& values, const std::wstring& argName);
FormatType GetFormatTypeFromString(const std::wstring& input, const std::wstring& argName = {});
InspectType GetInspectTypeFromString(const std::wstring& input, const std::wstring& argName);
void ValidateGpus(const std::vector<std::wstring>& values, const std::wstring& argName);
void ValidateVolumeMount(const std::vector<std::wstring>& values);
void ValidateFilter(const std::vector<std::wstring>& values);
std::pair<std::string, std::string> ParseLabel(const std::wstring& value);
std::pair<std::string, std::string> ParseDriverOption(const std::wstring& value);
} // namespace wsl::windows::wslc::validation