public static int Main(string[] args)
{
var console = new Common.Console();
var fileSystem = new PhysicalFileSystem(Directory.GetCurrentDirectory());
try
{
// Remove NuGet.exe.old
RemoveOldFile(fileSystem);
// Import Dependencies
var p = new Program();
p.Initialize(fileSystem, console);
// Add commands to the manager
foreach (ICommand cmd in p.Commands)
{
p.Manager.RegisterCommand(cmd);
}
CommandLineParser parser = new CommandLineParser(p.Manager);
// Parse the command
ICommand command = parser.ParseCommandLine(args) ?? p.HelpCommand;
// Fallback on the help command if we failed to parse a valid command
if (!ArgumentCountValid(command))
{
// Get the command name and add it to the argument list of the help command
string commandName = command.CommandAttribute.CommandName;
// Print invalid command then show help
console.WriteLine(NuGetResources.InvalidArguments, commandName);
p.HelpCommand.ViewHelpForCommand(commandName);
}
else
{
command.Execute();
}
}
catch (AggregateException exception)
{
if (ExceptionUtility.Unwrap(exception) == exception)
{
// If the AggregateException contains more than one InnerException, it cannot be unwrapped. In which case, simply print out individual error messages
var messages = exception.InnerExceptions.Select(ex => ex.Message)
.Distinct(StringComparer.CurrentCulture);
console.WriteError(String.Join("\n", messages));
}
}
catch (Exception e)
{
console.WriteError(ExceptionUtility.Unwrap(e).Message);
return 1;
}
return 0;
}