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;
|
2017-03-13 20:06:59 -07:00
|
|
|
|
using Microsoft.DotNet.Tools;
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
|
|
|
|
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; }
|
2017-03-10 16:43:44 -08:00
|
|
|
|
internal abstract Dictionary<string, Func<AppliedOption, CommandBase>> SubCommands { get; }
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
|
|
|
|
public int RunCommand(string[] args)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
|
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
|
var parser = Parser.Instance;
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
|
var result = parser.ParseFrom($"dotnet {CommandName}", args);
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
2017-03-13 13:29:03 -07:00
|
|
|
|
result.ShowHelpOrErrorIfAppropriate();
|
2016-12-16 01:04:09 -08:00
|
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
|
var subcommandName = result.Command().Name;
|
2016-12-16 01:04:09 -08:00
|
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-03-13 20:06:59 -07:00
|
|
|
|
var create = SubCommands[subcommandName];
|
|
|
|
|
|
|
|
|
|
var command = create(result["dotnet"][CommandName]);
|
|
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
|
return command.Execute();
|
2016-12-13 14:31:35 -10:00
|
|
|
|
}
|
2017-03-13 20:06:59 -07:00
|
|
|
|
catch (KeyNotFoundException e)
|
|
|
|
|
{
|
|
|
|
|
throw new GracefulException(CommonLocalizableStrings.RequiredCommandNotPassed);
|
|
|
|
|
}
|
2016-12-13 14:31:35 -10:00
|
|
|
|
catch (GracefulException e)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine(e.Message.Red());
|
2017-03-10 16:43:44 -08:00
|
|
|
|
result.ShowHelp();
|
2017-03-06 16:34:05 -08:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
catch (CommandParsingException e)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine(e.Message.Red());
|
2016-12-13 14:31:35 -10:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-10 16:43:44 -08:00
|
|
|
|
}
|