20 Commits
1.1 ... 1.2

Author SHA1 Message Date
Alexey Golub
7d2f934310 Update version 2020-05-11 21:29:26 +03:00
Alexey Golub
95a00b0952 Improve error messages 2020-05-11 21:28:49 +03:00
Alexey Golub
cb3fee65f3 [Analyzers] Update descriptors 2020-05-11 18:30:19 +03:00
Alexey Golub
65628b145a Move extensions closer to the actual classes 2020-05-11 16:51:36 +03:00
Alexey Golub
802bbfccc6 Add CursorLeft and CursorTop to IConsole
Closes #25
2020-05-11 16:29:54 +03:00
Alexey Golub
6e7742a4f3 Show valid values for parameters too 2020-05-08 16:40:19 +03:00
Alexey Golub
f6a1a40471 Cleanup 2020-05-08 16:33:28 +03:00
Domn Werner
33ca4da260 Show valid values of an enum option in help (#53) 2020-05-08 12:40:23 +03:00
Alexey Golub
cbb72b16ae Refactor a bit 2020-05-05 22:23:27 +03:00
Alexey Golub
c58629e999 Pack the analyzers together with CliFx in the main nupkg 2020-04-25 22:25:16 +03:00
Domn Werner
387fb72718 Print help text on specific domain exceptions (#51) 2020-04-25 21:59:03 +03:00
Alexey Golub
e04f0da318 [Analyzers] Remove redundant parameter check 2020-04-25 18:07:28 +03:00
Alexey Golub
d25873ee10 Add CliFx.Analyzers (#50) 2020-04-25 18:03:21 +03:00
Domn Werner
a28223fc8b Show help text on demand (#49) 2020-04-23 10:33:12 +03:00
Alexey Golub
1dab27de55 Fix warnings in tests 2020-04-20 17:20:17 +03:00
Alexey Golub
698629b153 Disable nullability checks for older target frameworks 2020-04-20 17:11:15 +03:00
Alexey Golub
65b66b0d27 Improve exceptions 2020-04-20 16:43:43 +03:00
Alexey Golub
7d3ba612c4 Validate option name length
Closes #40
2020-04-16 16:51:51 +03:00
Alexey Golub
8c3b8d1f49 Throw when a required option is set but doesn't have a value
Closes #47
2020-04-16 16:02:21 +03:00
Alexey Golub
fdd39855ad Use GitHub Actions test logger 2020-03-23 18:15:04 +02:00
57 changed files with 2653 additions and 446 deletions

View File

@@ -22,4 +22,4 @@ jobs:
run: dotnet pack CliFx --configuration Release
- name: Deploy
run: dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{secrets.NUGET_TOKEN}}
run: dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{ secrets.NUGET_TOKEN }}

View File

@@ -20,10 +20,16 @@ jobs:
dotnet-version: 3.1.100
- name: Build & test
run: dotnet test --configuration Release
run: dotnet test --configuration Release --logger GitHubActions
- name: Upload coverage
uses: codecov/codecov-action@v1.0.5
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: CliFx.Tests/bin/Release/Coverage.xml
- name: Upload coverage (analyzers)
uses: codecov/codecov-action@v1.0.5
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: CliFx.Analyzers.Tests/bin/Release/Coverage.xml

View File

@@ -1,3 +1,14 @@
### v1.2 (11-May-2020)
- Added built-in Roslyn analyzers that help catch incorrect usage of the library. Currently, all analyzers report issues as warnings so as to not prevent the project from building. In the future that may change.
- Added an optional parameter to `new CommandException(...)` called `showHelp` which can be used to instruct CliFx to show help for the current command after printing the error. (Thanks [@Domn Werner](https://github.com/domn1995))
- Improved help text shown for enum options and parameters by providing the list of valid values that the enum can accept. (Thanks [@Domn Werner](https://github.com/domn1995))
- Fixed an issue where it was possible to set an option without providing a value, while the option was marked as required.
- Fixed an issue where it was possible to configure an option with an empty name or a name consisting of a single character. If you want to use a single character as a name, you should set the option's short name instead.
- Added `CursorLeft` and `CursorTop` properties to `IConsole` and its implementations. In `VirtualConsole`, these are just auto-properties.
- Improved exception messages.
- Improved exceptions related to user input by also showing help text after the error message. (Thanks [@Domn Werner](https://github.com/domn1995))
### v1.1 (16-Mar-2020)
- Changed `IConsole` interface (and as a result, `SystemConsole` and `VirtualConsole`) to support writing binary data. Instead of `TextReader`/`TextWriter` instances, the streams are now exposed as `StreamReader`/`StreamWriter` which provide the `BaseStream` property that allows raw access. Existing usages inside commands should remain the same because `StreamReader`/`StreamWriter` are compatible with their base classes `TextReader`/`TextWriter`, but if you were using `VirtualConsole` in tests, you may have to update it to the new API. Refer to the readme for more info.

View File

@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
namespace CliFx.Analyzers.Tests
{
public class AnalyzerTestCase
{
public string Name { get; }
public IReadOnlyList<DiagnosticDescriptor> TestedDiagnostics { get; }
public IReadOnlyList<string> SourceCodes { get; }
public AnalyzerTestCase(
string name,
IReadOnlyList<DiagnosticDescriptor> testedDiagnostics,
IReadOnlyList<string> sourceCodes)
{
Name = name;
TestedDiagnostics = testedDiagnostics;
SourceCodes = sourceCodes;
}
public AnalyzerTestCase(
string name,
IReadOnlyList<DiagnosticDescriptor> testedDiagnostics,
string sourceCode)
: this(name, testedDiagnostics, new[] {sourceCode})
{
}
public AnalyzerTestCase(
string name,
DiagnosticDescriptor testedDiagnostic,
string sourceCode)
: this(name, new[] {testedDiagnostic}, sourceCode)
{
}
public override string ToString() => $"{Name} [{string.Join(", ", TestedDiagnostics.Select(d => d.Id))}]";
}
}

View File

@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../CliFx.props" />
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<CollectCoverage>true</CollectCoverage>
<CoverletOutputFormat>opencover</CoverletOutputFormat>
<CoverletOutput>bin/$(Configuration)/Coverage.xml</CoverletOutput>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Gu.Roslyn.Asserts" Version="3.3.0" />
<PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" />
<PackageReference Include="FluentAssertions" Version="5.10.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" />
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" />
<ProjectReference Include="..\CliFx\CliFx.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,489 @@
using System.Collections.Generic;
using CliFx.Analyzers.Tests.Internal;
using Microsoft.CodeAnalysis.Diagnostics;
using Xunit;
namespace CliFx.Analyzers.Tests
{
public class CommandSchemaAnalyzerTests
{
private static DiagnosticAnalyzer Analyzer { get; } = new CommandSchemaAnalyzer();
public static IEnumerable<object[]> GetValidCases()
{
yield return new object[]
{
new AnalyzerTestCase(
"Non-command type",
Analyzer.SupportedDiagnostics,
// language=cs
@"
public class Foo
{
public int Bar { get; set; } = 5;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Command implements interface and has attribute",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Command doesn't have an attribute but is an abstract type",
Analyzer.SupportedDiagnostics,
// language=cs
@"
public abstract class MyCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Parameters with unique order",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandParameter(13)]
public string ParamA { get; set; }
[CommandParameter(15)]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Parameters with unique names",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandParameter(13, Name = ""foo"")]
public string ParamA { get; set; }
[CommandParameter(15, Name = ""bar"")]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Single non-scalar parameter",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandParameter(1)]
public string ParamA { get; set; }
[CommandParameter(2)]
public HashSet<string> ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Non-scalar parameter is last in order",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandParameter(1)]
public string ParamA { get; set; }
[CommandParameter(2)]
public IReadOnlyList<string> ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Option with a proper name",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption(""foo"")]
public string Param { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Option with a proper name and short name",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption(""foo"", 'f')]
public string Param { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Options with unique names",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption(""foo"")]
public string ParamA { get; set; }
[CommandOption(""bar"")]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Options with unique short names",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption('f')]
public string ParamA { get; set; }
[CommandOption('x')]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Options with unique environment variable names",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption('a', EnvironmentVariableName = ""env_var_a"")]
public string ParamA { get; set; }
[CommandOption('b', EnvironmentVariableName = ""env_var_b"")]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
}
public static IEnumerable<object[]> GetInvalidCases()
{
yield return new object[]
{
new AnalyzerTestCase(
"Command is missing the attribute",
DiagnosticDescriptors.CliFx0002,
// language=cs
@"
public class MyCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Command doesn't implement the interface",
DiagnosticDescriptors.CliFx0001,
// language=cs
@"
[Command]
public class MyCommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Parameters with duplicate order",
DiagnosticDescriptors.CliFx0021,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandParameter(13)]
public string ParamA { get; set; }
[CommandParameter(13)]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Parameters with duplicate names",
DiagnosticDescriptors.CliFx0022,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandParameter(13, Name = ""foo"")]
public string ParamA { get; set; }
[CommandParameter(15, Name = ""foo"")]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Multiple non-scalar parameters",
DiagnosticDescriptors.CliFx0023,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandParameter(1)]
public IReadOnlyList<string> ParamA { get; set; }
[CommandParameter(2)]
public HashSet<string> ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Non-last non-scalar parameter",
DiagnosticDescriptors.CliFx0024,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandParameter(1)]
public IReadOnlyList<string> ParamA { get; set; }
[CommandParameter(2)]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Option with an empty name",
DiagnosticDescriptors.CliFx0041,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption("""")]
public string Param { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Option with a name which is too short",
DiagnosticDescriptors.CliFx0042,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption(""a"")]
public string Param { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Options with duplicate names",
DiagnosticDescriptors.CliFx0043,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption(""foo"")]
public string ParamA { get; set; }
[CommandOption(""foo"")]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Options with duplicate short names",
DiagnosticDescriptors.CliFx0044,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption('f')]
public string ParamA { get; set; }
[CommandOption('f')]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Options with duplicate environment variable names",
DiagnosticDescriptors.CliFx0045,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
[CommandOption('a', EnvironmentVariableName = ""env_var"")]
public string ParamA { get; set; }
[CommandOption('b', EnvironmentVariableName = ""env_var"")]
public string ParamB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
}
[Theory]
[MemberData(nameof(GetValidCases))]
public void Valid(AnalyzerTestCase testCase) =>
Analyzer.Should().NotProduceDiagnostics(testCase);
[Theory]
[MemberData(nameof(GetInvalidCases))]
public void Invalid(AnalyzerTestCase testCase) =>
Analyzer.Should().ProduceDiagnostics(testCase);
}
}

View File

@@ -0,0 +1,144 @@
using System.Collections.Generic;
using CliFx.Analyzers.Tests.Internal;
using Microsoft.CodeAnalysis.Diagnostics;
using Xunit;
namespace CliFx.Analyzers.Tests
{
public class ConsoleUsageAnalyzerTests
{
private static DiagnosticAnalyzer Analyzer { get; } = new ConsoleUsageAnalyzer();
public static IEnumerable<object[]> GetValidCases()
{
yield return new object[]
{
new AnalyzerTestCase(
"Using console abstraction",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console)
{
console.Output.WriteLine(""Hello world"");
return default;
}
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Console abstraction is not available in scope",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
public void SomeOtherMethod() => Console.WriteLine(""Test"");
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
}
public static IEnumerable<object[]> GetInvalidCases()
{
yield return new object[]
{
new AnalyzerTestCase(
"Not using available console abstraction in the ExecuteAsync method",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console)
{
Console.WriteLine(""Hello world"");
return default;
}
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Not using available console abstraction in the ExecuteAsync method when writing stderr",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console)
{
Console.Error.WriteLine(""Hello world"");
return default;
}
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Not using available console abstraction while referencing System.Console by full name",
Analyzer.SupportedDiagnostics,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console)
{
System.Console.Error.WriteLine(""Hello world"");
return default;
}
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Not using available console abstraction in another method",
DiagnosticDescriptors.CliFx0100,
// language=cs
@"
[Command]
public class MyCommand : ICommand
{
public void SomeOtherMethod(IConsole console) => Console.WriteLine(""Test"");
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
}
[Theory]
[MemberData(nameof(GetValidCases))]
public void Valid(AnalyzerTestCase testCase) =>
Analyzer.Should().NotProduceDiagnostics(testCase);
[Theory]
[MemberData(nameof(GetInvalidCases))]
public void Invalid(AnalyzerTestCase testCase) =>
Analyzer.Should().ProduceDiagnostics(testCase);
}
}

View File

@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;
using Gu.Roslyn.Asserts;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
namespace CliFx.Analyzers.Tests.Internal
{
internal partial class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer, AnalyzerAssertions>
{
protected override string Identifier { get; } = "analyzer";
public AnalyzerAssertions(DiagnosticAnalyzer analyzer)
: base(analyzer)
{
}
public void ProduceDiagnostics(
IReadOnlyList<DiagnosticDescriptor> diagnostics,
IReadOnlyList<string> sourceCodes)
{
var producedDiagnostics = GetProducedDiagnostics(Subject, sourceCodes);
var expectedIds = diagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
var producedIds = producedDiagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
var result = expectedIds.Intersect(producedIds).Count() == expectedIds.Length;
Execute.Assertion.ForCondition(result).FailWith($@"
Expected and produced diagnostics do not match.
Expected: {string.Join(", ", expectedIds)}
Produced: {(producedIds.Any() ? string.Join(", ", producedIds) : "<none>")}
".Trim());
}
public void ProduceDiagnostics(AnalyzerTestCase testCase) =>
ProduceDiagnostics(testCase.TestedDiagnostics, testCase.SourceCodes);
public void NotProduceDiagnostics(
IReadOnlyList<DiagnosticDescriptor> diagnostics,
IReadOnlyList<string> sourceCodes)
{
var producedDiagnostics = GetProducedDiagnostics(Subject, sourceCodes);
var expectedIds = diagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
var producedIds = producedDiagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
var result = !expectedIds.Intersect(producedIds).Any();
Execute.Assertion.ForCondition(result).FailWith($@"
Expected no produced diagnostics.
Produced: {string.Join(", ", producedIds)}
".Trim());
}
public void NotProduceDiagnostics(AnalyzerTestCase testCase) =>
NotProduceDiagnostics(testCase.TestedDiagnostics, testCase.SourceCodes);
}
internal partial class AnalyzerAssertions
{
private static IReadOnlyList<MetadataReference> DefaultMetadataReferences { get; } =
MetadataReferences.Transitive(typeof(CliApplication).Assembly).ToArray();
private static string WrapCodeWithUsingDirectives(string code)
{
var usingDirectives = new[]
{
"using System;",
"using System.Collections.Generic;",
"using System.Threading.Tasks;",
"using CliFx;",
"using CliFx.Attributes;",
"using CliFx.Exceptions;",
"using CliFx.Utilities;"
};
return
string.Join(Environment.NewLine, usingDirectives) +
Environment.NewLine +
code;
}
private static IReadOnlyList<Diagnostic> GetProducedDiagnostics(
DiagnosticAnalyzer analyzer,
IReadOnlyList<string> sourceCodes)
{
var compilationOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication);
var wrappedSourceCodes = sourceCodes.Select(WrapCodeWithUsingDirectives).ToArray();
return Analyze.GetDiagnostics(analyzer, wrappedSourceCodes, compilationOptions, DefaultMetadataReferences)
.SelectMany(d => d)
.ToArray();
}
}
internal static class AnalyzerAssertionsExtensions
{
public static AnalyzerAssertions Should(this DiagnosticAnalyzer analyzer) => new AnalyzerAssertions(analyzer);
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../CliFx.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>annotations</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" PrivateAssets="all" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,297 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
namespace CliFx.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CommandSchemaAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
DiagnosticDescriptors.CliFx0001,
DiagnosticDescriptors.CliFx0002,
DiagnosticDescriptors.CliFx0021,
DiagnosticDescriptors.CliFx0022,
DiagnosticDescriptors.CliFx0023,
DiagnosticDescriptors.CliFx0024,
DiagnosticDescriptors.CliFx0041,
DiagnosticDescriptors.CliFx0042,
DiagnosticDescriptors.CliFx0043,
DiagnosticDescriptors.CliFx0044,
DiagnosticDescriptors.CliFx0045
);
private static bool IsScalarType(ITypeSymbol typeSymbol) =>
KnownSymbols.IsSystemString(typeSymbol) ||
!typeSymbol.AllInterfaces.Select(i => i.ConstructedFrom).Any(KnownSymbols.IsSystemCollectionsGenericIEnumerable);
private static void CheckCommandParameterProperties(
SymbolAnalysisContext context,
IReadOnlyList<IPropertySymbol> properties)
{
var parameters = properties
.Select(p =>
{
var attribute = p
.GetAttributes()
.First(a => KnownSymbols.IsCommandParameterAttribute(a.AttributeClass));
var order = attribute
.ConstructorArguments
.Select(a => a.Value)
.FirstOrDefault() as int?;
var name = attribute
.NamedArguments
.Where(a => a.Key == "Name")
.Select(a => a.Value.Value)
.FirstOrDefault() as string;
return new
{
Property = p,
Order = order,
Name = name
};
})
.ToArray();
// Duplicate order
var duplicateOrderParameters = parameters
.Where(p => p.Order != null)
.GroupBy(p => p.Order)
.Where(g => g.Count() > 1)
.SelectMany(g => g.AsEnumerable())
.ToArray();
foreach (var parameter in duplicateOrderParameters)
{
context.ReportDiagnostic(
Diagnostic.Create(DiagnosticDescriptors.CliFx0021, parameter.Property.Locations.First()));
}
// Duplicate name
var duplicateNameParameters = parameters
.Where(p => !string.IsNullOrWhiteSpace(p.Name))
.GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
.Where(g => g.Count() > 1)
.SelectMany(g => g.AsEnumerable())
.ToArray();
foreach (var parameter in duplicateNameParameters)
{
context.ReportDiagnostic(
Diagnostic.Create(DiagnosticDescriptors.CliFx0022, parameter.Property.Locations.First()));
}
// Multiple non-scalar
var nonScalarParameters = parameters
.Where(p => !IsScalarType(p.Property.Type))
.ToArray();
if (nonScalarParameters.Length > 1)
{
foreach (var parameter in nonScalarParameters)
{
context.ReportDiagnostic(
Diagnostic.Create(DiagnosticDescriptors.CliFx0023, parameter.Property.Locations.First()));
}
}
// Non-last non-scalar
var nonLastNonScalarParameter = parameters
.OrderByDescending(a => a.Order)
.Skip(1)
.LastOrDefault(p => !IsScalarType(p.Property.Type));
if (nonLastNonScalarParameter != null)
{
context.ReportDiagnostic(
Diagnostic.Create(DiagnosticDescriptors.CliFx0024, nonLastNonScalarParameter.Property.Locations.First()));
}
}
private static void CheckCommandOptionProperties(
SymbolAnalysisContext context,
IReadOnlyList<IPropertySymbol> properties)
{
var options = properties
.Select(p =>
{
var attribute = p
.GetAttributes()
.First(a => KnownSymbols.IsCommandOptionAttribute(a.AttributeClass));
var name = attribute
.ConstructorArguments
.Where(a => KnownSymbols.IsSystemString(a.Type))
.Select(a => a.Value)
.FirstOrDefault() as string;
var shortName = attribute
.ConstructorArguments
.Where(a => KnownSymbols.IsSystemChar(a.Type))
.Select(a => a.Value)
.FirstOrDefault() as char?;
var envVarName = attribute
.NamedArguments
.Where(a => a.Key == "EnvironmentVariableName")
.Select(a => a.Value.Value)
.FirstOrDefault() as string;
return new
{
Property = p,
Name = name,
ShortName = shortName,
EnvironmentVariableName = envVarName
};
})
.ToArray();
// No name
var noNameOptions = options
.Where(o => string.IsNullOrWhiteSpace(o.Name) && o.ShortName == null)
.ToArray();
foreach (var option in noNameOptions)
{
context.ReportDiagnostic(
Diagnostic.Create(DiagnosticDescriptors.CliFx0041, option.Property.Locations.First()));
}
// Too short name
var invalidNameLengthOptions = options
.Where(o => !string.IsNullOrWhiteSpace(o.Name) && o.Name.Length <= 1)
.ToArray();
foreach (var option in invalidNameLengthOptions)
{
context.ReportDiagnostic(
Diagnostic.Create(DiagnosticDescriptors.CliFx0042, option.Property.Locations.First()));
}
// Duplicate name
var duplicateNameOptions = options
.Where(p => !string.IsNullOrWhiteSpace(p.Name))
.GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
.Where(g => g.Count() > 1)
.SelectMany(g => g.AsEnumerable())
.ToArray();
foreach (var option in duplicateNameOptions)
{
context.ReportDiagnostic(
Diagnostic.Create(DiagnosticDescriptors.CliFx0043, option.Property.Locations.First()));
}
// Duplicate name
var duplicateShortNameOptions = options
.Where(p => p.ShortName != null)
.GroupBy(p => p.ShortName)
.Where(g => g.Count() > 1)
.SelectMany(g => g.AsEnumerable())
.ToArray();
foreach (var option in duplicateShortNameOptions)
{
context.ReportDiagnostic(
Diagnostic.Create(DiagnosticDescriptors.CliFx0044, option.Property.Locations.First()));
}
// Duplicate environment variable name
var duplicateEnvironmentVariableNameOptions = options
.Where(p => !string.IsNullOrWhiteSpace(p.EnvironmentVariableName))
.GroupBy(p => p.EnvironmentVariableName, StringComparer.OrdinalIgnoreCase)
.Where(g => g.Count() > 1)
.SelectMany(g => g.AsEnumerable())
.ToArray();
foreach (var option in duplicateEnvironmentVariableNameOptions)
{
context.ReportDiagnostic(
Diagnostic.Create(DiagnosticDescriptors.CliFx0045, option.Property.Locations.First()));
}
}
private static void CheckCommandType(SymbolAnalysisContext context)
{
// Named type: MyCommand
if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol))
return;
// Only classes
if (namedTypeSymbol.TypeKind != TypeKind.Class)
return;
// Implements ICommand?
var implementsCommandInterface = namedTypeSymbol
.AllInterfaces
.Any(KnownSymbols.IsCommandInterface);
// Has CommandAttribute?
var hasCommandAttribute = namedTypeSymbol
.GetAttributes()
.Select(a => a.AttributeClass)
.Any(KnownSymbols.IsCommandAttribute);
var isValidCommandType =
// implements interface
implementsCommandInterface && (
// and either abstract class or has attribute
namedTypeSymbol.IsAbstract || hasCommandAttribute
);
if (!isValidCommandType)
{
// See if this was meant to be a command type (either interface or attribute present)
var isAlmostValidCommandType = implementsCommandInterface ^ hasCommandAttribute;
if (isAlmostValidCommandType && !implementsCommandInterface)
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0001, namedTypeSymbol.Locations.First()));
if (isAlmostValidCommandType && !hasCommandAttribute)
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0002, namedTypeSymbol.Locations.First()));
return;
}
var properties = namedTypeSymbol
.GetMembers()
.Where(m => m.Kind == SymbolKind.Property)
.OfType<IPropertySymbol>().ToArray();
// Check parameters
var parameterProperties = properties
.Where(p => p
.GetAttributes()
.Select(a => a.AttributeClass)
.Any(KnownSymbols.IsCommandParameterAttribute))
.ToArray();
CheckCommandParameterProperties(context, parameterProperties);
// Check options
var optionsProperties = properties
.Where(p => p
.GetAttributes()
.Select(a => a.AttributeClass)
.Any(KnownSymbols.IsCommandOptionAttribute))
.ToArray();
CheckCommandOptionProperties(context, optionsProperties);
}
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterSymbolAction(CheckCommandType, SymbolKind.NamedType);
}
}
}

View File

@@ -0,0 +1,80 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace CliFx.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ConsoleUsageAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
DiagnosticDescriptors.CliFx0100
);
private static bool IsSystemConsoleInvocation(
SyntaxNodeAnalysisContext context,
InvocationExpressionSyntax invocationSyntax)
{
// Get the method member access (Console.WriteLine or Console.Error.WriteLine)
if (!(invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax))
return false;
// Get the semantic model for the invoked method
if (!(context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol))
return false;
// Check if contained within System.Console
if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType))
return true;
// In case with Console.Error.WriteLine that wouldn't work, we need to check parent member access too
if (!(memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax))
return false;
// Get the semantic model for the parent member
if (!(context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol))
return false;
// Check if contained within System.Console
if (KnownSymbols.IsSystemConsole(propertySymbol.ContainingType))
return true;
return false;
}
private static void CheckSystemConsoleUsage(SyntaxNodeAnalysisContext context)
{
if (!(context.Node is InvocationExpressionSyntax invocationSyntax))
return;
if (!IsSystemConsoleInvocation(context, invocationSyntax))
return;
// Check if IConsole is available in the scope as a viable alternative
var isConsoleInterfaceAvailable = invocationSyntax
.Ancestors()
.OfType<MethodDeclarationSyntax>()
.SelectMany(m => m.ParameterList.Parameters)
.Select(p => p.Type)
.Select(t => context.SemanticModel.GetSymbolInfo(t).Symbol)
.Where(s => s != null)
.Any(KnownSymbols.IsConsoleInterface!);
if (!isConsoleInterfaceAvailable)
return;
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0100, invocationSyntax.GetLocation()));
}
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterSyntaxNodeAction(CheckSystemConsoleUsage, SyntaxKind.InvocationExpression);
}
}
}

View File

@@ -0,0 +1,79 @@
using Microsoft.CodeAnalysis;
namespace CliFx.Analyzers
{
public static class DiagnosticDescriptors
{
public static readonly DiagnosticDescriptor CliFx0001 =
new DiagnosticDescriptor(nameof(CliFx0001),
"Type must implement the 'CliFx.ICommand' interface in order to be a valid command",
"Type must implement the 'CliFx.ICommand' interface in order to be a valid command",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0002 =
new DiagnosticDescriptor(nameof(CliFx0002),
"Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command",
"Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0021 =
new DiagnosticDescriptor(nameof(CliFx0021),
"Parameter order must be unique within its command",
"Parameter order must be unique within its command",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0022 =
new DiagnosticDescriptor(nameof(CliFx0022),
"Parameter order must have unique name within its command",
"Parameter order must have unique name within its command",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0023 =
new DiagnosticDescriptor(nameof(CliFx0023),
"Only one non-scalar parameter per command is allowed",
"Only one non-scalar parameter per command is allowed",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0024 =
new DiagnosticDescriptor(nameof(CliFx0024),
"Non-scalar parameter must be last in order",
"Non-scalar parameter must be last in order",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0041 =
new DiagnosticDescriptor(nameof(CliFx0041),
"Option must have a name or short name specified",
"Option must have a name or short name specified",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0042 =
new DiagnosticDescriptor(nameof(CliFx0042),
"Option name must be at least 2 characters long",
"Option name must be at least 2 characters long",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0043 =
new DiagnosticDescriptor(nameof(CliFx0043),
"Option name must be unique within its command",
"Option name must be unique within its command",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0044 =
new DiagnosticDescriptor(nameof(CliFx0044),
"Option short name must be unique within its command",
"Option short name must be unique within its command",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0045 =
new DiagnosticDescriptor(nameof(CliFx0045),
"Option environment variable name must be unique within its command",
"Option environment variable name must be unique within its command",
"Usage", DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor CliFx0100 =
new DiagnosticDescriptor(nameof(CliFx0100),
"Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation",
"Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation",
"Usage", DiagnosticSeverity.Warning, true);
}
}

View File

@@ -0,0 +1,11 @@
using System;
using Microsoft.CodeAnalysis;
namespace CliFx.Analyzers.Internal
{
internal static class RoslynExtensions
{
public static bool DisplayNameMatches(this ISymbol symbol, string name) =>
string.Equals(symbol.ToDisplayString(), name, StringComparison.Ordinal);
}
}

View File

@@ -0,0 +1,37 @@
using CliFx.Analyzers.Internal;
using Microsoft.CodeAnalysis;
namespace CliFx.Analyzers
{
public static class KnownSymbols
{
public static bool IsSystemString(ISymbol symbol) =>
symbol.DisplayNameMatches("string") ||
symbol.DisplayNameMatches("System.String");
public static bool IsSystemChar(ISymbol symbol) =>
symbol.DisplayNameMatches("char") ||
symbol.DisplayNameMatches("System.Char");
public static bool IsSystemCollectionsGenericIEnumerable(ISymbol symbol) =>
symbol.DisplayNameMatches("System.Collections.Generic.IEnumerable<T>");
public static bool IsSystemConsole(ISymbol symbol) =>
symbol.DisplayNameMatches("System.Console");
public static bool IsConsoleInterface(ISymbol symbol) =>
symbol.DisplayNameMatches("CliFx.IConsole");
public static bool IsCommandInterface(ISymbol symbol) =>
symbol.DisplayNameMatches("CliFx.ICommand");
public static bool IsCommandAttribute(ISymbol symbol) =>
symbol.DisplayNameMatches("CliFx.Attributes.CommandAttribute");
public static bool IsCommandParameterAttribute(ISymbol symbol) =>
symbol.DisplayNameMatches("CliFx.Attributes.CommandParameterAttribute");
public static bool IsCommandOptionAttribute(ISymbol symbol) =>
symbol.DisplayNameMatches("CliFx.Attributes.CommandOptionAttribute");
}
}

View File

@@ -7,12 +7,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CliFx\CliFx.csproj" />
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
</ItemGroup>
</Project>

View File

@@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="..\CliFx\CliFx.csproj" />
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,20 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using System.Threading.Tasks;
namespace CliFx.Tests.Dummy.Commands
{
/// <summary>
/// Demos how to show an error message then help text from an organizational command.
/// </summary>
[Command("cmd-err", Description = "This is an organizational command. " +
"I don't do anything except provide a route to my subcommands. " +
"If you use just me, I print an error message then the help text " +
"to remind you of my subcommands.")]
public class ShowErrorMessageThenHelpTextOnCommandExceptionCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) =>
throw new CommandException("It is an error to use me without a subcommand. " +
"Please refer to the help text below for guidance.", showHelp: true);
}
}

View File

@@ -0,0 +1,18 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using System.Threading.Tasks;
namespace CliFx.Tests.Dummy.Commands
{
/// <summary>
/// Demos how to show help text from an organizational command.
/// </summary>
[Command("cmd", Description = "This is an organizational command. " +
"I don't do anything except provide a route to my subcommands. " +
"If you use just me, I print the help text to remind you of my subcommands.")]
public class ShowHelpTextOnCommandExceptionCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) =>
throw new CommandException(null, showHelp: false);
}
}

View File

@@ -76,6 +76,24 @@ namespace CliFx.Tests
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class EmptyOptionNameCommand : ICommand
{
[CommandOption("")]
public string? Apples { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class SingleCharacterOptionNameCommand : ICommand
{
[CommandOption("a")]
public string? Apples { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class DuplicateOptionNamesCommand : ICommand
{

View File

@@ -4,11 +4,16 @@ using CliFx.Domain;
using CliFx.Exceptions;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class ApplicationSpecs
{
private readonly ITestOutputHelper _output;
public ApplicationSpecs(ITestOutputHelper output) => _output = output;
[Fact]
public void Application_can_be_created_with_a_default_configuration()
{
@@ -38,7 +43,7 @@ namespace CliFx.Tests
.UseVersionText("test")
.UseDescription("test")
.UseConsole(new VirtualConsole(Stream.Null))
.UseTypeActivator(Activator.CreateInstance)
.UseTypeActivator(Activator.CreateInstance!)
.Build();
// Assert
@@ -52,7 +57,8 @@ namespace CliFx.Tests
var commandTypes = Array.Empty<Type>();
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -62,7 +68,8 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(NonImplementedCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -72,7 +79,8 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -82,7 +90,8 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -92,7 +101,8 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -102,7 +112,8 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -112,7 +123,8 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -122,7 +134,30 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
public void Command_options_must_have_names_that_are_not_empty()
{
// Arrange
var commandTypes = new[] {typeof(EmptyOptionNameCommand)};
// Act & assert
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
public void Command_options_must_have_names_that_are_longer_than_one_character()
{
// Arrange
var commandTypes = new[] {typeof(SingleCharacterOptionNameCommand)};
// Act & assert
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -132,7 +167,8 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -142,7 +178,8 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -152,7 +189,8 @@ namespace CliFx.Tests
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -174,7 +212,7 @@ namespace CliFx.Tests
new[]
{
new CommandParameterSchema(
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter)),
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter))!,
13,
"param",
"Param description")
@@ -182,7 +220,7 @@ namespace CliFx.Tests
new[]
{
new CommandOptionSchema(
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option)),
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option))!,
"option",
'o',
"ENV",

View File

@@ -17,20 +17,14 @@ namespace CliFx.Tests
{
public string Value { get; }
public StringConstructable(string value)
{
Value = value;
}
public StringConstructable(string value) => Value = value;
}
private class StringParseable
{
public string Value { get; }
private StringParseable(string value)
{
Value = value;
}
private StringParseable(string value) => Value = value;
public static StringParseable Parse(string value) => new StringParseable(value);
}
@@ -39,10 +33,7 @@ namespace CliFx.Tests
{
public string Value { get; }
private StringParseableWithFormatProvider(string value)
{
Value = value;
}
private StringParseableWithFormatProvider(string value) => Value = value;
public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
new StringParseableWithFormatProvider(value + " " + formatProvider);
@@ -54,9 +45,7 @@ namespace CliFx.Tests
public class CustomEnumerable<T> : IEnumerable<T>
{
private readonly T[] _arr = new T[0];
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) _arr).GetEnumerator();
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

View File

@@ -5,11 +5,16 @@ using CliFx.Domain;
using CliFx.Exceptions;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class ArgumentBindingSpecs
{
private readonly ITestOutputHelper _output;
public ArgumentBindingSpecs(ITestOutputHelper output) => _output = output;
[Fact]
public void Property_of_type_object_is_bound_directly_from_the_argument_value()
{
@@ -933,7 +938,7 @@ namespace CliFx.Tests
}
[Fact]
public void Property_annotated_as_a_required_option_must_always_be_bound_to_some_value()
public void Property_annotated_as_a_required_option_must_always_be_set()
{
// Arrange
var schema = ApplicationSchema.Resolve(new[] {typeof(RequiredOptionCommand)});
@@ -943,7 +948,23 @@ namespace CliFx.Tests
.Build();
// Act & assert
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
_output.WriteLine(ex.Message);
}
[Fact]
public void Property_annotated_as_a_required_option_must_always_be_bound_to_some_value()
{
// Arrange
var schema = ApplicationSchema.Resolve(new[] {typeof(RequiredOptionCommand)});
var input = new CommandLineInputBuilder()
.AddOption(nameof(RequiredOptionCommand.OptionB))
.Build();
// Act & assert
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -982,7 +1003,8 @@ namespace CliFx.Tests
.Build();
// Act & assert
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -996,7 +1018,8 @@ namespace CliFx.Tests
.Build();
// Act & assert
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -1010,7 +1033,8 @@ namespace CliFx.Tests
.Build();
// Act & assert
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -1024,7 +1048,8 @@ namespace CliFx.Tests
.Build();
// Act & assert
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -1038,7 +1063,8 @@ namespace CliFx.Tests
.Build();
// Act & assert
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -1053,7 +1079,8 @@ namespace CliFx.Tests
.Build();
// Act & assert
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -1070,7 +1097,8 @@ namespace CliFx.Tests
.Build();
// Act & assert
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
_output.WriteLine(ex.Message);
}
}
}

View File

@@ -17,10 +17,11 @@
<ItemGroup>
<PackageReference Include="CliWrap" Version="3.0.0" />
<PackageReference Include="FluentAssertions" Version="5.10.2" />
<PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,11 +1,16 @@
using CliFx.Exceptions;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class DependencyInjectionSpecs
{
private readonly ITestOutputHelper _output;
public DependencyInjectionSpecs(ITestOutputHelper output) => _output = output;
[Fact]
public void Default_type_activator_can_initialize_a_command_if_it_has_a_parameterless_constructor()
{
@@ -26,8 +31,8 @@ namespace CliFx.Tests
var activator = new DefaultTypeActivator();
// Act & assert
Assert.Throws<CliFxException>(() =>
activator.CreateInstance(typeof(WithDependenciesCommand)));
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
_output.WriteLine(ex.Message);
}
[Fact]
@@ -48,11 +53,11 @@ namespace CliFx.Tests
public void Delegate_type_activator_throws_if_the_underlying_function_returns_null()
{
// Arrange
var activator = new DelegateTypeActivator(_ => null);
var activator = new DelegateTypeActivator(_ => null!);
// Act & assert
Assert.Throws<CliFxException>(() =>
activator.CreateInstance(typeof(WithDependenciesCommand)));
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
_output.WriteLine(ex.Message);
}
}
}

View File

@@ -27,5 +27,51 @@ namespace CliFx.Tests
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
}
[Command("exc")]
private class ShowHelpTextOnlyCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(null, showHelp: true);
}
[Command("exc sub")]
private class ShowHelpTextOnlySubCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("exc")]
private class ShowErrorMessageThenHelpTextCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) =>
throw new CommandException("Error message.", showHelp: true);
}
[Command("exc sub")]
private class ShowErrorMessageThenHelpTextSubCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("exc")]
private class StackTraceOnlyCommand : ICommand
{
[CommandOption("msg", 'm')]
public string? Message { get; set; }
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(null);
}
[Command("inv")]
private class InvalidUserInputCommand : ICommand
{
[CommandOption("required", 'r')]
public string? RequiredOption { get; }
public ValueTask ExecuteAsync(IConsole console)
{
throw new NotImplementedException();
}
}
}
}

View File

@@ -3,13 +3,18 @@ using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class ErrorReportingSpecs
{
private readonly ITestOutputHelper _output;
public ErrorReportingSpecs(ITestOutputHelper output) => _output = output;
[Fact]
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_full_error_details()
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_error_message_and_stack_trace()
{
// Arrange
await using var stdErr = new MemoryStream();
@@ -29,8 +34,12 @@ namespace CliFx.Tests
// Assert
exitCode.Should().NotBe(0);
stdErrData.Should().Contain("Kaput");
stdErrData.Length.Should().BeGreaterThan("Kaput".Length);
stdErrData.Should().ContainAll(
"System.Exception:",
"Kaput", "at",
"CliFx.Tests");
_output.WriteLine(stdErrData);
}
[Fact]
@@ -55,6 +64,8 @@ namespace CliFx.Tests
// Assert
exitCode.Should().Be(69);
stdErrData.Should().Be("Kaput");
_output.WriteLine(stdErrData);
}
[Fact]
@@ -79,6 +90,149 @@ namespace CliFx.Tests
// Assert
exitCode.Should().NotBe(0);
stdErrData.Should().NotBeEmpty();
_output.WriteLine(stdErrData);
}
[Fact]
public async Task Command_may_throw_a_specialized_exception_which_shows_only_the_help_text()
{
// Arrange
await using var stdOut = new MemoryStream();
await using var stdErr = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var application = new CliApplicationBuilder()
.AddCommand(typeof(ShowHelpTextOnlyCommand))
.AddCommand(typeof(ShowHelpTextOnlySubCommand))
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] {"exc"});
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
var stdErrData = console.Output.Encoding.GetString(stdErr.ToArray()).TrimEnd();
// Assert
stdErrData.Should().BeEmpty();
stdOutData.Should().ContainAll(
"Usage",
"[command]",
"Options",
"-h|--help", "Shows help text.",
"Commands",
"sub",
"You can run", "to show help on a specific command."
);
_output.WriteLine(stdOutData);
_output.WriteLine(stdErrData);
}
[Fact]
public async Task Command_may_throw_specialized_exception_which_shows_the_error_message_then_the_help_text()
{
// Arrange
await using var stdOut = new MemoryStream();
await using var stdErr = new MemoryStream();
var console = new VirtualConsole(output: stdOut, error: stdErr);
var application = new CliApplicationBuilder()
.AddCommand(typeof(ShowErrorMessageThenHelpTextCommand))
.AddCommand(typeof(ShowErrorMessageThenHelpTextSubCommand))
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] {"exc"});
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
// Assert
stdErrData.Should().Be("Error message.");
stdOutData.Should().ContainAll(
"Usage",
"[command]",
"Options",
"-h|--help", "Shows help text.",
"Commands",
"sub",
"You can run", "to show help on a specific command."
);
_output.WriteLine(stdOutData);
_output.WriteLine(stdErrData);
}
[Fact]
public async Task Command_may_throw_a_specialized_exception_which_shows_only_a_stack_trace_and_no_help_text()
{
// Arrange
await using var stdErr = new MemoryStream();
var console = new VirtualConsole(error: stdErr);
var application = new CliApplicationBuilder()
.AddCommand(typeof(GenericExceptionCommand))
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(
new[] {"exc", "-m", "Kaput"},
new Dictionary<string, string>());
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
// Assert
exitCode.Should().NotBe(0);
stdErrData.Should().ContainAll(
"System.Exception:",
"Kaput", "at",
"CliFx.Tests");
_output.WriteLine(stdErrData);
}
[Fact]
public async Task Command_shows_help_text_on_exceptions_related_to_invalid_user_input()
{
// Arrange
await using var stdOut = new MemoryStream();
await using var stdErr = new MemoryStream();
var console = new VirtualConsole(output: stdOut, error: stdErr);
var application = new CliApplicationBuilder()
.AddCommand(typeof(InvalidUserInputCommand))
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(
new[] {"not-a-valid-command", "-r", "foo"},
new Dictionary<string, string>());
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
// Assert
exitCode.Should().NotBe(0);
stdErrData.Should().ContainAll(
"Can't find a command that matches the following arguments:",
"not-a-valid-command"
);
stdOutData.Should().ContainAll(
"Usage",
"[command]",
"Options",
"-h|--help", "Shows help text.",
"Commands",
"inv",
"You can run", "to show help on a specific command."
);
_output.WriteLine(stdOutData);
_output.WriteLine(stdErrData);
}
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Exceptions;
namespace CliFx.Tests
{
@@ -81,6 +82,23 @@ namespace CliFx.Tests
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("cmd-with-enum-args")]
private class EnumArgumentsCommand : ICommand
{
public enum TestEnum { Value1, Value2, Value3 };
[CommandParameter(0, Name = "value", Description = "Enum parameter.")]
public TestEnum ParamA { get; set; }
[CommandOption("value", Description = "Enum option.", IsRequired = true)]
public TestEnum OptionA { get; set; } = TestEnum.Value1;
[CommandOption("nullable-value", Description = "Nullable enum option.")]
public TestEnum? OptionB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("cmd-with-env-vars")]
private class EnvironmentVariableCommand : ICommand
{

View File

@@ -2,11 +2,16 @@
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class HelpTextSpecs
{
private readonly ITestOutputHelper _output;
public HelpTextSpecs(ITestOutputHelper output) => _output = output;
[Fact]
public async Task Version_information_can_be_requested_by_providing_the_version_option_without_other_arguments()
{
@@ -29,6 +34,8 @@ namespace CliFx.Tests
// Assert
exitCode.Should().Be(0);
stdOutData.Should().Be("v6.9");
_output.WriteLine(stdOutData);
}
[Fact]
@@ -68,6 +75,8 @@ namespace CliFx.Tests
"cmd", "NamedCommand description.",
"You can run", "to show help on a specific command."
);
_output.WriteLine(stdOutData);
}
[Fact]
@@ -104,6 +113,8 @@ namespace CliFx.Tests
"sub", "SubCommand description.",
"You can run", "to show help on a specific command."
);
_output.WriteLine(stdOutData);
}
[Fact]
@@ -137,6 +148,8 @@ namespace CliFx.Tests
"-e|--option-e", "OptionE description.",
"-h|--help", "Shows help text."
);
_output.WriteLine(stdOutData);
}
[Fact]
@@ -167,6 +180,8 @@ namespace CliFx.Tests
"cmd", "NamedCommand description.",
"You can run", "to show help on a specific command."
);
_output.WriteLine(stdOutData);
}
[Fact]
@@ -190,6 +205,8 @@ namespace CliFx.Tests
"Usage",
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
);
_output.WriteLine(stdOutData);
}
[Fact]
@@ -217,6 +234,38 @@ namespace CliFx.Tests
"* -g|--option-g",
"-h|--option-h"
);
_output.WriteLine(stdOutData);
}
[Fact]
public async Task Help_text_shows_usage_format_which_lists_all_valid_values_for_enum_arguments()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var application = new CliApplicationBuilder()
.AddCommand(typeof(EnumArgumentsCommand))
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] { "cmd-with-enum-args", "--help" });
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
// Assert
stdOutData.Should().ContainAll(
"Usage",
"cmd-with-enum-args", "[options]",
"Parameters",
"value", "Valid values: Value1, Value2, Value3.",
"Options",
"* --value", "Enum option.", "Valid values: Value1, Value2, Value3.",
"--nullable-value", "Nullable enum option.", "Valid values: Value1, Value2, Value3."
);
_output.WriteLine(stdOutData);
}
[Fact]
@@ -241,6 +290,8 @@ namespace CliFx.Tests
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
);
_output.WriteLine(stdOutData);
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx.Attributes;
@@ -20,7 +21,7 @@ namespace CliFx.Tests
private class ConcatCommand : ICommand
{
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
public IReadOnlyList<string> Inputs { get; set; }
public IReadOnlyList<string> Inputs { get; set; } = Array.Empty<string>();
[CommandOption('s', Description = "String separator.")]
public string Separator { get; set; } = "";
@@ -36,10 +37,10 @@ namespace CliFx.Tests
private class DivideCommand : ICommand
{
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
public double Dividend { get; set; }
public double Dividend { get; set; } = 0;
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
public double Divisor { get; set; }
public double Divisor { get; set; } = 0;
public ValueTask ExecuteAsync(IConsole console)
{

View File

@@ -3,11 +3,16 @@ using System.Linq;
using CliFx.Utilities;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public class UtilitiesSpecs
{
private readonly ITestOutputHelper _output;
public UtilitiesSpecs(ITestOutputHelper output) => _output = output;
[Fact]
public void Progress_ticker_can_be_used_to_report_progress_to_console()
{
@@ -28,6 +33,8 @@ namespace CliFx.Tests
// Assert
stdOutData.Should().ContainAll(progressStringValues);
_output.WriteLine(stdOutData);
}
[Fact]
@@ -49,6 +56,8 @@ namespace CliFx.Tests
// Assert
stdOutData.Should().BeEmpty();
_output.WriteLine(stdOutData);
}
}
}

View File

@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Version>1.1</Version>
<Version>1.2</Version>
<Company>Tyrrrz</Company>
<Copyright>Copyright (C) Alexey Golub</Copyright>
<LangVersion>latest</LangVersion>

View File

@@ -10,16 +10,20 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3AAE8166-BB8E-49DA-844C-3A0EE6BD40A0}"
ProjectSection(SolutionItems) = preProject
Changelog.md = Changelog.md
CliFx.props = CliFx.props
License.txt = License.txt
Readme.md = Readme.md
CliFx.props = CliFx.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Benchmarks", "CliFx.Benchmarks\CliFx.Benchmarks.csproj", "{8ACD6DC2-D768-4850-9223-5B7C83A78513}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Demo", "CliFx.Demo\CliFx.Demo.csproj", "{AAB6844C-BF71-448F-A11B-89AEE459AB15}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliFx.Tests.Dummy", "CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj", "{F717347D-8656-44DA-A4A2-BE515E8C4655}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Tests.Dummy", "CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj", "{F717347D-8656-44DA-A4A2-BE515E8C4655}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Analyzers", "CliFx.Analyzers\CliFx.Analyzers.csproj", "{F8460D69-F8CF-405C-A6ED-BED02A21DB42}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliFx.Analyzers.Tests", "CliFx.Analyzers.Tests\CliFx.Analyzers.Tests.csproj", "{49878E75-2097-4C79-9151-B98A28FBB973}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -91,6 +95,30 @@ Global
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x64.Build.0 = Release|Any CPU
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x86.ActiveCfg = Release|Any CPU
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x86.Build.0 = Release|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x64.ActiveCfg = Debug|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x64.Build.0 = Debug|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x86.ActiveCfg = Debug|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x86.Build.0 = Debug|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|Any CPU.Build.0 = Release|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x64.ActiveCfg = Release|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x64.Build.0 = Release|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x86.ActiveCfg = Release|Any CPU
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x86.Build.0 = Release|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|Any CPU.Build.0 = Debug|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x64.ActiveCfg = Debug|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x64.Build.0 = Debug|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x86.ActiveCfg = Debug|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x86.Build.0 = Debug|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|Any CPU.ActiveCfg = Release|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|Any CPU.Build.0 = Release|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x64.ActiveCfg = Release|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x64.Build.0 = Release|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x86.ActiveCfg = Release|Any CPU
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -9,14 +9,14 @@ namespace CliFx.Attributes
public class CommandOptionAttribute : Attribute
{
/// <summary>
/// Option name.
/// Option name (must be longer than a single character).
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
/// All options in a command must have different names (comparison is not case-sensitive).
/// </summary>
public string? Name { get; }
/// <summary>
/// Option short name.
/// Option short name (single character).
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
/// All options in a command must have different short names (comparison is case-sensitive).
/// </summary>

View File

@@ -143,6 +143,32 @@ namespace CliFx
return 0;
}
/// <summary>
/// Handle <see cref="CommandException"/>s differently from the rest because we want to
/// display it different based on whether we are showing the help text or not.
/// </summary>
private int HandleCliFxException(IReadOnlyList<string> commandLineArguments, CliFxException cfe)
{
var showHelp = cfe.ShowHelp;
var errorMessage = cfe.HasMessage
? cfe.Message
: cfe.ToString();
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(errorMessage));
if (showHelp)
{
var applicationSchema = ApplicationSchema.Resolve(_configuration.CommandTypes);
var commandLineInput = CommandLineInput.Parse(commandLineArguments);
var commandSchema = applicationSchema.TryFindCommand(commandLineInput) ??
CommandSchema.StubDefaultCommand;
_helpTextWriter.Write(applicationSchema, commandSchema);
}
return cfe.ExitCode;
}
/// <summary>
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
/// </summary>
@@ -162,21 +188,18 @@ namespace CliFx
HandleHelpOption(applicationSchema, commandLineInput) ??
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
}
catch (Exception ex)
catch (CliFxException cfe)
{
// We want to catch exceptions in order to print errors and return correct exit codes.
// Doing this also gets rid of the annoying Windows troubleshooting dialog that shows up on unhandled exceptions.
// Prefer showing message without stack trace on exceptions coming from CliFx or on CommandException
var errorMessage = !string.IsNullOrWhiteSpace(ex.Message) && (ex is CliFxException || ex is CommandException)
? ex.Message
: ex.ToString();
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(errorMessage));
return ex is CommandException commandException
? commandException.ExitCode
: ex.HResult;
var exitCode = HandleCliFxException(commandLineArguments, cfe);
return exitCode;
}
catch (Exception ex)
{
// For all other errors, we just write the entire thing to stderr.
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(ex.ToString()));
return ex.HResult;
}
}

View File

@@ -173,10 +173,10 @@ namespace CliFx
public partial class CliApplicationBuilder
{
private static readonly Lazy<Assembly> LazyEntryAssembly = new Lazy<Assembly>(Assembly.GetEntryAssembly);
private static readonly Lazy<Assembly?> LazyEntryAssembly = new Lazy<Assembly?>(Assembly.GetEntryAssembly);
// Entry assembly is null in tests
private static Assembly EntryAssembly => LazyEntryAssembly.Value;
private static Assembly? EntryAssembly => LazyEntryAssembly.Value;
private static string? GetDefaultTitle() => EntryAssembly?.GetName().Name;
@@ -184,14 +184,12 @@ namespace CliFx
{
var entryAssemblyLocation = EntryAssembly?.Location;
// If it's a .dll assembly, prepend 'dotnet' and keep the file extension
if (string.Equals(Path.GetExtension(entryAssemblyLocation), ".dll", StringComparison.OrdinalIgnoreCase))
{
return "dotnet " + Path.GetFileName(entryAssemblyLocation);
}
// The assembly can be an executable or a dll, depending on how it was packaged
var isDll = string.Equals(Path.GetExtension(entryAssemblyLocation), ".dll", StringComparison.OrdinalIgnoreCase);
// Otherwise just use assembly file name without extension
return Path.GetFileNameWithoutExtension(entryAssemblyLocation);
return isDll
? "dotnet " + Path.GetFileName(entryAssemblyLocation)
: Path.GetFileNameWithoutExtension(entryAssemblyLocation);
}
private static string? GetDefaultVersionText() =>

View File

@@ -2,7 +2,7 @@
<Import Project="../CliFx.props" />
<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net45</TargetFrameworks>
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
<Authors>$(Company)</Authors>
<Description>Declarative framework for CLI applications</Description>
<PackageTags>command line executable interface framework parser arguments net core</PackageTags>
@@ -10,12 +10,17 @@
<PackageReleaseNotes>https://github.com/Tyrrrz/CliFx/blob/master/Changelog.md</PackageReleaseNotes>
<PackageIcon>favicon.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PublishRepositoryUrl>True</PublishRepositoryUrl>
<EmbedUntrackedSources>True</EmbedUntrackedSources>
<IncludeSymbols>True</IncludeSymbols>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);CopyAnalyzerToPackage</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>
<!-- Disable nullability warnings on older frameworks because there is no nullability info for BCL -->
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Nullable>annotations</Nullable>
</PropertyGroup>
<ItemGroup>
@@ -27,18 +32,29 @@
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<None Include="../favicon.png" Pack="True" PackagePath="" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="Nullable" Version="1.2.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net45'">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" />
</ItemGroup>
<ItemGroup>
<None Include="../favicon.png" Pack="true" PackagePath="" />
</ItemGroup>
<!-- The following item group and target ensure that the analyzer project is copied into the output NuGet package -->
<ItemGroup>
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="true" IncludeAssets="CliFx.Analyzers.dll" />
</ItemGroup>
<Target Name="CopyAnalyzerToPackage">
<ItemGroup>
<TfmSpecificPackageFile Include="$(OutDir)/CliFx.Analyzers.dll" PackagePath="analyzers/dotnet/cs" BuildAction="none" />
</ItemGroup>
</Target>
</Project>

View File

@@ -18,11 +18,7 @@ namespace CliFx
}
catch (Exception ex)
{
throw new CliFxException(new StringBuilder()
.Append($"Failed to create an instance of {type.FullName}.").Append(" ")
.AppendLine("The type must have a public parameter-less constructor in order to be instantiated by the default activator.")
.Append($"To supply a custom activator (for example when using dependency injection), call {nameof(CliApplicationBuilder)}.{nameof(CliApplicationBuilder.UseTypeActivator)}(...).")
.ToString(), ex);
throw CliFxException.DefaultActivatorFailed(type, ex);
}
}
}

View File

@@ -18,10 +18,6 @@ namespace CliFx
/// <inheritdoc />
public object CreateInstance(Type type) =>
_func(type) ?? throw new CliFxException(new StringBuilder()
.Append($"Failed to create an instance of type {type.FullName}, received <null> instead.").Append(" ")
.Append("Make sure that the provided type activator was configured correctly.").Append(" ")
.Append("If you are using a dependency container, make sure that this type is registered.")
.ToString());
_func(type) ?? throw CliFxException.DelegateActivatorReceivedNull(type);
}
}

View File

@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Internal;
namespace CliFx.Domain
{
@@ -71,18 +68,14 @@ namespace CliFx.Domain
IReadOnlyDictionary<string, string> environmentVariables,
ITypeActivator activator)
{
var command = TryFindCommand(commandLineInput, out var argumentOffset);
if (command == null)
{
throw new CliFxException(
$"Can't find a command that matches arguments [{string.Join(" ", commandLineInput.UnboundArguments)}].");
}
var command = TryFindCommand(commandLineInput, out var argumentOffset) ??
throw CliFxException.CannotFindMatchingCommand(commandLineInput);
var parameterValues = argumentOffset == 0
? commandLineInput.UnboundArguments.Select(a => a.Value).ToArray()
: commandLineInput.UnboundArguments.Skip(argumentOffset).Select(a => a.Value).ToArray();
var parameterInputs = argumentOffset == 0
? commandLineInput.UnboundArguments.ToArray()
: commandLineInput.UnboundArguments.Skip(argumentOffset).ToArray();
return command.CreateInstance(parameterValues, commandLineInput.Options, environmentVariables, activator);
return command.CreateInstance(parameterInputs, commandLineInput.Options, environmentVariables, activator);
}
public ICommand InitializeEntryPoint(
@@ -106,28 +99,23 @@ namespace CliFx.Domain
if (duplicateOrderGroup != null)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Command {command.Type.FullName} contains two or more parameters that have the same order ({duplicateOrderGroup.Key}):")
.AppendBulletList(duplicateOrderGroup.Select(o => o.Property.Name))
.AppendLine()
.Append("Parameters in a command must all have unique order.")
.ToString());
throw CliFxException.CommandParametersDuplicateOrder(
command,
duplicateOrderGroup.Key,
duplicateOrderGroup.ToArray());
}
var duplicateNameGroup = command.Parameters
.Where(a => !string.IsNullOrWhiteSpace(a.Name))
.GroupBy(a => a.Name, StringComparer.OrdinalIgnoreCase)
.GroupBy(a => a.Name!, StringComparer.OrdinalIgnoreCase)
.FirstOrDefault(g => g.Count() > 1);
if (duplicateNameGroup != null)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Command {command.Type.FullName} contains two or more parameters that have the same name ({duplicateNameGroup.Key}):")
.AppendBulletList(duplicateNameGroup.Select(o => o.Property.Name))
.AppendLine()
.Append("Parameters in a command must all have unique names.").Append(" ")
.Append("Comparison is NOT case-sensitive.")
.ToString());
throw CliFxException.CommandParametersDuplicateName(
command,
duplicateNameGroup.Key,
duplicateNameGroup.ToArray());
}
var nonScalarParameters = command.Parameters
@@ -136,13 +124,9 @@ namespace CliFx.Domain
if (nonScalarParameters.Length > 1)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Command [{command.Type.FullName}] contains two or more parameters of an enumerable type:")
.AppendBulletList(nonScalarParameters.Select(o => o.Property.Name))
.AppendLine()
.AppendLine("There can only be one parameter of an enumerable type in a command.")
.Append("Note, the string type is not considered enumerable in this context.")
.ToString());
throw CliFxException.CommandParametersTooManyNonScalar(
command,
nonScalarParameters);
}
var nonLastNonScalarParameter = command.Parameters
@@ -152,63 +136,74 @@ namespace CliFx.Domain
if (nonLastNonScalarParameter != null)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Command {command.Type.FullName} contains a parameter of an enumerable type which doesn't appear last in order:")
.AppendLine($"- {nonLastNonScalarParameter.Property.Name}")
.AppendLine()
.Append("Parameter of an enumerable type must always come last to avoid ambiguity.")
.ToString());
throw CliFxException.CommandParametersNonLastNonScalar(
command,
nonLastNonScalarParameter);
}
}
private static void ValidateOptions(CommandSchema command)
{
var noNameGroup = command.Options
.Where(o => o.ShortName == null && string.IsNullOrWhiteSpace(o.Name))
.ToArray();
if (noNameGroup.Any())
{
throw CliFxException.CommandOptionsNoName(
command,
noNameGroup.ToArray());
}
var invalidLengthNameGroup = command.Options
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
.Where(o => o.Name!.Length <= 1)
.ToArray();
if (invalidLengthNameGroup.Any())
{
throw CliFxException.CommandOptionsInvalidLengthName(
command,
invalidLengthNameGroup);
}
var duplicateNameGroup = command.Options
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
.GroupBy(o => o.Name, StringComparer.OrdinalIgnoreCase)
.GroupBy(o => o.Name!, StringComparer.OrdinalIgnoreCase)
.FirstOrDefault(g => g.Count() > 1);
if (duplicateNameGroup != null)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Command {command.Type.FullName} contains two or more options that have the same name ({duplicateNameGroup.Key}):")
.AppendBulletList(duplicateNameGroup.Select(o => o.Property.Name))
.AppendLine()
.Append("Options in a command must all have unique names.").Append(" ")
.Append("Comparison is NOT case-sensitive.")
.ToString());
throw CliFxException.CommandOptionsDuplicateName(
command,
duplicateNameGroup.Key,
duplicateNameGroup.ToArray());
}
var duplicateShortNameGroup = command.Options
.Where(o => o.ShortName != null)
.GroupBy(o => o.ShortName)
.GroupBy(o => o.ShortName!.Value)
.FirstOrDefault(g => g.Count() > 1);
if (duplicateShortNameGroup != null)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Command {command.Type.FullName} contains two or more options that have the same short name ({duplicateShortNameGroup.Key}):")
.AppendBulletList(duplicateShortNameGroup.Select(o => o.Property.Name))
.AppendLine()
.Append("Options in a command must all have unique short names.").Append(" ")
.Append("Comparison is case-sensitive.")
.ToString());
throw CliFxException.CommandOptionsDuplicateShortName(
command,
duplicateShortNameGroup.Key,
duplicateShortNameGroup.ToArray());
}
var duplicateEnvironmentVariableNameGroup = command.Options
.Where(o => !string.IsNullOrWhiteSpace(o.EnvironmentVariableName))
.GroupBy(o => o.EnvironmentVariableName, StringComparer.OrdinalIgnoreCase)
.GroupBy(o => o.EnvironmentVariableName!, StringComparer.OrdinalIgnoreCase)
.FirstOrDefault(g => g.Count() > 1);
if (duplicateEnvironmentVariableNameGroup != null)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Command {command.Type.FullName} contains two or more options that have the same environment variable name ({duplicateEnvironmentVariableNameGroup.Key}):")
.AppendBulletList(duplicateEnvironmentVariableNameGroup.Select(o => o.Property.Name))
.AppendLine()
.Append("Options in a command must all have unique environment variable names.").Append(" ")
.Append("Comparison is NOT case-sensitive.")
.ToString());
throw CliFxException.CommandOptionsDuplicateEnvironmentVariableName(
command,
duplicateEnvironmentVariableNameGroup.Key,
duplicateEnvironmentVariableNameGroup.ToArray());
}
}
@@ -216,7 +211,7 @@ namespace CliFx.Domain
{
if (!commands.Any())
{
throw new CliFxException("There are no commands configured for this application.");
throw CliFxException.CommandsNotRegistered();
}
var duplicateNameGroup = commands
@@ -225,13 +220,12 @@ namespace CliFx.Domain
if (duplicateNameGroup != null)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Application contains two or more commands that have the same name ({duplicateNameGroup.Key}):")
.AppendBulletList(duplicateNameGroup.Select(o => o.Type.FullName))
.AppendLine()
.Append("Commands must all have unique names. Likewise, there must not be more than one command without a name.").Append(" ")
.Append("Comparison is NOT case-sensitive.")
.ToString());
if (!string.IsNullOrWhiteSpace(duplicateNameGroup.Key))
throw CliFxException.CommandsDuplicateName(
duplicateNameGroup.Key,
duplicateNameGroup.ToArray());
throw CliFxException.CommandsTooManyDefaults(duplicateNameGroup.ToArray());
}
}
@@ -241,17 +235,8 @@ namespace CliFx.Domain
foreach (var commandType in commandTypes)
{
var command = CommandSchema.TryResolve(commandType);
if (command == null)
{
throw new CliFxException(new StringBuilder()
.Append($"Command {commandType.FullName} is not a valid command type.").Append(" ")
.AppendLine("In order to be a valid command type it must:")
.AppendLine($" - Be annotated with {typeof(CommandAttribute).FullName}")
.AppendLine($" - Implement {typeof(ICommand).FullName}")
.AppendLine(" - Not be an abstract class")
.ToString());
}
var command = CommandSchema.TryResolve(commandType) ??
throw CliFxException.InvalidCommandType(commandType);
ValidateParameters(command);
ValidateOptions(command);

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using CliFx.Exceptions;
using CliFx.Internal;
@@ -15,7 +14,9 @@ namespace CliFx.Domain
public string? Description { get; }
public bool IsScalar => GetEnumerableArgumentUnderlyingType() == null;
public abstract string DisplayName { get; }
public bool IsScalar => TryGetEnumerableArgumentUnderlyingType() == null;
protected CommandArgumentSchema(PropertyInfo property, string? description)
{
@@ -23,28 +24,85 @@ namespace CliFx.Domain
Description = description;
}
private Type? GetEnumerableArgumentUnderlyingType() =>
private Type? TryGetEnumerableArgumentUnderlyingType() =>
Property.PropertyType != typeof(string)
? Property.PropertyType.GetEnumerableUnderlyingType()
: null;
private object Convert(IReadOnlyList<string> values)
private object? ConvertScalar(string? value, Type targetType)
{
try
{
// Primitive
var primitiveConverter = PrimitiveConverters.GetValueOrDefault(targetType);
if (primitiveConverter != null)
return primitiveConverter(value);
// Enum
if (targetType.IsEnum)
return Enum.Parse(targetType, value, true);
// Nullable
var nullableUnderlyingType = targetType.GetNullableUnderlyingType();
if (nullableUnderlyingType != null)
return !string.IsNullOrWhiteSpace(value)
? ConvertScalar(value, nullableUnderlyingType)
: null;
// String-constructable
var stringConstructor = GetStringConstructor(targetType);
if (stringConstructor != null)
return stringConstructor.Invoke(new object[] {value!});
// String-parseable (with format provider)
var parseMethodWithFormatProvider = GetStaticParseMethodWithFormatProvider(targetType);
if (parseMethodWithFormatProvider != null)
return parseMethodWithFormatProvider.Invoke(null, new object[] {value!, ConversionFormatProvider});
// String-parseable (without format provider)
var parseMethod = GetStaticParseMethod(targetType);
if (parseMethod != null)
return parseMethod.Invoke(null, new object[] {value!});
}
catch (Exception ex)
{
throw CliFxException.CannotConvertToType(this, value, targetType, ex);
}
throw CliFxException.CannotConvertToType(this, value, targetType);
}
private object ConvertNonScalar(IReadOnlyList<string> values, Type targetEnumerableType, Type targetElementType)
{
var array = values
.Select(v => ConvertScalar(v, targetElementType))
.ToNonGenericArray(targetElementType);
var arrayType = array.GetType();
// Assignable from an array
if (targetEnumerableType.IsAssignableFrom(arrayType))
return array;
// Constructable from an array
var arrayConstructor = targetEnumerableType.GetConstructor(new[] {arrayType});
if (arrayConstructor != null)
return arrayConstructor.Invoke(new object[] {array});
throw CliFxException.CannotConvertNonScalar(this, values, targetEnumerableType);
}
private object? Convert(IReadOnlyList<string> values)
{
var targetType = Property.PropertyType;
var enumerableUnderlyingType = GetEnumerableArgumentUnderlyingType();
var enumerableUnderlyingType = TryGetEnumerableArgumentUnderlyingType();
// Scalar
if (enumerableUnderlyingType == null)
{
if (values.Count > 1)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Can't convert a sequence of values [{string.Join(", ", values)}] to type {targetType.FullName}.")
.Append("Target type is not enumerable and can't accept more than one value.")
.ToString());
}
return ConvertScalar(values.SingleOrDefault(), targetType);
return values.Count <= 1
? ConvertScalar(values.SingleOrDefault(), targetType)
: throw CliFxException.CannotConvertMultipleValuesToNonScalar(this, values);
}
// Non-scalar
else
@@ -58,14 +116,32 @@ namespace CliFx.Domain
public void Inject(ICommand command, params string[] values) =>
Inject(command, (IReadOnlyList<string>) values);
public IReadOnlyList<string> GetValidValues()
{
var result = new List<string>();
// Some arguments may have this as null due to a hack that enables built-in options
if (Property == null)
return result;
var underlyingPropertyType =
Property.PropertyType.GetNullableUnderlyingType() ?? Property.PropertyType;
// Enum
if (underlyingPropertyType.IsEnum)
result.AddRange(Enum.GetNames(underlyingPropertyType));
return result;
}
}
internal partial class CommandArgumentSchema
{
private static readonly IFormatProvider ConversionFormatProvider = CultureInfo.InvariantCulture;
private static readonly IReadOnlyDictionary<Type, Func<string, object>> PrimitiveConverters =
new Dictionary<Type, Func<string?, object>>
private static readonly IReadOnlyDictionary<Type, Func<string?, object?>> PrimitiveConverters =
new Dictionary<Type, Func<string?, object?>>
{
[typeof(object)] = v => v,
[typeof(string)] = v => v,
@@ -99,78 +175,5 @@ namespace CliFx.Domain
type.GetMethod("Parse",
BindingFlags.Public | BindingFlags.Static,
null, new[] {typeof(string), typeof(IFormatProvider)}, null);
private static object ConvertScalar(string? value, Type targetType)
{
try
{
// Primitive
var primitiveConverter = PrimitiveConverters.GetValueOrDefault(targetType);
if (primitiveConverter != null)
return primitiveConverter(value);
// Enum
if (targetType.IsEnum)
return Enum.Parse(targetType, value, true);
// Nullable
var nullableUnderlyingType = targetType.GetNullableUnderlyingType();
if (nullableUnderlyingType != null)
return !string.IsNullOrWhiteSpace(value)
? ConvertScalar(value, nullableUnderlyingType)
: null;
// String-constructable
var stringConstructor = GetStringConstructor(targetType);
if (stringConstructor != null)
return stringConstructor.Invoke(new object[] {value});
// String-parseable (with format provider)
var parseMethodWithFormatProvider = GetStaticParseMethodWithFormatProvider(targetType);
if (parseMethodWithFormatProvider != null)
return parseMethodWithFormatProvider.Invoke(null, new object[] {value, ConversionFormatProvider});
// String-parseable (without format provider)
var parseMethod = GetStaticParseMethod(targetType);
if (parseMethod != null)
return parseMethod.Invoke(null, new object[] {value});
}
catch (Exception ex)
{
throw new CliFxException(new StringBuilder()
.AppendLine($"Failed to convert value '{value ?? "<null>"}' to type {targetType.FullName}.")
.Append(ex.Message)
.ToString(), ex);
}
throw new CliFxException(new StringBuilder()
.AppendLine($"Can't convert value '{value ?? "<null>"}' to type {targetType.FullName}.")
.Append("Target type is not supported by CliFx.")
.ToString());
}
private static object ConvertNonScalar(IReadOnlyList<string> values, Type targetEnumerableType, Type targetElementType)
{
var array = values
.Select(v => ConvertScalar(v, targetElementType))
.ToNonGenericArray(targetElementType);
var arrayType = array.GetType();
// Assignable from an array
if (targetEnumerableType.IsAssignableFrom(arrayType))
return array;
// Constructable from an array
var arrayConstructor = targetEnumerableType.GetConstructor(new[] {arrayType});
if (arrayConstructor != null)
return arrayConstructor.Invoke(new object[] {array});
throw new CliFxException(new StringBuilder()
.AppendLine($"Can't convert a sequence of values [{string.Join(", ", values)}] to type {targetEnumerableType.FullName}.")
.AppendLine($"Underlying element type is [{targetElementType.FullName}].")
.Append("Target type must either be assignable from an array or have a public constructor that takes a single array argument.")
.ToString());
}
}
}

View File

@@ -8,6 +8,11 @@ namespace CliFx.Domain
{
public string Alias { get; }
public string DisplayAlias =>
Alias.Length > 1
? $"--{Alias}"
: $"-{Alias}";
public IReadOnlyList<string> Values { get; }
public bool IsHelpOption => CommandOptionSchema.HelpOption.MatchesNameOrShortName(Alias);
@@ -24,8 +29,7 @@ namespace CliFx.Domain
{
var buffer = new StringBuilder();
buffer.Append(Alias.Length > 1 ? "--" : "-");
buffer.Append(Alias);
buffer.Append(DisplayAlias);
foreach (var value in Values)
{

View File

@@ -3,7 +3,6 @@ using System.Linq;
using System.Reflection;
using System.Text;
using CliFx.Attributes;
using CliFx.Internal;
namespace CliFx.Domain
{
@@ -13,9 +12,9 @@ namespace CliFx.Domain
public char? ShortName { get; }
public string DisplayName => !string.IsNullOrWhiteSpace(Name)
? Name
: ShortName?.AsString()!;
public override string DisplayName => !string.IsNullOrWhiteSpace(Name)
? $"--{Name}"
: $"-{ShortName}";
public string? EnvironmentVariableName { get; }
@@ -40,7 +39,7 @@ namespace CliFx.Domain
!string.IsNullOrWhiteSpace(Name) &&
string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
public bool MatchesShortName(char shortName) =>
public bool MatchesShortName(char? shortName) =>
ShortName != null &&
ShortName == shortName;

View File

@@ -10,9 +10,10 @@ namespace CliFx.Domain
public string? Name { get; }
public string DisplayName => !string.IsNullOrWhiteSpace(Name)
? Name
: Property.Name.ToLowerInvariant();
public override string DisplayName =>
!string.IsNullOrWhiteSpace(Name)
? Name
: Property.Name.ToLowerInvariant();
public CommandParameterSchema(PropertyInfo property, int order, string? name, string? description)
: base(property, description)

View File

@@ -40,7 +40,22 @@ namespace CliFx.Domain
public bool MatchesName(string? name) => string.Equals(name, Name, StringComparison.OrdinalIgnoreCase);
private void InjectParameters(ICommand command, IReadOnlyList<string> parameterInputs)
public IReadOnlyList<CommandOptionSchema> GetBuiltInOptions()
{
var result = new List<CommandOptionSchema>(2);
var helpOption = CommandOptionSchema.HelpOption;
var versionOption = CommandOptionSchema.VersionOption;
result.Add(helpOption);
if (IsDefault)
result.Add(versionOption);
return result;
}
private void InjectParameters(ICommand command, IReadOnlyList<CommandUnboundArgumentInput> parameterInputs)
{
// All inputs must be bound
var remainingParameterInputs = parameterInputs.ToList();
@@ -57,9 +72,9 @@ namespace CliFx.Domain
var scalarParameterInput = i < parameterInputs.Count
? parameterInputs[i]
: throw new CliFxException($"Missing value for parameter <{scalarParameter.DisplayName}>.");
: throw CliFxException.ParameterNotSet(scalarParameter);
scalarParameter.Inject(command, scalarParameterInput);
scalarParameter.Inject(command, scalarParameterInput.Value);
remainingParameterInputs.Remove(scalarParameterInput);
}
@@ -70,18 +85,16 @@ namespace CliFx.Domain
if (nonScalarParameter != null)
{
var nonScalarParameterInputs = parameterInputs.Skip(scalarParameters.Length).ToArray();
nonScalarParameter.Inject(command, nonScalarParameterInputs);
var nonScalarParameterValues = parameterInputs.Skip(scalarParameters.Length).Select(i => i.Value).ToArray();
nonScalarParameter.Inject(command, nonScalarParameterValues);
remainingParameterInputs.Clear();
}
// Ensure all inputs were bound
if (remainingParameterInputs.Any())
{
throw new CliFxException(new StringBuilder()
.AppendLine("Unrecognized parameters provided:")
.AppendBulletList(remainingParameterInputs)
.ToString());
throw CliFxException.UnrecognizedParametersProvided(remainingParameterInputs);
}
}
@@ -97,15 +110,15 @@ namespace CliFx.Domain
var unsetRequiredOptions = Options.Where(o => o.IsRequired).ToList();
// Environment variables
foreach (var environmentVariable in environmentVariables)
foreach (var (name, value) in environmentVariables)
{
var option = Options.FirstOrDefault(o => o.MatchesEnvironmentVariableName(environmentVariable.Key));
var option = Options.FirstOrDefault(o => o.MatchesEnvironmentVariableName(name));
if (option != null)
{
var values = option.IsScalar
? new[] {environmentVariable.Value}
: environmentVariable.Value.Split(Path.PathSeparator);
? new[] {value}
: value.Split(Path.PathSeparator);
option.Inject(command, values);
unsetRequiredOptions.Remove(option);
@@ -122,36 +135,32 @@ namespace CliFx.Domain
if (inputs.Any())
{
option.Inject(command, inputs.SelectMany(i => i.Values).ToArray());
var inputValues = inputs.SelectMany(i => i.Values).ToArray();
option.Inject(command, inputValues);
foreach (var input in inputs)
remainingOptionInputs.Remove(input);
unsetRequiredOptions.Remove(option);
if (inputValues.Any())
unsetRequiredOptions.Remove(option);
}
}
// Ensure all required options were set
if (unsetRequiredOptions.Any())
{
throw new CliFxException(new StringBuilder()
.AppendLine("Missing values for some of the required options:")
.AppendBulletList(unsetRequiredOptions.Select(o => o.DisplayName))
.ToString());
throw CliFxException.RequiredOptionsNotSet(unsetRequiredOptions);
}
// Ensure all inputs were bound
if (remainingOptionInputs.Any())
{
throw new CliFxException(new StringBuilder()
.AppendLine("Unrecognized options provided:")
.AppendBulletList(remainingOptionInputs.Select(o => o.Alias).Distinct())
.ToString());
throw CliFxException.UnrecognizedOptionsProvided(remainingOptionInputs);
}
}
public ICommand CreateInstance(
IReadOnlyList<string> parameterInputs,
IReadOnlyList<CommandUnboundArgumentInput> parameterInputs,
IReadOnlyList<CommandOptionInput> optionInputs,
IReadOnlyDictionary<string, string> environmentVariables,
ITypeActivator activator)
@@ -216,8 +225,8 @@ namespace CliFx.Domain
type,
attribute?.Name,
attribute?.Description,
parameters,
options
parameters!,
options!
);
}
}

View File

@@ -61,7 +61,7 @@ namespace CliFx.Domain
}
else
{
Render(" ");
RenderIndent(margin);
}
}
@@ -199,6 +199,15 @@ namespace CliFx.Domain
if (!string.IsNullOrWhiteSpace(parameter.Description))
{
Render(parameter.Description);
Render(" ");
}
// Valid values
var validValues = parameter.GetValidValues();
if (validValues.Any())
{
Render($"Valid values: {string.Join(", ", validValues)}.");
Render(" ");
}
RenderNewLine();
@@ -212,12 +221,8 @@ namespace CliFx.Domain
var options = command.Options
.OrderByDescending(o => o.IsRequired)
.ToList();
// Add built-in options
options.Add(CommandOptionSchema.HelpOption);
if (command.IsDefault)
options.Add(CommandOptionSchema.VersionOption);
.Concat(command.GetBuiltInOptions())
.ToArray();
foreach (var option in options)
{
@@ -257,11 +262,20 @@ namespace CliFx.Domain
Render(" ");
}
// Valid values
var validValues = option.GetValidValues();
if (validValues.Any())
{
Render($"Valid values: {string.Join(", ", validValues)}.");
Render(" ");
}
// TODO: Render default value here.
// Environment variable
if (!string.IsNullOrWhiteSpace(option.EnvironmentVariableName))
{
Render($"(Environment variable: {option.EnvironmentVariableName}).");
Render(" ");
Render($"Environment variable: {option.EnvironmentVariableName}");
}
RenderNewLine();
@@ -279,9 +293,9 @@ namespace CliFx.Domain
foreach (var childCommand in childCommands)
{
var relativeCommandName =
string.IsNullOrWhiteSpace(childCommand.Name) || string.IsNullOrWhiteSpace(command.Name)
? childCommand.Name
: childCommand.Name.Substring(command.Name.Length + 1);
!string.IsNullOrWhiteSpace(command.Name)
? childCommand.Name!.Substring(command.Name.Length + 1)
: childCommand.Name!;
// Name
RenderIndent();

View File

@@ -1,26 +1,376 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CliFx.Attributes;
using CliFx.Domain;
namespace CliFx.Exceptions
{
/// <summary>
/// Domain exception thrown within CliFx.
/// </summary>
public class CliFxException : Exception
public partial class CliFxException : Exception
{
/// <summary>
/// Represents the default exit code assigned to exceptions in CliFx.
/// </summary>
protected const int DefaultExitCode = -100;
/// <summary>
/// Whether to show the help text after handling this exception.
/// </summary>
public bool ShowHelp { get; }
/// <summary>
/// Whether this exception was constructed with a message.
/// </summary>
/// <remarks>
/// We cannot check against the 'Message' property because it will always return
/// a default message if it was constructed with a null value or is currently null.
/// </remarks>
public bool HasMessage { get; }
/// <summary>
/// Returns an exit code associated with this exception.
/// </summary>
public int ExitCode { get; }
/// <summary>
/// Initializes an instance of <see cref="CliFxException"/>.
/// </summary>
public CliFxException(string? message)
: base(message)
public CliFxException(string? message, bool showHelp = false)
: this(message, null, showHelp: showHelp)
{
}
/// <summary>
/// Initializes an instance of <see cref="CliFxException"/>.
/// </summary>
public CliFxException(string? message, Exception? innerException)
public CliFxException(string? message, Exception? innerException, int exitCode = DefaultExitCode, bool showHelp = false)
: base(message, innerException)
{
ExitCode = exitCode != 0
? exitCode
: throw new ArgumentException("Exit code must not be zero in order to signify failure.");
HasMessage = !string.IsNullOrWhiteSpace(message);
ShowHelp = showHelp;
}
}
// Mid-user-facing exceptions
// Provide more diagnostic information here
public partial class CliFxException
{
internal static CliFxException DefaultActivatorFailed(Type type, Exception? innerException = null)
{
var configureActivatorMethodName = $"{nameof(CliApplicationBuilder)}.{nameof(CliApplicationBuilder.UseTypeActivator)}(...)";
var message = $@"
Failed to create an instance of type '{type.FullName}'.
The type must have a public parameterless constructor in order to be instantiated by the default activator.
To fix this, either make sure this type has a public parameterless constructor, or configure a custom activator using {configureActivatorMethodName}.
Refer to the readme to learn how to integrate a dependency container of your choice to act as a type activator.";
return new CliFxException(message.Trim(), innerException);
}
internal static CliFxException DelegateActivatorReceivedNull(Type type)
{
var message = $@"
Failed to create an instance of type '{type.FullName}', received <null> instead.
To fix this, ensure that the provided type activator was configured correctly, as it's not expected to return <null>.
If you are using a dependency container, ensure this type is registered, because it may return <null> otherwise.";
return new CliFxException(message.Trim());
}
internal static CliFxException InvalidCommandType(Type type)
{
var message = $@"
Command '{type.FullName}' is not a valid command type.
In order to be a valid command type, it must:
- Not be an abstract class
- Implement {typeof(ICommand).FullName}
- Be annotated with {typeof(CommandAttribute).FullName}
If you're experiencing problems, please refer to the readme for a quickstart example.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandsNotRegistered()
{
var message = $@"
There are no commands configured in the application.
To fix this, ensure that at least one command is added through one of the methods on {nameof(CliApplicationBuilder)}.
If you're experiencing problems, please refer to the readme for a quickstart example.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandsTooManyDefaults(
IReadOnlyList<CommandSchema> invalidCommands)
{
var message = $@"
Application configuration is invalid because there are {invalidCommands.Count} default commands:
{string.Join(Environment.NewLine, invalidCommands.Select(p => p.Type.FullName))}
There can only be one default command (i.e. command with no name) in an application.
Other commands must have unique non-empty names that identify them.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandsDuplicateName(
string name,
IReadOnlyList<CommandSchema> invalidCommands)
{
var message = $@"
Application configuration is invalid because there are {invalidCommands.Count} commands with the same name ('{name}'):
{string.Join(Environment.NewLine, invalidCommands.Select(p => p.Type.FullName))}
Commands must have unique names.
Names are not case-sensitive.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandParametersDuplicateOrder(
CommandSchema command,
int order,
IReadOnlyList<CommandParameterSchema> invalidParameters)
{
var message = $@"
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameters with the same order ({order}):
{string.Join(Environment.NewLine, invalidParameters.Select(p => p.Property.Name))}
Parameters must have unique order.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandParametersDuplicateName(
CommandSchema command,
string name,
IReadOnlyList<CommandParameterSchema> invalidParameters)
{
var message = $@"
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameters with the same name ('{name}'):
{string.Join(Environment.NewLine, invalidParameters.Select(p => p.Property.Name))}
Parameters must have unique names to avoid potential confusion in the help text.
Names are not case-sensitive.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandParametersTooManyNonScalar(
CommandSchema command,
IReadOnlyList<CommandParameterSchema> invalidParameters)
{
var message = $@"
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} non-scalar parameters:
{string.Join(Environment.NewLine, invalidParameters.Select(p => p.Property.Name))}
Non-scalar parameter is such that is bound from more than one value (e.g. array or a complex object).
Only one parameter in a command may be non-scalar and it must be the last one in order.
If it's not feasible to fit into these constraints, consider using options instead as they don't have these limitations.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandParametersNonLastNonScalar(
CommandSchema command,
CommandParameterSchema invalidParameter)
{
var message = $@"
Command '{command.Type.FullName}' is invalid because it contains a non-scalar parameter which is not the last in order:
{invalidParameter.Property.Name}
Non-scalar parameter is such that is bound from more than one value (e.g. array or a complex object).
Only one parameter in a command may be non-scalar and it must be the last one in order.
If it's not feasible to fit into these constraints, consider using options instead as they don't have these limitations.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandOptionsNoName(
CommandSchema command,
IReadOnlyList<CommandOptionSchema> invalidOptions)
{
var message = $@"
Command '{command.Type.FullName}' is invalid because it contains one or more options without a name:
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
Options must have either a name or a short name or both.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandOptionsInvalidLengthName(
CommandSchema command,
IReadOnlyList<CommandOptionSchema> invalidOptions)
{
var message = $@"
Command '{command.Type.FullName}' is invalid because it contains one or more options whose names are too short:
{string.Join(Environment.NewLine, invalidOptions.Select(o => $"{o.Property.Name} ('{o.DisplayName}')"))}
Option names must be at least 2 characters long to avoid confusion with short names.
If you intended to set the short name instead, use the attribute overload that accepts a char.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandOptionsDuplicateName(
CommandSchema command,
string name,
IReadOnlyList<CommandOptionSchema> invalidOptions)
{
var message = $@"
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same name ('{name}'):
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
Options must have unique names, because that's what identifies them.
Names are not case-sensitive.
To fix this, ensure that all options have different names.";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandOptionsDuplicateShortName(
CommandSchema command,
char shortName,
IReadOnlyList<CommandOptionSchema> invalidOptions)
{
var message = $@"
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same short name ('{shortName}'):
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
Options must have unique short names.
Short names are case-sensitive (i.e. 'a' and 'A' are different short names).";
return new CliFxException(message.Trim());
}
internal static CliFxException CommandOptionsDuplicateEnvironmentVariableName(
CommandSchema command,
string environmentVariableName,
IReadOnlyList<CommandOptionSchema> invalidOptions)
{
var message = $@"
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same fallback environment variable name ('{environmentVariableName}'):
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
Options cannot share the same environment variable as a fallback.
Environment variable names are not case-sensitive.";
return new CliFxException(message.Trim());
}
}
// End-user-facing exceptions
// Avoid internal details and fix recommendations here
public partial class CliFxException
{
internal static CliFxException CannotFindMatchingCommand(CommandLineInput input)
{
var message = $@"
Can't find a command that matches the following arguments:
{string.Join(" ", input.UnboundArguments.Select(a => a.Value))}";
return new CliFxException(message.Trim(), showHelp: true);
}
internal static CliFxException CannotConvertMultipleValuesToNonScalar(
CommandArgumentSchema argument,
IReadOnlyList<string> values)
{
var argumentDisplayText = argument is CommandParameterSchema
? $"Parameter <{argument.DisplayName}>"
: $"Option '{argument.DisplayName}'";
var message = $@"
{argumentDisplayText} expects a single value, but provided with multiple:
{string.Join(", ", values.Select(v => $"'{v}'"))}";
return new CliFxException(message.Trim(), showHelp: true);
}
internal static CliFxException CannotConvertToType(
CommandArgumentSchema argument,
string? value,
Type type,
Exception? innerException = null)
{
var argumentDisplayText = argument is CommandParameterSchema
? $"parameter <{argument.DisplayName}>"
: $"option '{argument.DisplayName}'";
var message = $@"
Can't convert value '{value ?? "<null>"}' to type '{type.FullName}' for {argumentDisplayText}.
{innerException?.Message ?? "This type is not supported."}";
return new CliFxException(message.Trim(), innerException, showHelp: true);
}
internal static CliFxException CannotConvertNonScalar(
CommandArgumentSchema argument,
IReadOnlyList<string> values,
Type type)
{
var argumentDisplayText = argument is CommandParameterSchema
? $"parameter <{argument.DisplayName}>"
: $"option '{argument.DisplayName}'";
var message = $@"
Can't convert provided values to type '{type.FullName}' for {argumentDisplayText}:
{string.Join(", ", values.Select(v => $"'{v}'"))}
Target type is not assignable from array and doesn't have a public constructor that takes an array.";
return new CliFxException(message.Trim(), showHelp: true);
}
internal static CliFxException ParameterNotSet(CommandParameterSchema parameter)
{
var message = $@"
Missing value for parameter <{parameter.DisplayName}>.";
return new CliFxException(message.Trim(), showHelp: true);
}
internal static CliFxException RequiredOptionsNotSet(IReadOnlyList<CommandOptionSchema> options)
{
var message = $@"
Missing values for one or more required options:
{string.Join(Environment.NewLine, options.Select(o => o.DisplayName))}";
return new CliFxException(message.Trim(), showHelp: true);
}
internal static CliFxException UnrecognizedParametersProvided(IReadOnlyList<CommandUnboundArgumentInput> inputs)
{
var message = $@"
Unrecognized parameters provided:
{string.Join(Environment.NewLine, inputs.Select(i => $"<{i.Value}>"))}";
return new CliFxException(message.Trim(), showHelp: true);
}
internal static CliFxException UnrecognizedOptionsProvided(IReadOnlyList<CommandOptionInput> inputs)
{
var message = $@"
Unrecognized options provided:
{string.Join(Environment.NewLine, inputs.Select(i => i.DisplayAlias))}";
return new CliFxException(message.Trim(), showHelp: true);
}
}
}

View File

@@ -7,39 +7,31 @@ namespace CliFx.Exceptions
/// Use this exception if you want to report an error that occured during execution of a command.
/// This exception also allows specifying exit code which will be returned to the calling process.
/// </summary>
public class CommandException : Exception
public class CommandException : CliFxException
{
private const int DefaultExitCode = -100;
/// <summary>
/// Process exit code.
/// </summary>
public int ExitCode { get; }
/// <summary>
/// Initializes an instance of <see cref="CommandException"/>.
/// </summary>
public CommandException(string? message, Exception? innerException, int exitCode = DefaultExitCode)
: base(message, innerException)
public CommandException(string? message, Exception? innerException,
int exitCode = DefaultExitCode, bool showHelp = false)
: base(message, innerException, exitCode, showHelp)
{
ExitCode = exitCode != 0
? exitCode
: throw new ArgumentException("Exit code must not be zero in order to signify failure.");
}
/// <summary>
/// Initializes an instance of <see cref="CommandException"/>.
/// </summary>
public CommandException(string? message, int exitCode = DefaultExitCode)
: this(message, null, exitCode)
public CommandException(string? message, int exitCode = DefaultExitCode, bool showHelp = false)
: this(message, null, exitCode, showHelp)
{
}
/// <summary>
/// Initializes an instance of <see cref="CommandException"/>.
/// </summary>
public CommandException(int exitCode = DefaultExitCode)
: this(null, exitCode)
public CommandException(int exitCode = DefaultExitCode, bool showHelp = false)
: this(null, exitCode, showHelp)
{
}
}

View File

@@ -1,42 +0,0 @@
using System;
namespace CliFx
{
/// <summary>
/// Extensions for <see cref="CliFx"/>
/// </summary>
public static class Extensions
{
/// <summary>
/// Sets console foreground color, executes specified action, and sets the color back to the original value.
/// </summary>
public static void WithForegroundColor(this IConsole console, ConsoleColor foregroundColor, Action action)
{
var lastColor = console.ForegroundColor;
console.ForegroundColor = foregroundColor;
action();
console.ForegroundColor = lastColor;
}
/// <summary>
/// Sets console background color, executes specified action, and sets the color back to the original value.
/// </summary>
public static void WithBackgroundColor(this IConsole console, ConsoleColor backgroundColor, Action action)
{
var lastColor = console.BackgroundColor;
console.BackgroundColor = backgroundColor;
action();
console.BackgroundColor = lastColor;
}
/// <summary>
/// Sets console foreground and background colors, executes specified action, and sets the colors back to the original values.
/// </summary>
public static void WithColors(this IConsole console, ConsoleColor foregroundColor, ConsoleColor backgroundColor, Action action) =>
console.WithForegroundColor(foregroundColor, () => console.WithBackgroundColor(backgroundColor, action));
}
}

View File

@@ -54,6 +54,16 @@ namespace CliFx
/// </summary>
void ResetColor();
/// <summary>
/// Cursor left offset.
/// </summary>
int CursorLeft { get; set; }
/// <summary>
/// Cursor top offset.
/// </summary>
int CursorTop { get; set; }
/// <summary>
/// Provides a token that signals when application cancellation is requested.
/// Subsequent calls return the same token.
@@ -61,4 +71,42 @@ namespace CliFx
/// </summary>
CancellationToken GetCancellationToken();
}
/// <summary>
/// Extensions for <see cref="IConsole"/>.
/// </summary>
public static class ConsoleExtensions
{
/// <summary>
/// Sets console foreground color, executes specified action, and sets the color back to the original value.
/// </summary>
public static void WithForegroundColor(this IConsole console, ConsoleColor foregroundColor, Action action)
{
var lastColor = console.ForegroundColor;
console.ForegroundColor = foregroundColor;
action();
console.ForegroundColor = lastColor;
}
/// <summary>
/// Sets console background color, executes specified action, and sets the color back to the original value.
/// </summary>
public static void WithBackgroundColor(this IConsole console, ConsoleColor backgroundColor, Action action)
{
var lastColor = console.BackgroundColor;
console.BackgroundColor = backgroundColor;
action();
console.BackgroundColor = lastColor;
}
/// <summary>
/// Sets console foreground and background colors, executes specified action, and sets the colors back to the original values.
/// </summary>
public static void WithColors(this IConsole console, ConsoleColor foregroundColor, ConsoleColor backgroundColor, Action action) =>
console.WithForegroundColor(foregroundColor, () => console.WithBackgroundColor(backgroundColor, action));
}
}

View File

@@ -1,18 +1,20 @@
// ReSharper disable CheckNamespace
#if NET45 || NETSTANDARD2_0
using System.Collections.Generic;
using System.Text;
// Polyfills to bridge the missing APIs in older versions of the framework/standard.
namespace CliFx.Internal
#if NETSTANDARD2_0
namespace System.Collections.Generic
{
internal static class Polyfills
internal static class Extensions
{
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> self, TKey key) =>
self.TryGetValue(key, out var value) ? value : default;
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
{
key = pair.Key;
value = pair.Value;
}
public static StringBuilder AppendJoin<T>(this StringBuilder self, string separator, IEnumerable<T> items) =>
self.Append(string.Join(separator, items));
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dic, TKey key) =>
dic.TryGetValue(key, out var result) ? result! : default!;
}
}
#endif

View File

@@ -0,0 +1,14 @@
using System.Text;
namespace CliFx.Internal
{
internal static class StringExtensions
{
public static string Repeat(this char c, int count) => new string(c, count);
public static string AsString(this char c) => c.Repeat(1);
public static StringBuilder AppendIfNotEmpty(this StringBuilder builder, char value) =>
builder.Length > 0 ? builder.Append(value) : builder;
}
}

View File

@@ -2,31 +2,11 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CliFx.Internal
{
internal static class Extensions
internal static class TypeExtensions
{
public static string Repeat(this char c, int count) => new string(c, count);
public static string AsString(this char c) => c.Repeat(1);
public static StringBuilder AppendIfNotEmpty(this StringBuilder builder, char value) =>
builder.Length > 0 ? builder.Append(value) : builder;
public static StringBuilder AppendBulletList<T>(this StringBuilder builder, IEnumerable<T> items)
{
foreach (var item in items)
{
builder.Append("- ");
builder.Append(item);
builder.AppendLine();
}
return builder;
}
public static bool Implements(this Type type, Type interfaceType) => type.GetInterfaces().Contains(interfaceType);
public static Type? GetNullableUnderlyingType(this Type type) => Nullable.GetUnderlyingType(type);

View File

@@ -7,7 +7,7 @@ namespace CliFx
/// <summary>
/// Implementation of <see cref="IConsole"/> that wraps the default system console.
/// </summary>
public class SystemConsole : IConsole
public partial class SystemConsole : IConsole
{
private CancellationTokenSource? _cancellationTokenSource;
@@ -48,14 +48,28 @@ namespace CliFx
/// </summary>
public SystemConsole()
{
Input = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding, false);
Output = new StreamWriter(Console.OpenStandardOutput(), Console.OutputEncoding) {AutoFlush = true};
Error = new StreamWriter(Console.OpenStandardError(), Console.OutputEncoding) {AutoFlush = true};
Input = WrapInput(Console.OpenStandardInput());
Output = WrapOutput(Console.OpenStandardOutput());
Error = WrapOutput(Console.OpenStandardError());
}
/// <inheritdoc />
public void ResetColor() => Console.ResetColor();
/// <inheritdoc />
public int CursorLeft
{
get => Console.CursorLeft;
set => Console.CursorLeft = value;
}
/// <inheritdoc />
public int CursorTop
{
get => Console.CursorTop;
set => Console.CursorTop = value;
}
/// <inheritdoc />
public CancellationToken GetCancellationToken()
{
@@ -77,4 +91,17 @@ namespace CliFx
return (_cancellationTokenSource = cts).Token;
}
}
public partial class SystemConsole
{
private static StreamReader WrapInput(Stream? stream) =>
stream != null
? new StreamReader(stream, Console.InputEncoding, false)
: StreamReader.Null;
private static StreamWriter WrapOutput(Stream? stream) =>
stream != null
? new StreamWriter(stream, Console.OutputEncoding) {AutoFlush = true}
: StreamWriter.Null;
}
}

View File

@@ -1,13 +0,0 @@
namespace CliFx.Utilities
{
/// <summary>
/// Extensions for <see cref="Utilities"/>.
/// </summary>
public static class Extensions
{
/// <summary>
/// Creates a <see cref="ProgressTicker"/> bound to this console.
/// </summary>
public static ProgressTicker CreateProgressTicker(this IConsole console) => new ProgressTicker(console);
}
}

View File

@@ -9,26 +9,29 @@ namespace CliFx.Utilities
{
private readonly IConsole _console;
private string _lastOutput = "";
private int? _originalCursorLeft;
private int? _originalCursorTop;
/// <summary>
/// Initializes an instance of <see cref="ProgressTicker"/>.
/// </summary>
public ProgressTicker(IConsole console)
{
_console = console;
}
private void EraseLastOutput()
{
for (var i = 0; i < _lastOutput.Length; i++)
_console.Output.Write('\b');
}
public ProgressTicker(IConsole console) => _console = console;
private void RenderProgress(double progress)
{
_lastOutput = progress.ToString("P2", _console.Output.FormatProvider);
_console.Output.Write(_lastOutput);
if (_originalCursorLeft != null && _originalCursorTop != null)
{
_console.CursorLeft = _originalCursorLeft.Value;
_console.CursorTop = _originalCursorTop.Value;
}
else
{
_originalCursorLeft = _console.CursorLeft;
_originalCursorTop = _console.CursorTop;
}
var str = progress.ToString("P2", _console.Output.FormatProvider);
_console.Output.Write(str);
}
/// <summary>
@@ -41,9 +44,19 @@ namespace CliFx.Utilities
// when there's no active console window.
if (!_console.IsOutputRedirected)
{
EraseLastOutput();
RenderProgress(progress);
}
}
}
/// <summary>
/// Extensions for <see cref="ProgressTicker"/>.
/// </summary>
public static class ProgressTickerExtensions
{
/// <summary>
/// Creates a <see cref="ProgressTicker"/> bound to this console.
/// </summary>
public static ProgressTicker CreateProgressTicker(this IConsole console) => new ProgressTicker(console);
}
}

View File

@@ -44,6 +44,12 @@ namespace CliFx
BackgroundColor = ConsoleColor.Black;
}
/// <inheritdoc />
public int CursorLeft { get; set; }
/// <inheritdoc />
public int CursorTop { get; set; }
/// <inheritdoc />
public CancellationToken GetCancellationToken() => _cancellationToken;

View File

@@ -26,6 +26,7 @@ An important property of CliFx, when compared to some other libraries, is that i
- Prints errors and routes exit codes on exceptions
- Provides comprehensive and colorful auto-generated help text
- Highly testable and easy to debug
- Comes with built-in analyzers to help catch common mistakes
- Targets .NET Framework 4.5+ and .NET Standard 2.0+
- No external dependencies
@@ -430,6 +431,19 @@ Division by zero is not supported.
1337
```
You can also specify the `showHelp` parameter to instruct whether to show the help text after printing the error:
```c#
[Command]
public class ExampleCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console)
{
throw new CommandException("Something went wrong.", showHelp: true);
}
}
```
### Graceful cancellation
It is possible to gracefully cancel execution of a command and preform any necessary cleanup. By default an app gets forcefully killed when it receives an interrupt signal (Ctrl+C or Ctrl+Break), but you can easily override this behavior.