ExecutionArgs.h (10765B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <string> 5 #include <string_view> 6 #include <map> 7 #include <vector> 8 9 namespace AppInstaller::CLI::Execution 10 { 11 struct Args 12 { 13 enum class Type : uint32_t 14 { 15 // Args to specify where to get app 16 Query, // Query to be performed against index 17 MultiQuery, // Like query, but can take multiple values 18 Manifest, // Provide the app manifest directly 19 20 // Query filtering criteria and query behavior 21 Id, 22 Name, 23 Moniker, 24 Tag, 25 Command, 26 Source, // Index source to be queried against 27 Count, // Maximum query results 28 Exact, // Exact match required 29 30 // Manifest selection behavior after an app is found 31 Version, 32 Channel, 33 34 // Install behavior 35 Interactive, 36 Silent, 37 Locale, 38 Log, 39 CustomSwitches, // CustomSwitches args are args passed to the installer in addition to any defined in the manifest 40 Override, // Override args are (and the only args) directly passed to installer 41 InstallLocation, 42 InstallScope, 43 InstallArchitecture, 44 InstallerArchitecture, 45 InstallerType, 46 HashOverride, // Ignore hash mismatches 47 SkipDependencies, // Skip dependencies 48 IgnoreLocalArchiveMalwareScan, // Ignore the local malware scan on archive files 49 AcceptPackageAgreements, // Accept all license agreements for packages 50 Rename, // Renames the file of the executable. Only applies to the portable installerType 51 NoUpgrade, // Install flow should not try to convert to upgrade flow upon finding existing installed version 52 AllowReboot, // Allows the reboot flow to proceed if applicable 53 54 // Uninstall behavior 55 Purge, // Removes all files and directories related to a package during an uninstall. Only applies to the portable installerType. 56 Preserve, // Retains any files and directories created by the portable exe. 57 ProductCode, // Uninstalls using the product code as the identifier. 58 AllVersions, // Uninstall all versions of the package 59 TargetVersion, // The specific version to target 60 61 //Source Command 62 SourceName, 63 SourceType, 64 SourceArg, 65 ForceSourceReset, 66 SourceExplicit, 67 SourceTrustLevel, 68 69 //Hash Command 70 HashFile, 71 Msix, // Flag to indicate the input file is msix 72 73 //Validate Command 74 ValidateManifest, 75 IgnoreWarnings, 76 77 // Complete Command 78 Word, 79 CommandLine, 80 Position, 81 82 // Export Command 83 IncludeVersions, 84 85 // Import Command 86 ImportFile, 87 IgnoreUnavailable, 88 IgnoreVersions, 89 90 // Download Command 91 DownloadDirectory, 92 SkipMicrosoftStorePackageLicense, 93 Platform, 94 OSVersion, 95 96 // Setting Command 97 AdminSettingEnable, 98 AdminSettingDisable, 99 SettingName, 100 SettingValue, 101 102 // Upgrade command 103 All, // Used in Update command to update all installed packages to latest 104 IncludeUnknown, // Used in Upgrade command to allow upgrades of packages with unknown versions 105 IncludePinned, // Used in Upgrade command to allow upgrades to pinned packages (only for pinning type of pins) 106 UninstallPrevious, // Used in Upgrade command to override the default manifest behavior to UninstallPrevious 107 108 // Show command 109 ListVersions, // Used in Show command to list all available versions of an app 110 111 // List Command 112 Upgrade, // Used in List command to only show versions with upgrades 113 114 // Pin command 115 GatedVersion, // Differs from Version in that this supports wildcards 116 BlockingPin, 117 PinInstalled, 118 119 // Error command 120 ErrorInput, 121 122 // Resume Command 123 ResumeId, 124 IgnoreResumeLimit, 125 126 // Font Command 127 Family, 128 129 // Stub package (extended features) 130 ExtendedFeaturesEnable, 131 ExtendedFeaturesDisable, 132 133 // Configuration 134 ConfigurationFile, 135 ConfigurationAcceptWarning, 136 ConfigurationSuppressPrologue, 137 ConfigurationProcessorPath, 138 ConfigurationModulePath, 139 ConfigurationExportPackageId, 140 ConfigurationExportModule, 141 ConfigurationExportResource, 142 ConfigurationExportAll, 143 ConfigurationHistoryItem, 144 ConfigurationHistoryRemove, 145 ConfigurationStatusWatch, 146 147 // DSCv3 resources 148 DscResourceFunctionGet, 149 DscResourceFunctionSet, 150 DscResourceFunctionWhatIf, 151 DscResourceFunctionTest, 152 DscResourceFunctionDelete, 153 DscResourceFunctionExport, 154 DscResourceFunctionValidate, 155 DscResourceFunctionResolve, 156 DscResourceFunctionAdapter, 157 DscResourceFunctionSchema, 158 DscResourceFunctionManifest, 159 160 // Common arguments 161 NoVT, // Disable VirtualTerminal outputs 162 RetroStyle, // Makes progress display as retro 163 RainbowStyle, // Makes progress display as a rainbow 164 Help, // Show command usage 165 Info, // Show general info about WinGet 166 VerboseLogs, // Increases winget logging level to verbose 167 DisableInteractivity, // Disable interactive prompts 168 Wait, // Prompts the user to press any key before exiting 169 OpenLogs, // Opens the default logs directory after executing the command 170 Force, // Forces the execution of the workflow with non security related issues 171 OutputFile, 172 Correlation, 173 174 DependencySource, // Index source to be queried against for finding dependencies 175 CustomHeader, // Optional Rest source header 176 AcceptSourceAgreements, // Accept all source agreements 177 178 AuthenticationMode, // Authentication mode (silent, silentPreferred or interactive) 179 AuthenticationAccount, // Authentication account to be used 180 181 // Network Behavior 182 Proxy, // Set a proxy to use in this execution 183 NoProxy, // Do not use the default proxy 184 185 ToolVersion, 186 187 // Used for demonstration purposes 188 ExperimentalArg, 189 190 // This should always be at the end 191 Max 192 }; 193 194 template<typename... T, std::enable_if_t<(... && std::is_same_v<T, Args::Type>), bool> = true> 195 bool Contains(T... arg) const 196 { 197 return (... && (m_parsedArgs.count(arg) != 0)); 198 } 199 200 const std::vector<std::string>* GetArgs(Type arg) const 201 { 202 auto itr = m_parsedArgs.find(arg); 203 return (itr == m_parsedArgs.end() ? nullptr : &(itr->second)); 204 } 205 206 std::string_view GetArg(Type arg) const 207 { 208 auto itr = m_parsedArgs.find(arg); 209 210 if (itr == m_parsedArgs.end()) 211 { 212 return {}; 213 } 214 215 return itr->second[0]; 216 } 217 218 size_t GetCount(Type arg) const 219 { 220 auto args = GetArgs(arg); 221 return (args ? args->size() : 0); 222 } 223 224 bool AddArg(Type arg) 225 { 226 return m_parsedArgs[arg].empty(); 227 } 228 229 void AddArg(Type arg, std::string value) 230 { 231 m_parsedArgs[arg].emplace_back(std::move(value)); 232 } 233 234 void AddArg(Type arg, std::string_view value) 235 { 236 m_parsedArgs[arg].emplace_back(value); 237 } 238 239 bool Empty() 240 { 241 return m_parsedArgs.empty(); 242 } 243 244 size_t GetArgsCount() const 245 { 246 return m_parsedArgs.size(); 247 } 248 249 std::vector<Type> GetTypes() const 250 { 251 std::vector<Type> types; 252 253 for (auto const& i : m_parsedArgs) 254 { 255 types.emplace_back(i.first); 256 } 257 258 return types; 259 } 260 261 // If the user passes the same value multiple times inside a MultiQuery, operations will be repeated 262 // Since there currently is not a way to include search options within a MultiQuery, processing duplicates 263 // does not make sense within a single invocation 264 void MakeMultiQueryContainUniqueValues() 265 { 266 auto itr = m_parsedArgs.find(Type::MultiQuery); 267 268 // If there is not a value in MultiQuery, or there is only one value, it is presumed to be unique 269 if (itr == m_parsedArgs.end() || itr->second.size() == 1) 270 { 271 return; 272 } 273 274 std::set<std::string> querySet; 275 std::vector<std::string>& queryStrings = itr->second; 276 277 queryStrings.erase(std::remove_if(queryStrings.begin(), queryStrings.end(), [&](const std::string value) { return !querySet.insert(value).second; }), queryStrings.end()); 278 } 279 280 // If we get a single value for multi-query, we remove the argument and add it back as a single query. 281 // This way the rest of the code can assume that if there is a MultiQuery we will always have multiple values, 282 // and if there is a single one it will be in the Query type. 283 void MoveMultiQueryToSingleQueryIfNeeded() 284 { 285 auto itr = m_parsedArgs.find(Type::MultiQuery); 286 if (itr != m_parsedArgs.end() && itr->second.size() == 1) 287 { 288 // A test ensures that commands don't have both Query and MultiQuery arguments, 289 // so if we had a MultiQuery value, we can be sure there is no Query value 290 m_parsedArgs[Type::Query].emplace_back(std::move(itr->second[0])); 291 m_parsedArgs.erase(itr); 292 } 293 } 294 295 private: 296 std::map<Type, std::vector<std::string>> m_parsedArgs; 297 }; 298 }