forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.h
More file actions
134 lines (110 loc) · 5.98 KB
/
Copy pathCommand.h
File metadata and controls
134 lines (110 loc) · 5.98 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#include "Argument.h"
#include "ExecutionContext.h"
#include "Invocation.h"
#include "Resources.h"
#include <winget/UserSettings.h>
#include <winget/ExperimentalFeature.h>
#include <winget/GroupPolicy.h>
#include <initializer_list>
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
namespace AppInstaller::CLI
{
struct CommandException
{
CommandException(Resource::LocString message) : m_message(std::move(message)) {}
// The message should be a localized string.
// The parameters can be either localized or not.
// We 'convert' the param to a localization independent view here if needed.
CommandException(Resource::LocString message, Resource::LocString param) : m_message(std::move(message)), m_params({ param }) {}
CommandException(Resource::LocString message, std::string_view param) : m_message(std::move(message)), m_params({ Utility::LocIndString{ param } }) {}
// The message should be a localized string, but the replacement and parameters are not.
// This supports replacing %1 in the message with the replace value.
CommandException(Resource::LocString message, Utility::LocIndView replace, std::vector<Utility::LocIndString>&& params) :
m_message(std::move(message)), m_replace(replace), m_params(std::move(params)) {}
const Utility::LocIndString Message() const;
const std::vector<Utility::LocIndString>& Params() const { return m_params; }
private:
Resource::LocString m_message;
std::optional<Utility::LocIndString> m_replace;
std::vector<Utility::LocIndString> m_params;
};
struct Command
{
// Controls the visibility of the field.
enum class Visibility
{
// Shown in help.
Show,
// Not shown in help.
Hidden,
};
Command(std::string_view name, std::string_view parent) :
Command(name, parent, Settings::ExperimentalFeature::Feature::None) {}
Command(std::string_view name, std::string_view parent, Command::Visibility visibility) :
Command(name, parent, visibility, Settings::ExperimentalFeature::Feature::None) {}
Command(std::string_view name, std::string_view parent, Settings::ExperimentalFeature::Feature feature) :
Command(name, parent, Command::Visibility::Show, feature) {}
Command(std::string_view name, std::string_view parent, Settings::TogglePolicy::Policy groupPolicy) :
Command(name, parent, Command::Visibility::Show, Settings::ExperimentalFeature::Feature::None, groupPolicy) {}
Command(std::string_view name, std::string_view parent, Command::Visibility visibility, Settings::ExperimentalFeature::Feature feature) :
Command(name, parent, visibility, feature, Settings::TogglePolicy::Policy::None) {}
Command(std::string_view name, std::string_view parent, Command::Visibility visibility, Settings::ExperimentalFeature::Feature feature, Settings::TogglePolicy::Policy groupPolicy);
virtual ~Command() = default;
Command(const Command&) = default;
Command& operator=(const Command&) = default;
Command(Command&&) = default;
Command& operator=(Command&&) = default;
// The character used to split between commands and their parents in FullName.
constexpr static char ParentSplitChar = ':';
std::string_view Name() const { return m_name; }
const std::string& FullName() const { return m_fullName; }
Command::Visibility GetVisibility() const;
Settings::ExperimentalFeature::Feature Feature() const { return m_feature; }
Settings::TogglePolicy::Policy GroupPolicy() const { return m_groupPolicy; }
virtual std::vector<std::unique_ptr<Command>> GetCommands() const { return {}; }
virtual std::vector<Argument> GetArguments() const { return {}; }
std::vector<std::unique_ptr<Command>> GetVisibleCommands() const;
std::vector<Argument> GetVisibleArguments() const;
virtual Resource::LocString ShortDescription() const { return {}; }
virtual Resource::LocString LongDescription() const { return {}; }
virtual void OutputIntroHeader(Execution::Reporter& reporter) const;
virtual void OutputHelp(Execution::Reporter& reporter, const CommandException* exception = nullptr) const;
virtual std::string HelpLink() const { return {}; }
virtual std::unique_ptr<Command> FindSubCommand(Invocation& inv) const;
virtual void ParseArguments(Invocation& inv, Execution::Args& execArgs) const;
virtual void ValidateArguments(Execution::Args& execArgs) const;
virtual void Complete(Execution::Context& context) const;
virtual void Complete(Execution::Context& context, Execution::Args::Type valueType) const;
virtual void Execute(Execution::Context& context) const;
protected:
virtual void ValidateArgumentsInternal(Execution::Args& execArgs) const;
virtual void ExecuteInternal(Execution::Context& context) const;
private:
std::string_view m_name;
std::string m_fullName;
Command::Visibility m_visibility;
Settings::ExperimentalFeature::Feature m_feature;
Settings::TogglePolicy::Policy m_groupPolicy;
};
template <typename Container>
Container InitializeFromMoveOnly(std::initializer_list<typename Container::value_type> il)
{
using Value = typename Container::value_type;
Container result;
for (const auto& v : il)
{
result.emplace_back(std::move(*const_cast<Value*>(&v)));
}
return result;
}
int Execute(Execution::Context& context, std::unique_ptr<Command>& command);
}