Rename "help" command to "man"
This commit is contained in:
Родитель
1b93dae4bc
Коммит
2b9bec8897
|
@ -1,119 +0,0 @@
|
|||
// Copyright 2020 the original author or authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using McMaster.Extensions.CommandLineUtils;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Steeltoe.Tooling;
|
||||
using Steeltoe.Tooling.Controllers;
|
||||
|
||||
namespace Steeltoe.Cli
|
||||
{
|
||||
[Command(Description = "Displays documentation on a topic",
|
||||
ExtendedHelpText = @"
|
||||
Overview:
|
||||
Displays documentation for various topics. Run with no arguments for a list of available topics.
|
||||
|
||||
Examples:
|
||||
Display documentation topics:
|
||||
$ st help
|
||||
|
||||
Display documentation for autodetection:
|
||||
$ st help autodetection"
|
||||
)]
|
||||
public class HelpCommand : Command
|
||||
{
|
||||
public const string CommandName = "help";
|
||||
|
||||
[Argument(0, Name = "topic", Description = "Topic")]
|
||||
private string Topic { get; } = null;
|
||||
|
||||
public HelpCommand(IConsole console) : base(console)
|
||||
{
|
||||
}
|
||||
|
||||
protected override Controller GetController()
|
||||
{
|
||||
return new ShowTopicController(Topic);
|
||||
}
|
||||
|
||||
private class ShowTopicController : Controller
|
||||
{
|
||||
private static readonly ILogger<ShowTopicController> Logger =
|
||||
Logging.LoggerFactory.CreateLogger<ShowTopicController>();
|
||||
|
||||
private readonly string _topicsPath = Path.Combine(
|
||||
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
|
||||
"steeltoe.rc",
|
||||
"topics");
|
||||
|
||||
private readonly string _topic;
|
||||
|
||||
public ShowTopicController(string topic)
|
||||
{
|
||||
_topic = topic;
|
||||
}
|
||||
|
||||
protected override void Execute()
|
||||
{
|
||||
Logger.LogDebug($"topics path: {_topicsPath}");
|
||||
if (_topic == null)
|
||||
{
|
||||
ListTopics();
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowTopic(_topic);
|
||||
}
|
||||
}
|
||||
|
||||
private void ListTopics()
|
||||
{
|
||||
var topicPaths = Directory.GetFiles(_topicsPath, "*.txt");
|
||||
var maxTopicLength = topicPaths.Select(Path.GetFileNameWithoutExtension)
|
||||
.Select(topic => topic.Length).Concat(new[] {0}).Max();
|
||||
var descriptionColumn = maxTopicLength + 4;
|
||||
foreach (var topicPath in topicPaths)
|
||||
{
|
||||
var topic = Path.GetFileNameWithoutExtension(topicPath);
|
||||
var description = File.ReadLines(topicPath).First();
|
||||
Context.Console.WriteLine($"{topic.PadRight(descriptionColumn)}{description}");
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowTopic(string topic)
|
||||
{
|
||||
var topicPath = Path.Combine(_topicsPath, $"{topic}.txt");
|
||||
if (!File.Exists(topicPath))
|
||||
{
|
||||
throw new TopicDoesNotExistException(topic);
|
||||
}
|
||||
|
||||
foreach (var line in File.ReadLines(topicPath))
|
||||
{
|
||||
Context.Console.WriteLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
class TopicDoesNotExistException : ToolingException
|
||||
{
|
||||
public TopicDoesNotExistException(string topic) : base($"Topic does not exist: {topic}")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -86,7 +86,10 @@ Examples:
|
|||
var maxTopicLength = topicPaths.Select(Path.GetFileNameWithoutExtension)
|
||||
.Select(topic => topic.Length).Concat(new[] {0}).Max();
|
||||
var descriptionColumn = maxTopicLength + 4;
|
||||
<<<<<<< HEAD
|
||||
Context.Console.WriteLine("Topic".PadRight(descriptionColumn) + "Description");
|
||||
=======
|
||||
>>>>>>> Rename "help" command to "man"
|
||||
foreach (var topicPath in topicPaths)
|
||||
{
|
||||
var topic = Path.GetFileNameWithoutExtension(topicPath);
|
||||
|
|
|
@ -1,98 +0,0 @@
|
|||
// Copyright 2020 the original author or authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using LightBDD.Framework.Scenarios.Extended;
|
||||
using LightBDD.XUnit2;
|
||||
|
||||
namespace Steeltoe.Cli.Test
|
||||
{
|
||||
public class HelpFeature : FeatureSpecs
|
||||
{
|
||||
[Scenario]
|
||||
public void HelpHelp()
|
||||
{
|
||||
Runner.RunScenario(
|
||||
given => an_empty_directory("help_help"),
|
||||
when => the_developer_runs_cli_command("help --help"),
|
||||
then => the_cli_command_should_succeed(),
|
||||
and => the_cli_should_output(new[]
|
||||
{
|
||||
"Displays documentation on a topic",
|
||||
$"Usage: {Program.Name} help [arguments] [options]",
|
||||
"Arguments:",
|
||||
"topic Topic",
|
||||
"Options:",
|
||||
"-?|-h|--help Show help information",
|
||||
"Overview:",
|
||||
"Displays documentation for various topics. Run with no arguments for a list of available topics.",
|
||||
"Examples:",
|
||||
"Display documentation topics:",
|
||||
"$ st help",
|
||||
"Display documentation for autodetection:",
|
||||
"$ st help autodetection",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
[Scenario]
|
||||
public void HelpTooManyArgs()
|
||||
{
|
||||
Runner.RunScenario(
|
||||
given => an_empty_directory("help_too_many_args"),
|
||||
when => the_developer_runs_cli_command("help arg1 arg2"),
|
||||
then => the_cli_should_fail_parse("Unrecognized command or argument 'arg2'")
|
||||
);
|
||||
}
|
||||
|
||||
[Scenario]
|
||||
public void HelpTopicList()
|
||||
{
|
||||
Runner.RunScenario(
|
||||
given => an_empty_directory("help_topic_list"),
|
||||
when => the_developer_runs_cli_command("help"),
|
||||
then => the_cli_command_should_succeed(),
|
||||
and => the_cli_should_output(new[]
|
||||
{
|
||||
"autodetection Application and Service Autodetection",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
[Scenario]
|
||||
public void HelpNotFound()
|
||||
{
|
||||
Runner.RunScenario(
|
||||
given => an_empty_directory("help_topic_not_found"),
|
||||
when => the_developer_runs_cli_command("help no-such-topic"),
|
||||
then => the_cli_should_error(ErrorCode.Tooling, "Topic does not exist: no-such-topic")
|
||||
);
|
||||
}
|
||||
|
||||
[Scenario]
|
||||
public void HelpAutodetection()
|
||||
{
|
||||
Runner.RunScenario(
|
||||
given => an_empty_directory("help_topic_autodetection"),
|
||||
when => the_developer_runs_cli_command("help autodetection"),
|
||||
then => the_cli_command_should_succeed(),
|
||||
and => the_cli_should_output(new[]
|
||||
{
|
||||
"Application and Service Autodetection",
|
||||
"Application Autodetection",
|
||||
"***",
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -64,7 +64,10 @@ namespace Steeltoe.Cli.Test
|
|||
then => the_cli_command_should_succeed(),
|
||||
and => the_cli_should_output(new[]
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
"Topic Description",
|
||||
=======
|
||||
>>>>>>> Rename "help" command to "man"
|
||||
"autodetection Application and Service Autodetection",
|
||||
})
|
||||
);
|
||||
|
|
Загрузка…
Ссылка в новой задаче