2017-03-06 11:57:19 -08: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;
|
2017-03-08 16:02:24 -08:00
|
|
|
using System.Linq;
|
2017-03-06 11:57:19 -08:00
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli
|
|
|
|
{
|
|
|
|
public static class ParseResultExtensions
|
|
|
|
{
|
2017-03-08 16:02:24 -08:00
|
|
|
public static void ShowHelp(this ParseResult parseResult) =>
|
2018-04-30 13:42:47 -07:00
|
|
|
Console.WriteLine(parseResult.Command().HelpView().TrimEnd());
|
2017-03-09 12:31:34 -08:00
|
|
|
|
2017-03-13 13:29:03 -07:00
|
|
|
public static void ShowHelpOrErrorIfAppropriate(this ParseResult parseResult)
|
2017-03-23 08:15:41 -07:00
|
|
|
{
|
|
|
|
parseResult.ShowHelpIfRequested();
|
|
|
|
|
|
|
|
if (parseResult.Errors.Any())
|
|
|
|
{
|
|
|
|
throw new CommandParsingException(
|
|
|
|
message: string.Join(Environment.NewLine,
|
|
|
|
parseResult.Errors.Select(e => e.Message)),
|
2018-04-30 13:42:47 -07:00
|
|
|
helpText: parseResult?.Command()?.HelpView().TrimEnd());
|
2017-03-23 08:15:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void ShowHelpIfRequested(this ParseResult parseResult)
|
2017-03-09 12:31:34 -08:00
|
|
|
{
|
2017-03-14 11:30:35 -07:00
|
|
|
var appliedCommand = parseResult.AppliedCommand();
|
|
|
|
|
|
|
|
if (appliedCommand.HasOption("help") ||
|
|
|
|
appliedCommand.Arguments.Contains("-?") ||
|
|
|
|
appliedCommand.Arguments.Contains("/?"))
|
2017-03-13 15:29:39 -07:00
|
|
|
{
|
|
|
|
// NOTE: this is a temporary stage in refactoring toward the ClicCommandLineParser being used at the CLI entry point.
|
2018-04-30 13:42:47 -07:00
|
|
|
throw new HelpException(parseResult.Command().HelpView().TrimEnd());
|
2017-03-13 15:29:39 -07:00
|
|
|
}
|
2017-03-09 12:31:34 -08:00
|
|
|
}
|
2017-03-06 11:57:19 -08:00
|
|
|
}
|
|
|
|
}
|