Skip to content

Commit

Permalink
Long Aliases indicated by -+
Browse files Browse the repository at this point in the history
  • Loading branch information
Trenly committed Apr 29, 2022
1 parent fb5838c commit 963728c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/AppInstallerCLICore/Argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace AppInstaller::CLI
case Args::Type::ExperimentalArg:
return Argument{ "arg", NoAlias, Args::Type::ExperimentalArg, Resource::String::ExperimentalArgumentDescription, ArgumentType::Flag, ExperimentalFeature::Feature::ExperimentalArg };
case Args::Type::Rename:
return Argument{ "rename", 'r', Args::Type::Rename, Resource::String::RenameArgumentDescription, ArgumentType::Standard, false };
return Argument{ "rename", "r", Args::Type::Rename, Resource::String::RenameArgumentDescription, ArgumentType::Standard, false };
case Args::Type::Purge:
return Argument{ "purge", NoAlias, Args::Type::Purge, Resource::String::PurgeArgumentDescription, ArgumentType::Flag, false };
case Args::Type::Preserve:
Expand Down
2 changes: 2 additions & 0 deletions src/AppInstallerCLICore/Argument.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#define APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR '-'
#define APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_STRING "-"
#define APPINSTALLER_CLI_LONG_ALIAS_IDENTIFIER_CHAR '+'
#define APPINSTALLER_CLI_LONG_ALIAS_IDENTIFIER_STRING "+"
#define APPINSTALLER_CLI_ARGUMENT_SPLIT_CHAR '='
#define APPINSTALLER_CLI_HELP_ARGUMENT_TEXT_CHAR '?'
#define APPINSTALLER_CLI_HELP_ARGUMENT_TEXT_STRING "?"
Expand Down
74 changes: 69 additions & 5 deletions src/AppInstallerCLICore/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ namespace AppInstaller::CLI
{
infoOut << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Name();
}
else if(strlen(arg.Alias()) > 1)
{
infoOut << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << APPINSTALLER_CLI_LONG_ALIAS_IDENTIFIER_CHAR << arg.Alias();
}
else
{
infoOut << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Alias();
Expand Down Expand Up @@ -226,7 +230,14 @@ namespace AppInstaller::CLI
std::ostringstream strstr;
if (arg.Alias() != Argument::NoAlias)
{
strstr << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Alias() << ',';
if (strlen(arg.Alias()) > 1)
{
strstr << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << APPINSTALLER_CLI_LONG_ALIAS_IDENTIFIER_CHAR << arg.Alias() << ',';
}
else
{
strstr << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Alias() << ',';
}
}
strstr << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR << arg.Name();

Expand Down Expand Up @@ -488,20 +499,73 @@ namespace AppInstaller::CLI
{
return CommandException(Resource::String::InvalidArgumentSpecifierError, currArg);
}
// The currentArg is --
// Now it must be at least 2 chars
else if (currArg[1] != APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR && currArg[1] != APPINSTALLER_CLI_LONG_ALIAS_IDENTIFIER_CHAR)
{
// Parse the single character alias argument
std::string currChar(1, currArg.at(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.assign(1, currArg.at(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 (Utility::CaseInsensitiveEquals(currArg, "--"))
{
m_onlyPositionalArgumentsRemain = true;
}
// The currentArg is more than 2 chars, both of which are --
// The currentArg is more than 2 chars
else
{
// This is an arg name or alias, find it and process its value if needed.
// Skip the identifier chars.
size_t argStart = currArg.find_first_not_of(APPINSTALLER_CLI_ARGUMENT_IDENTIFIER_CHAR);
std::string_view argName = currArg.substr(argStart);
// If the argument starts at position 1, there was a single dash and the argument is an alias
// If the argument starts at position 1, there was a single dash and the argument is a long alias
bool isAlias = argStart == 1;
// If the argument is a long alias, we need to also skip the alias character
argStart += isAlias;
std::string_view argName = currArg.substr(argStart);
bool argFound = false;

bool hasValue = false;
Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ namespace AppInstaller::CLI::Resource
WINGET_DEFINE_RESOURCE_STRINGID(ShowLabelWindowsLibrariesDependencies);
WINGET_DEFINE_RESOURCE_STRINGID(ShowVersion);
WINGET_DEFINE_RESOURCE_STRINGID(SilentArgumentDescription);
WINGET_DEFINE_RESOURCE_STRINGID(SingleCharAfterDashError);
WINGET_DEFINE_RESOURCE_STRINGID(SourceAddAlreadyExistsDifferentArg);
WINGET_DEFINE_RESOURCE_STRINGID(SourceAddAlreadyExistsDifferentName);
WINGET_DEFINE_RESOURCE_STRINGID(SourceAddAlreadyExistsMatch);
Expand Down
3 changes: 3 additions & 0 deletions src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,9 @@ They can be configured through the settings file 'winget settings'.</value>
<data name="SilentArgumentDescription" xml:space="preserve">
<value>Request silent installation</value>
</data>
<data name="SingleCharAfterDashError" xml:space="preserve">
<value>Only the single character alias can occur after a single -</value>
</data>
<data name="SourceAddAlreadyExistsDifferentArg" xml:space="preserve">
<value>A source with the given name already exists and refers to a different location:</value>
</data>
Expand Down

0 comments on commit 963728c

Please sign in to comment.