master
cpp 69 lines 1.88 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 Argument.cpp
8
9 Abstract:
10
11 Implementation of the Argument class.
12
13 --*/
14 #include "Argument.h"
15 #include "Command.h"
16 #include "Exceptions.h"
17 #include "ArgumentDefinitions.h"
18
19 #include <algorithm>
20 #include <iterator>
21 #include <sstream>
22 #include <string>
23
24 using namespace wsl::shared;
25 using namespace wsl::windows::wslc::argument;
26 using namespace std::literals;
27
28 namespace wsl::windows::wslc {
29 using namespace wsl::windows::wslc::execution;
30
31 Argument Argument::Create(ArgType type, ArgumentOverrides overrides)
32 {
33 WI_ASSERT(!overrides.Name.has_value() || overrides.Name->size() >= 2);
34
35 switch (type)
36 {
37 #define WSLC_ARG_CREATE_CASE(EnumName, DefaultName, DefaultAlias, ArgumentKind, ConvertedType, DefaultDesc) \
38 case ArgType::EnumName: \
39 return Argument{ \
40 type, \
41 overrides.Name.has_value() ? std::move(overrides.Name.value()) : std::wstring{L##DefaultName}, \
42 overrides.Alias.has_value() ? std::move(overrides.Alias.value()) : std::wstring{DefaultAlias}, \
43 overrides.Desc.has_value() ? std::move(overrides.Desc.value()) : std::wstring(DefaultDesc), \
44 ArgumentKind, \
45 overrides.Required.value_or(Argument::DefaultRequired), \
46 overrides.Limit.value_or(Argument::DefaultLimit)};
47
48 WSLC_ARGUMENTS(WSLC_ARG_CREATE_CASE)
49 #undef WSLC_ARG_CREATE_CASE
50
51 default:
52 THROW_HR(E_UNEXPECTED);
53 }
54 }
55
56 // Retrieves the usage string of the Argument, based on its Alias and Name.
57 // The format is "-alias,--name" or just "--name" if no alias.
58 std::wstring Argument::GetUsageString() const
59 {
60 std::wostringstream strstr;
61 if (!m_alias.empty())
62 {
63 strstr << WSLC_CLI_ARG_ID_CHAR << m_alias << L',';
64 }
65
66 strstr << WSLC_CLI_ARG_ID_CHAR << WSLC_CLI_ARG_ID_CHAR << m_name;
67 return strstr.str();
68 }
69 } // namespace wsl::windows::wslc