// Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Collections.Generic; using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Tools.MSBuild; using Microsoft.DotNet.Cli; using System.Diagnostics; namespace Microsoft.DotNet.Tools.Clean { public class CleanCommand : MSBuildForwardingApp { public CleanCommand(IEnumerable msbuildArgs, string msbuildPath = null) : base(msbuildArgs, msbuildPath) { } public static CleanCommand FromArgs(string[] args, string msbuildPath = null) { DebugHelper.HandleDebugSwitch(ref args); CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false) { Name = "dotnet clean", FullName = LocalizableStrings.AppFullName, Description = LocalizableStrings.AppDescription, HandleRemainingArguments = true, ArgumentSeparatorHelpText = HelpMessageStrings.MSBuildAdditionalArgsHelpText }; app.HelpOption("-h|--help"); CommandArgument projectArgument = app.Argument( $"<{LocalizableStrings.CmdArgProject}>", LocalizableStrings.CmdArgProjDescription); CommandOption outputOption = app.Option( $"-o|--output <{LocalizableStrings.CmdOutputDir}>", LocalizableStrings.CmdOutputDirDescription, CommandOptionType.SingleValue); CommandOption frameworkOption = app.Option( $"-f|--framework <{LocalizableStrings.CmdFramework}>", LocalizableStrings.CmdFrameworkDescription, CommandOptionType.SingleValue); CommandOption configurationOption = app.Option( $"-c|--configuration <{LocalizableStrings.CmdConfiguration}>", LocalizableStrings.CmdConfigurationDescription, CommandOptionType.SingleValue); CommandOption verbosityOption = AddVerbosityOption(app); List msbuildArgs = null; app.OnExecute(() => { msbuildArgs = new List(); if (!string.IsNullOrEmpty(projectArgument.Value)) { msbuildArgs.Add(projectArgument.Value); } msbuildArgs.Add("/t:Clean"); if (outputOption.HasValue()) { msbuildArgs.Add($"/p:OutputPath={outputOption.Value()}"); } if (frameworkOption.HasValue()) { msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}"); } if (configurationOption.HasValue()) { msbuildArgs.Add($"/p:Configuration={configurationOption.Value()}"); } if (verbosityOption.HasValue()) { msbuildArgs.Add($"/verbosity:{verbosityOption.Value()}"); } msbuildArgs.AddRange(app.RemainingArguments); return 0; }); int exitCode = app.Execute(args); if (msbuildArgs == null) { throw new CommandCreationException(exitCode); } return new CleanCommand(msbuildArgs, msbuildPath); } public static int Run(string[] args) { DebugHelper.HandleDebugSwitch(ref args); CleanCommand cmd; try { cmd = FromArgs(args); } catch (CommandCreationException e) { return e.ExitCode; } return cmd.Execute(); } } }