2016-12-13 14:31:35 -10:00
|
|
|
|
// 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;
|
|
|
|
|
using System.Collections.Generic;
|
2016-12-16 01:04:09 -08:00
|
|
|
|
using System.Linq;
|
2016-12-13 14:31:35 -10:00
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli
|
|
|
|
|
{
|
|
|
|
|
public abstract class DotNetTopLevelCommandBase
|
|
|
|
|
{
|
|
|
|
|
protected abstract string CommandName { get; }
|
|
|
|
|
protected abstract string FullCommandNameLocalized { get; }
|
2017-01-06 10:58:23 -10:00
|
|
|
|
protected abstract string ArgumentName { get; }
|
|
|
|
|
protected abstract string ArgumentDescriptionLocalized { get; }
|
2016-12-16 01:04:09 -08:00
|
|
|
|
internal abstract List<Func<DotNetSubCommandBase>> SubCommands { get; }
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
|
|
|
|
public int RunCommand(string[] args)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
|
|
|
|
|
2017-03-06 11:57:19 -08:00
|
|
|
|
var result = Parser.DotnetCommand[CommandName]
|
|
|
|
|
.Parse(args);
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
2017-03-06 11:57:19 -08:00
|
|
|
|
Reporter.Verbose.WriteLine(result.Diagram());
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
2017-03-06 11:57:19 -08:00
|
|
|
|
var command = result[CommandName];
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
2017-03-06 11:57:19 -08:00
|
|
|
|
if (command.HasOption("help"))
|
2016-12-13 14:31:35 -10:00
|
|
|
|
{
|
2017-03-06 11:57:19 -08:00
|
|
|
|
result.ShowHelp();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-12-16 01:04:09 -08:00
|
|
|
|
|
2017-03-06 11:57:19 -08:00
|
|
|
|
if (result.Errors.Any())
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine(result.Errors.First().Message.Red());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2016-12-16 01:04:09 -08:00
|
|
|
|
|
2017-03-06 11:57:19 -08:00
|
|
|
|
var subCommand = SubCommands
|
|
|
|
|
.Select(c => c())
|
|
|
|
|
.FirstOrDefault(c => c.Name == command.AppliedOptions.First().Name);
|
2016-12-16 01:04:09 -08:00
|
|
|
|
|
2017-03-06 11:57:19 -08:00
|
|
|
|
var fileOrDirectory = command.AppliedOptions
|
|
|
|
|
.First()
|
|
|
|
|
.Arguments
|
|
|
|
|
.FirstOrDefault();
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-03-06 11:57:19 -08:00
|
|
|
|
return subCommand.Run(fileOrDirectory);
|
2016-12-13 14:31:35 -10:00
|
|
|
|
}
|
|
|
|
|
catch (GracefulException e)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine(e.Message.Red());
|
2017-03-06 11:57:19 -08:00
|
|
|
|
subCommand.ShowHelp();
|
2016-12-13 14:31:35 -10:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-06 11:57:19 -08:00
|
|
|
|
}
|