forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cpp
More file actions
773 lines (660 loc) · 28.6 KB
/
Command.cpp
File metadata and controls
773 lines (660 loc) · 28.6 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Command.h"
#include "Resources.h"
#include <winget/UserSettings.h>
namespace AppInstaller::CLI
{
using namespace std::string_view_literals;
using namespace Utility::literals;
using namespace Settings;
constexpr std::string_view s_Command_ArgName_SilentAndInteractive = "silent|interactive"sv;
Command::Command(std::string_view name, std::string_view parent, Command::Visibility visibility, ExperimentalFeature::Feature feature) :
m_name(name), m_visibility(visibility), m_feature(feature)
{
if (!parent.empty())
{
m_fullName.reserve(parent.length() + 1 + name.length());
m_fullName = parent;
m_fullName += ParentSplitChar;
m_fullName += name;
}
else
{
m_fullName = name;
}
}
void Command::OutputIntroHeader(Execution::Reporter& reporter) const
{
reporter.Info() <<
"Windows Package Manager v"_liv << Runtime::GetClientVersion() << ' ' << Resource::String::PreviewVersion << std::endl <<
Resource::String::MainCopyrightNotice << std::endl;
}
void Command::OutputHelp(Execution::Reporter& reporter, const CommandException* exception) const
{
// Header
OutputIntroHeader(reporter);
reporter.EmptyLine();
// Error if given
if (exception)
{
reporter.Error() <<
exception->Message() << " : '"_liv << exception->Param() << '\'' << std::endl <<
std::endl;
}
// Description
auto infoOut = reporter.Info();
infoOut <<
LongDescription() << std::endl <<
std::endl;
// Example usage for this command
// First create the command chain for output
std::string commandChain = FullName();
size_t firstSplit = commandChain.find_first_of(ParentSplitChar);
if (firstSplit == std::string::npos)
{
commandChain.clear();
}
else
{
commandChain = commandChain.substr(firstSplit);
for (char& c : commandChain)
{
if (c == ParentSplitChar)
{
c = ' ';
}
}
}
// Output the command preamble and command chain
infoOut << Resource::String::Usage << ": winget"_liv << Utility::LocIndView{ commandChain };
auto commands = GetVisibleCommands();
auto arguments = GetVisibleArguments();
bool hasArguments = false;
bool hasOptions = false;
// Output the command token, made optional if arguments are present.
if (!commands.empty())
{
infoOut << ' ';
if (!arguments.empty())
{
infoOut << '[';
}
infoOut << '<' << Resource::String::Command << '>';
if (!arguments.empty())
{
infoOut << ']';
}
}
// Arguments are required by a test to have all positionals first.
for (const auto& arg : arguments)
{
if (arg.Type() == ArgumentType::Positional)
{
hasArguments = true;
infoOut << ' ';
if (!arg.Required())
{
infoOut << '[';
}
infoOut << '[';
if (arg.Alias() == Argument::NoAlias)
{
infoOut << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Name();
}
else
{
infoOut << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Alias();
}
infoOut << "] <"_liv << arg.Name() << '>';
if (!arg.Required())
{
infoOut << ']';
}
}
else
{
hasOptions = true;
infoOut << " [<"_liv << Resource::String::Options << ">]"_liv;
break;
}
}
infoOut <<
std::endl <<
std::endl;
if (!commands.empty())
{
if (Name() == FullName())
{
infoOut << Resource::String::AvailableCommands << std::endl;
}
else
{
infoOut << Resource::String::AvailableSubcommands << std::endl;
}
size_t maxCommandNameLength = 0;
for (const auto& command : commands)
{
maxCommandNameLength = std::max(maxCommandNameLength, command->Name().length());
}
for (const auto& command : commands)
{
size_t fillChars = (maxCommandNameLength - command->Name().length()) + 2;
infoOut << " "_liv << Execution::HelpCommandEmphasis << command->Name() << Utility::LocIndString{ std::string(fillChars, ' ') } << command->ShortDescription() << std::endl;
}
infoOut <<
std::endl <<
Resource::String::HelpForDetails
<< " ["_liv << APPINSTALLER_CLI_HELP_ARGUMENT << ']' << std::endl;
}
if (!arguments.empty())
{
if (!commands.empty())
{
infoOut << std::endl;
}
std::vector<std::string> argNames;
size_t maxArgNameLength = 0;
for (const auto& arg : arguments)
{
std::ostringstream strstr;
if (arg.Alias() != Argument::NoAlias)
{
strstr << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Alias() << ',';
}
strstr << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Name();
argNames.emplace_back(strstr.str());
maxArgNameLength = std::max(maxArgNameLength, argNames.back().length());
}
if (hasArguments)
{
infoOut << Resource::String::AvailableArguments << std::endl;
size_t i = 0;
for (const auto& arg : arguments)
{
const std::string& argName = argNames[i++];
if (arg.Type() == ArgumentType::Positional)
{
size_t fillChars = (maxArgNameLength - argName.length()) + 2;
infoOut << " "_liv << Execution::HelpArgumentEmphasis << argName << Utility::LocIndString{ std::string(fillChars, ' ') } << arg.Description() << std::endl;
}
}
}
if (hasOptions)
{
if (hasArguments)
{
infoOut << std::endl;
}
infoOut << Resource::String::AvailableOptions << std::endl;
size_t i = 0;
for (const auto& arg : arguments)
{
const std::string& argName = argNames[i++];
if (arg.Type() != ArgumentType::Positional)
{
size_t fillChars = (maxArgNameLength - argName.length()) + 2;
infoOut << " "_liv << Execution::HelpArgumentEmphasis << argName << Utility::LocIndString{ std::string(fillChars, ' ') } << arg.Description() << std::endl;
}
}
}
}
// Finally, the link to the documentation pages
std::string helpLink = HelpLink();
if (!helpLink.empty())
{
infoOut << std::endl << Resource::String::HelpLinkPreamble << ' ' << helpLink << std::endl;
}
}
std::unique_ptr<Command> Command::FindSubCommand(Invocation& inv) const
{
auto itr = inv.begin();
if (itr == inv.end() || (*itr)[0] == APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR)
{
// No more command arguments to check, so no command to find
return {};
}
auto commands = GetCommands();
if (commands.empty())
{
// No more subcommands
return {};
}
for (auto& command : commands)
{
if (Utility::CaseInsensitiveEquals(*itr, command->Name()))
{
if (!ExperimentalFeature::IsEnabled(command->Feature()))
{
auto feature = ExperimentalFeature::GetFeature(command->Feature());
AICLI_LOG(CLI, Error, << "Trying to use command: " << *itr << " without enabling feature " << feature.JsonName());
throw CommandException(Resource::String::FeatureDisabledMessage, feature.JsonName());
}
AICLI_LOG(CLI, Info, << "Found subcommand: " << *itr);
inv.consume(itr);
return std::move(command);
}
}
// TODO: If we get to a large number of commands, do a fuzzy search much like git
throw CommandException(Resource::String::UnrecognizedCommand, *itr);
}
// The argument parsing state machine.
// It is broken out to enable completion to process arguments, ignore errors,
// and determine the likely state of the word to be completed.
struct ParseArgumentsStateMachine
{
ParseArgumentsStateMachine(Invocation& inv, Execution::Args& execArgs, std::vector<CLI::Argument> arguments);
ParseArgumentsStateMachine(const ParseArgumentsStateMachine&) = delete;
ParseArgumentsStateMachine& operator=(const ParseArgumentsStateMachine&) = delete;
ParseArgumentsStateMachine(ParseArgumentsStateMachine&&) = default;
ParseArgumentsStateMachine& operator=(ParseArgumentsStateMachine&&) = default;
// Processes the next argument from the invocation.
// Returns true if there was an argument to process;
// returns false if there were none.
bool Step();
// Throws if there was an error during the prior step.
void ThrowIfError() const;
// The current state of the state machine.
// An empty state indicates that the next argument can be anything.
struct State
{
State() = default;
State(Execution::Args::Type type, std::string_view arg) : m_type(type), m_arg(arg) {}
State(CommandException ce) : m_exception(std::move(ce)) {}
// If set, indicates that the next argument is a value for this type.
const std::optional<Execution::Args::Type>& Type() const { return m_type; }
// The actual argument string associated with Type.
const std::string& Arg() const { return m_arg; }
// If set, indicates that the last argument produced an error.
const std::optional<CommandException>& Exception() const { return m_exception; }
private:
std::optional<Execution::Args::Type> m_type;
std::string m_arg;
std::optional<CommandException> m_exception;
};
const State& GetState() const { return m_state; }
bool OnlyPositionalRemain() const { return m_onlyPositionalArgumentsRemain; }
// Gets the next positional argument, or nullptr if there is not one.
const CLI::Argument* NextPositional();
const std::vector<CLI::Argument>& Arguments() const { return m_arguments; }
private:
State StepInternal();
void ProcessAdjoinedValue(Execution::Args::Type type, std::string_view value);
Invocation& m_invocation;
Execution::Args& m_executionArgs;
std::vector<CLI::Argument> m_arguments;
Invocation::iterator m_invocationItr;
std::vector<CLI::Argument>::iterator m_positionalSearchItr;
bool m_onlyPositionalArgumentsRemain = false;
State m_state;
};
ParseArgumentsStateMachine::ParseArgumentsStateMachine(Invocation& inv, Execution::Args& execArgs, std::vector<CLI::Argument> arguments) :
m_invocation(inv),
m_executionArgs(execArgs),
m_arguments(std::move(arguments)),
m_invocationItr(m_invocation.begin()),
m_positionalSearchItr(m_arguments.begin())
{
}
bool ParseArgumentsStateMachine::Step()
{
if (m_invocationItr == m_invocation.end())
{
return false;
}
m_state = StepInternal();
return true;
}
void ParseArgumentsStateMachine::ThrowIfError() const
{
if (m_state.Exception())
{
throw m_state.Exception().value();
}
// If the next argument was to be a value, but none was provided, convert it to an exception.
else if (m_state.Type() && m_invocationItr == m_invocation.end())
{
throw CommandException(Resource::String::MissingArgumentError, m_state.Arg());
}
}
const CLI::Argument* ParseArgumentsStateMachine::NextPositional()
{
// Find the next appropriate positional arg if the current itr isn't one or has hit its limit.
if (m_positionalSearchItr != m_arguments.end() &&
(m_positionalSearchItr->Type() != ArgumentType::Positional || m_executionArgs.GetCount(m_positionalSearchItr->ExecArgType()) == m_positionalSearchItr->Limit()))
{
do
{
++m_positionalSearchItr;
}
while (m_positionalSearchItr != m_arguments.end() && m_positionalSearchItr->Type() != ArgumentType::Positional);
}
if (m_positionalSearchItr == m_arguments.end())
{
return nullptr;
}
return &*m_positionalSearchItr;
}
// Parse arguments as such:
// 1. If argument starts with a single -, only the single character alias is considered.
// a. If the named argument alias (a) needs a VALUE, it can be provided in these ways:
// -a=VALUE
// -a VALUE
// b. If the argument is a flag, additional characters after are treated as if they start
// with a -, repeatedly until the end of the argument is reached. Fails if non-flags hit.
// 2. If the argument starts with a double --, only the full name is considered.
// a. If the named argument (arg) needs a VALUE, it can be provided in these ways:
// --arg=VALUE
// --arg VALUE
// 3. If the argument does not start with any -, it is considered the next positional argument.
// 4. If the argument is only a double --, all further arguments are only considered as positional.
ParseArgumentsStateMachine::State ParseArgumentsStateMachine::StepInternal()
{
std::string_view currArg = *m_invocationItr;
++m_invocationItr;
// If the previous step indicated a value was needed, set it and forget it.
if (m_state.Type())
{
m_executionArgs.AddArg(m_state.Type().value(), currArg);
return {};
}
// This is a positional argument
if (m_onlyPositionalArgumentsRemain || currArg.empty() || currArg[0] != APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR)
{
const CLI::Argument* nextPositional = NextPositional();
if (!nextPositional)
{
return CommandException(Resource::String::ExtraPositionalError, currArg);
}
m_executionArgs.AddArg(nextPositional->ExecArgType(), currArg);
}
// The currentArg must not be empty, and starts with a -
else if (currArg.length() == 1)
{
return CommandException(Resource::String::InvalidArgumentSpecifierError, currArg);
}
// Now it must be at least 2 chars
else if (currArg[1] != APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR)
{
// Parse the single character alias argument
char currChar = currArg[1];
auto itr = std::find_if(m_arguments.begin(), m_arguments.end(), [&](const Argument& arg) { return (currChar == arg.Alias()); });
if (itr == m_arguments.end())
{
return CommandException(Resource::String::InvalidAliasError, currArg);
}
if (itr->Type() == ArgumentType::Flag)
{
m_executionArgs.AddArg(itr->ExecArgType());
for (size_t i = 2; i < currArg.length(); ++i)
{
currChar = currArg[i];
auto itr2 = std::find_if(m_arguments.begin(), m_arguments.end(), [&](const Argument& arg) { return (currChar == arg.Alias()); });
if (itr2 == m_arguments.end())
{
return CommandException(Resource::String::AdjoinedNotFoundError, currArg);
}
else if (itr2->Type() != ArgumentType::Flag)
{
return CommandException(Resource::String::AdjoinedNotFlagError, currArg);
}
else
{
m_executionArgs.AddArg(itr2->ExecArgType());
}
}
}
else if (currArg.length() > 2)
{
if (currArg[2] == APPINSTALLER_CLI_ARGUMENT_SPLIT_CHAR)
{
ProcessAdjoinedValue(itr->ExecArgType(), currArg.substr(3));
}
else
{
return CommandException(Resource::String::SingleCharAfterDashError, currArg);
}
}
else
{
return { itr->ExecArgType(), currArg };
}
}
// The currentArg is at least 2 chars, both of which are --
else if (currArg.length() == 2)
{
m_onlyPositionalArgumentsRemain = true;
}
// The currentArg is more than 2 chars, both of which are --
else
{
// This is an arg name, find it and process its value if needed.
// Skip the double arg identifier chars.
std::string_view argName = currArg.substr(2);
bool argFound = false;
bool hasValue = false;
std::string_view argValue;
size_t splitChar = argName.find_first_of(APPINSTALLER_CLI_ARGUMENT_SPLIT_CHAR);
if (splitChar != std::string::npos)
{
hasValue = true;
argValue = argName.substr(splitChar + 1);
argName = argName.substr(0, splitChar);
}
for (const auto& arg : m_arguments)
{
if (Utility::CaseInsensitiveEquals(argName, arg.Name()))
{
if (arg.Type() == ArgumentType::Flag)
{
if (hasValue)
{
return CommandException(Resource::String::FlagContainAdjoinedError, currArg);
}
m_executionArgs.AddArg(arg.ExecArgType());
}
else if (hasValue)
{
ProcessAdjoinedValue(arg.ExecArgType(), argValue);
}
else
{
return { arg.ExecArgType(), currArg };
}
argFound = true;
break;
}
}
if (!argFound)
{
return CommandException(Resource::String::InvalidNameError, currArg);
}
}
// If we get here, the next argument can be anything again.
return {};
}
void ParseArgumentsStateMachine::ProcessAdjoinedValue(Execution::Args::Type type, std::string_view value)
{
// If the adjoined value is wrapped in quotes, strip them off.
if (value.length() >= 2 && value[0] == '"' && value[value.length() - 1] == '"')
{
value = value.substr(1, value.length() - 2);
}
m_executionArgs.AddArg(type, std::string{ value });
}
void Command::ParseArguments(Invocation& inv, Execution::Args& execArgs) const
{
auto definedArgs = GetArguments();
Argument::GetCommon(definedArgs);
ParseArgumentsStateMachine stateMachine{ inv, execArgs, std::move(definedArgs) };
while (stateMachine.Step())
{
stateMachine.ThrowIfError();
}
}
void Command::ValidateArguments(Execution::Args& execArgs) const
{
// If help is asked for, don't bother validating anything else
if (execArgs.Contains(Execution::Args::Type::Help))
{
return;
}
ValidateArgumentsInternal(execArgs);
}
// Completion can produce one of several things if the completion context is appropriate:
// 1. Sub commands, if the context is immediately after this command.
// 2. Argument names, if a value is not expected.
// 3. Argument values, if one is expected.
void Command::Complete(Execution::Context& context) const
{
CompletionData& data = context.Get<Execution::Data::CompletionData>();
const std::string& word = data.Word();
// The word we are to complete is directly after the command, thus it's sub-commands are potentials.
if (data.BeforeWord().begin() == data.BeforeWord().end())
{
for (const auto& command : GetCommands())
{
if (word.empty() || Utility::CaseInsensitiveStartsWith(command->Name(), word))
{
context.Reporter.Completion() << command->Name() << std::endl;
}
}
}
// Consume what remains, if any, of the preceding values to determine what type the word is.
auto definedArgs = GetArguments();
Argument::GetCommon(definedArgs);
ParseArgumentsStateMachine stateMachine{ data.BeforeWord(), context.Args, std::move(definedArgs) };
// We don't care if there are errors along the way, just do the best that can be done and try to
// complete whatever would be next if the bad strings were simply ignored. To do that we just spin
// through the state until we reach our word.
while (stateMachine.Step());
const auto& state = stateMachine.GetState();
// This means that anything is possible, so argument names are on the table.
if (!state.Type() && !stateMachine.OnlyPositionalRemain())
{
// Use argument names if:
// 1. word is empty
// 2. word is just "-"
// 3. word starts with "--"
if (word.empty() ||
word == APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_STRING ||
Utility::CaseInsensitiveStartsWith(word, APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_STRING APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_STRING))
{
for (const auto& arg : stateMachine.Arguments())
{
if (word.length() <= 2 || Utility::CaseInsensitiveStartsWith(arg.Name(), word.substr(2)))
{
context.Reporter.Completion() << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Name() << std::endl;
}
}
}
// Use argument aliases if the word is already one; allow cycling through them.
else if (Utility::CaseInsensitiveStartsWith(word, APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_STRING) && word.length() == 2)
{
for (const auto& arg : stateMachine.Arguments())
{
if (arg.Alias() != Argument::NoAlias)
{
context.Reporter.Completion() << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Alias() << std::endl;
}
}
}
}
std::optional<Execution::Args::Type> typeToComplete = state.Type();
// We are not waiting on an argument value, so the next could be a positional if the incoming word is not an argument name.
// If there is one, offer to complete it.
if (!typeToComplete && (word.empty() || word[0] != APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR))
{
const auto* nextPositional = stateMachine.NextPositional();
if (nextPositional)
{
typeToComplete = nextPositional->ExecArgType();
}
}
// To enable more complete scenarios, also attempt to parse any arguments after the word to complete.
// This will allow these later values to affect the result of the completion (for instance, if a specific source is listed).
{
ParseArgumentsStateMachine afterWordStateMachine{ data.AfterWord(), context.Args, stateMachine.Arguments() };
while (afterWordStateMachine.Step());
}
// Let the derived command take over supplying context sensitive argument value.
if (typeToComplete)
{
Complete(context, typeToComplete.value());
}
}
void Command::Complete(Execution::Context&, Execution::Args::Type) const
{
// Derived commands must suppy context sensitive argument values.
}
void Command::Execute(Execution::Context& context) const
{
AICLI_LOG(CLI, Info, << "Executing command: " << Name());
if (context.Args.Contains(Execution::Args::Type::Help))
{
OutputHelp(context.Reporter);
}
else
{
ExecuteInternal(context);
}
}
void Command::ValidateArgumentsInternal(Execution::Args& execArgs) const
{
for (const auto& arg : GetArguments())
{
if (!ExperimentalFeature::IsEnabled(arg.Feature()) && execArgs.Contains(arg.ExecArgType()))
{
auto feature = ExperimentalFeature::GetFeature(arg.Feature());
AICLI_LOG(CLI, Error, << "Trying to use argument: " << arg.Name() << " without enabling feature " << feature.JsonName());
throw CommandException(Resource::String::FeatureDisabledMessage, feature.JsonName());
}
if (arg.Required() && !execArgs.Contains(arg.ExecArgType()))
{
throw CommandException(Resource::String::RequiredArgError, arg.Name());
}
if (arg.Limit() < execArgs.GetCount(arg.ExecArgType()))
{
throw CommandException(Resource::String::TooManyArgError, arg.Name());
}
}
if (execArgs.Contains(Execution::Args::Type::Silent) && execArgs.Contains(Execution::Args::Type::Interactive))
{
throw CommandException(Resource::String::TooManyBehaviorsError, s_Command_ArgName_SilentAndInteractive);
}
}
void Command::ExecuteInternal(Execution::Context& context) const
{
context.Reporter.Error() << Resource::String::PendingWorkError << std::endl;
THROW_HR(E_NOTIMPL);
}
Command::Visibility Command::GetVisibility() const
{
if (!ExperimentalFeature::IsEnabled(m_feature))
{
return Command::Visibility::Hidden;
}
return m_visibility;
}
std::vector<std::unique_ptr<Command>> Command::GetVisibleCommands() const
{
auto commands = GetCommands();
commands.erase(
std::remove_if(
commands.begin(), commands.end(),
[](const std::unique_ptr<Command>& command) { return command->GetVisibility() == Command::Visibility::Hidden; }),
commands.end());
return commands;
}
std::vector<Argument> Command::GetVisibleArguments() const
{
auto arguments = GetArguments();
arguments.erase(
std::remove_if(
arguments.begin(), arguments.end(),
[](const Argument& arg) { return arg.GetVisibility() == Argument::Visibility::Hidden; }),
arguments.end());
return arguments;
}
}