Skip to content

Commit 5f7edb4

Browse files
committed
could do some more work on the formatting, but features are complete now
1 parent bf31e1b commit 5f7edb4

File tree

6 files changed

+95
-18
lines changed

6 files changed

+95
-18
lines changed

GoCommando/DescriptionAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GoCommando
66
/// Apply this attribute to a property of a command class (which is also decorated with <see cref="ParameterAttribute"/>) in
77
/// order to provide a description of the parameter
88
/// </summary>
9-
[AttributeUsage(AttributeTargets.Property)]
9+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
1010
public class DescriptionAttribute : Attribute
1111
{
1212
public string DescriptionText { get; }

GoCommando/Go.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,16 @@ static void InnerRun()
8181
{
8282
if (command.Parameters.Any())
8383
{
84-
Console.WriteLine(@"Type
84+
Console.WriteLine(@"{0}
85+
86+
Type
8587
86-
{0} {1} <args>
88+
{1} {2} <args>
8789
8890
where <args> can consist of the following parameters:
8991
90-
{2}",
92+
{3}",
93+
command.Description,
9194
exe,
9295
command.Command,
9396
string.Join(Environment.NewLine, command.Parameters.Select(parameter => FormatParameter(parameter, settings))));
@@ -102,13 +105,11 @@ static void InnerRun()
102105
}
103106
return;
104107
}
105-
else
106-
{
107-
Console.WriteLine("Unknown command '{0}'", helpSwitch.Value);
108-
}
108+
109+
throw new GoCommandoException($"Unknown command: '{helpSwitch.Value}'");
109110
}
110111

111-
var availableCommands = string.Join(Environment.NewLine, commandTypes.Select(c => " " + c.Command));
112+
var availableCommands = string.Join(Environment.NewLine, commandTypes.Select(c => $" {c.Command} - {c.Description}"));
112113

113114
Console.WriteLine($@"The following commands are available:
114115
@@ -157,30 +158,32 @@ static string FormatParameter(Parameter parameter, Settings settings)
157158
additionalProperties.Add("optional");
158159
}
159160

160-
var additionalPropertiesText = additionalProperties.Any() ? $" ({string.Join("/", additionalProperties)})" : "";
161+
var additionalPropertiesText = additionalProperties.Any()
162+
? $" ({string.Join("/", additionalProperties)})"
163+
: "";
161164

162-
var helpText = parameter.DescriptionText ?? "";
165+
var helpText = " " + (parameter.DescriptionText ?? "(no help text available)");
163166

164167
var examplesText = !parameter.ExampleValues.Any()
165168
? ""
166169
: FormatExamples(parameter, settings);
167170

168-
return $@" {settings.SwitchPrefix}{parameter.Name}{shorthand}{additionalPropertiesText}
171+
var switchText = $"{settings.SwitchPrefix}{parameter.Name}{shorthand}{additionalPropertiesText}";
169172

173+
return $@" {switchText}
170174
{helpText}
171-
{examplesText}
172-
";
175+
{examplesText}";
173176
}
174177

175178
static string FormatExamples(Parameter parameter, Settings settings)
176179
{
177180
var examples = string.Join(Environment.NewLine, parameter.ExampleValues
178-
.Select(e => $" {settings.SwitchPrefix}{parameter.Name} {e}"));
181+
.Select(e => $" {settings.SwitchPrefix}{parameter.Name} {e}"));
179182

180183
return $@"
181-
Examples:
182-
183-
{examples}";
184+
Examples:
185+
{examples}
186+
";
184187
}
185188

186189
internal static List<CommandInvoker> GetCommands(Settings settings)

GoCommando/GoCommando.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Compile Include="GoCommandoException.cs" />
5252
<Compile Include="Internals\Command.cs" />
5353
<Compile Include="Internals\Parameter.cs" />
54+
<Compile Include="Internals\StringExtensions.cs" />
5455
<Compile Include="ParameterAttribute.cs" />
5556
<Compile Include="Properties\AssemblyInfo.cs" />
5657
<Compile Include="Internals\Settings.cs" />

GoCommando/Internals/Command.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ static TAttribute GetSingleAttributeOrNull<TAttribute>(PropertyInfo p) where TAt
5555
public Type Type { get; }
5656
public IEnumerable<Parameter> Parameters { get; }
5757

58+
public string Description => Type.GetCustomAttribute<DescriptionAttribute>()?.DescriptionText ??
59+
"(no help text for this command)";
60+
61+
5862
public void Invoke(IEnumerable<Switch> switches)
5963
{
6064
var commandInstance = (ICommand)Activator.CreateInstance(Type);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
5+
namespace GoCommando.Internals
6+
{
7+
static class StringExtensions
8+
{
9+
public static string WrappedAt(this string str, int width)
10+
{
11+
var twoLineBreaks = Environment.NewLine + Environment.NewLine;
12+
13+
var sections = str.Split(new[] { twoLineBreaks },
14+
StringSplitOptions.RemoveEmptyEntries);
15+
16+
return string.Join(twoLineBreaks, sections.Select(section => WrapSection(section, width)));
17+
}
18+
19+
static string WrapSection(string section, int width)
20+
{
21+
var oneLongString = string.Join(" ",
22+
section.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
23+
24+
var words = oneLongString.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
25+
26+
var builder = new StringBuilder();
27+
28+
var currentLineLength = 0;
29+
30+
for (var index = 0; index < words.Length; index++)
31+
{
32+
var word = words[index];
33+
builder.Append(word);
34+
currentLineLength += word.Length;
35+
36+
if (index < words.Length - 1)
37+
{
38+
var nextWord = words[index];
39+
40+
var spaceLeftOnCurrentLine = width - currentLineLength - 1; // -1 to leave room for space...
41+
var nextWordIsTooLong = nextWord.Length > spaceLeftOnCurrentLine;
42+
43+
if (nextWordIsTooLong)
44+
{
45+
builder.AppendLine();
46+
currentLineLength = 0;
47+
}
48+
else
49+
{
50+
builder.Append(" ");
51+
currentLineLength++;
52+
}
53+
}
54+
}
55+
56+
return builder.ToString();
57+
}
58+
59+
public static string Indented(this string str, int indent)
60+
{
61+
var indentedLines = str
62+
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
63+
.Select(line => string.Concat(new string(' ', indent), line));
64+
65+
return string.Join(Environment.NewLine, indentedLines);
66+
}
67+
}
68+
}

TestApp/Commands/RunCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace TestApp.Commands
55
{
66
[Command("run")]
7+
[Description("Runs the program")]
78
public class RunCommand : ICommand
89
{
910
[Description("Specifies the path with which stuff is to be done")]

0 commit comments

Comments
 (0)