dotnet-installer/src/dotnet/ParseResultExtensions.cs

31 lines
1.1 KiB
C#
Raw Normal View History

// 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;
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) =>
Console.WriteLine(parseResult.Command().HelpView());
2017-03-13 13:29:03 -07:00
public static void ShowHelpOrErrorIfAppropriate(this ParseResult parseResult)
{
2017-03-13 13:29:03 -07:00
if (parseResult.Errors.Any())
{
throw new CommandParsingException(
string.Join(Environment.NewLine,
parseResult.Errors.Select(e => e.Message)));
}
if (parseResult.AppliedCommand().HasOption("help"))
{
// NOTE: this is a temporary stage in refactoring toward the ClicCommandLineParser being used at the CLI entry point.
throw new HelpException(parseResult.Command().HelpView());
}
}
}
}