62 Commits
1.1 ... 1.5

Author SHA1 Message Date
Tyrrrz
60a3b26fd1 Update version 2020-10-23 23:46:57 +03:00
Tyrrrz
3abdfb1acf Improve child command usage info in help text 2020-10-23 23:36:36 +03:00
Tyrrrz
9557d386e2 Better success check in stack trace parsing 2020-10-23 23:00:29 +03:00
Tyrrrz
d0d024c427 Improve stack trace parsing 2020-10-23 22:57:48 +03:00
Tyrrrz
f765af6061 Update readme with info about custom converters 2020-10-23 22:03:20 +03:00
Tyrrrz
7f2202e869 Cleanup 2020-10-23 21:40:53 +03:00
Tyrrrz
14ad9d5738 Improve tests 2020-10-23 21:18:57 +03:00
Tyrrrz
b120138de3 Update github actions 2020-10-23 20:53:27 +03:00
Tyrrrz
8df1d607c1 Refactor & improve argument conversion feature 2020-10-23 20:52:26 +03:00
Tyrrrz
c06f2810b9 Cleanup analyzers 2020-10-23 18:23:58 +03:00
Tyrrrz
d52a205f13 Improve coverage slightly 2020-10-23 18:17:17 +03:00
Tyrrrz
0ec12e57c1 Refactor pretty stack traces 2020-10-23 18:01:40 +03:00
Nikiforov Alexey
c322b7029c Add child command usage in help text (#77) 2020-10-22 16:02:58 +03:00
Oleksandr Shustov
6a38c04c11 Custom value converters (#81) 2020-10-16 14:22:42 +03:00
Alexey Golub
5e53107def Treat nullability warnings as errors 2020-10-07 15:20:35 +03:00
Alexey Golub
36cea937de Update nuget packages 2020-10-07 15:11:37 +03:00
Mårten Åsberg
438d6b98ac Pretty printing of exception messages (#79) 2020-10-06 20:43:19 +03:00
Alexey Golub
8e1488c395 Update readme 2020-09-14 22:31:25 +03:00
Alexey Golub
65d321b476 Update readme 2020-08-20 19:31:26 +03:00
Alexey Golub
c6d2359d6b Update version 2020-08-20 17:02:19 +03:00
Alexey Golub
0d32876bad Add VirtualConsole.CreateBuffered() for easier testing 2020-08-20 16:27:23 +03:00
Alexey Golub
c063251d89 Exclude some ToString() methods from coverage
These are only used for debug information and I'm okayish with them failing at runtime.
2020-08-19 22:39:43 +03:00
Alexey Golub
3831cfc7c0 Get rid of internal tests
Move all tests to e2e+ level
2020-08-19 22:31:09 +03:00
Alexey Golub
b17341b56c Update version 2020-07-31 15:41:47 +03:00
Alexey Golub
5bda964fb5 Cleanup 2020-07-31 15:34:38 +03:00
Daniel Hix
432430489a Add error for non-scalar parameters bound without any values (#71) 2020-07-31 15:08:13 +03:00
Ron Myers
9a20101f30 Fix application crashes if there are two environment variables with same name, differing only in case (#67) 2020-07-28 14:20:02 +03:00
Alexey Golub
b491818779 Update version 2020-07-19 18:20:42 +03:00
Alexey Golub
69c24c8dfc Refactor 2020-07-19 18:11:54 +03:00
Ihor Nechyporuk
004f906148 Fix exit code overflow for unhandled exceptions on Unix systems (#62) 2020-07-19 16:50:37 +03:00
Volodymyr Shkolka
ac83233dc2 Add ability to specify active debugger attachment instead of passive (#61) 2020-07-10 13:54:09 +03:00
Alexey Golub
082910c968 Update readme 2020-05-24 12:46:16 +03:00
Alexey Golub
11e3e0f85d Update version 2020-05-23 19:02:48 +03:00
Alexey Golub
42f4d7d5a7 Use Stream.Synchronized 2020-05-23 18:48:46 +03:00
Alexey Golub
bed22b6500 Refactor (#56) 2020-05-23 18:45:07 +03:00
Alexey Golub
17449e0794 Remove unused dummy commands 2020-05-16 22:16:42 +03:00
Alexey Golub
4732166f5f Refactor 2020-05-16 21:54:16 +03:00
Alexey Golub
f5e37b96fc Default to semantic representation of assembly version in help text 2020-05-16 14:49:25 +03:00
Domn Werner
4cef596fe8 Show default values in help (#54) 2020-05-16 14:11:23 +03:00
Alexey Golub
19b87717c1 [Analyzers] Switch from warnings to errors where relevant 2020-05-13 23:15:46 +03:00
Alexey Golub
7e4c6b20ff Update readme 2020-05-12 20:36:38 +03:00
Alexey Golub
fb2071ed2b Update readme 2020-05-11 21:53:22 +03:00
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
125 changed files with 7551 additions and 3916 deletions

View File

@@ -11,10 +11,10 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v2.3.3
- name: Install .NET Core
uses: actions/setup-dotnet@v1.4.0
uses: actions/setup-dotnet@v1.7.2
with:
dotnet-version: 3.1.100
@@ -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

@@ -12,18 +12,24 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v2.3.3
- name: Install .NET Core
uses: actions/setup-dotnet@v1.4.0
uses: actions/setup-dotnet@v1.7.2
with:
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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View File

@@ -1,3 +1,43 @@
### v1.5 (23-Oct-2020)
- Added pretty-printing for unhandled exceptions thrown from within the application. This makes the errors easier to parse visually and should help in troubleshooting. This change does not affect `CommandException`, as it already has special treatment. (Thanks [@Mårten Åsberg](https://github.com/89netraM))
- Added support for custom value converters. You can now create a type that implements `CliFx.IArgumentValueConverter` and specify it as a converter for your parameters or options via the `Converter` named property. This should enable conversion between raw argument values and custom types which are not string-initializable. (Thanks [@Oleksandr Shustov](https://github.com/AlexandrShustov))
- Improved help text so that it also shows minimal usage examples for child and descendant commands, besides the actual command it was requested on. This should improve user experience for applications with many nested commands. (Thanks [@Nikiforov Alexey](https://github.com/NikiforovAll))
### v1.4 (20-Aug-2020)
- Added `VirtualConsole.CreateBuffered()` method to simplify test setup when using in-memory backing stores for output and error streams. Please refer to the readme for updated recommendations on how to test applications built with CliFx.
- Added generic `CliApplicationBuilder.AddCommand<TCommand>()`. This overload simplifies adding commands one-by-one as it also checks that the type implements `ICommand`.
### v1.3.2 (31-Jul-2020)
- Fixed an issue where a command was incorrectly allowed to execute when the user did not specify any value for a non-scalar parameter. Since they are always required, a parameter needs to be bound to (at least) one value. (Thanks [@Daniel Hix](https://github.com/ADustyOldMuffin))
- Fixed an issue where `CliApplication.RunAsync(...)` threw `ArgumentException` if there were two environment variables, whose names differed only in case. Environment variable names are now treated case-sensitively. (Thanks [@Ron Myers](https://github.com/ron-myers))
### v1.3.1 (19-Jul-2020)
- Running the application with the debug directive (`myapp [debug]`) will now also try to launch a debugger instance. In most cases it will save time as you won't need to attach the debugger manually. (Thanks [@Volodymyr Shkolka](https://github.com/BlackGad))
- Fixed an issue where unhandled generic exceptions (i.e. not `CommandException`) sometimes caused the application to incorrectly return successful exit code due to an overflow issue on Unix systems. Starting from this version, all unhandled generic exceptions will produce `1` as the exit code when thrown. Instances of `CommandException` can still be configured to return any specified exit code, but it's recommended to constrain the values between `1` and `255` to avoid overflow issues. (Thanks [@Ihor Nechyporuk](https://github.com/inech))
### v1.3 (23-May-2020)
- Changed analyzers to report errors instead of warnings. If you find that some analyzer works incorrectly, please report it on GitHub. You can also configure inspection severity overrides in your project if you need to.
- Improved help text by showing default values for non-required options. This only works on types that have a custom override for `ToString()` method. Additionally, if the type implements `IFormattable`, the overload with a format provider will be used instead. (Thanks [@Domn Werner](https://github.com/domn1995))
- Changed default version text to only show 3 version components instead of 4, if the last component (revision) is not specified or is zero. This makes the default version text compliant with semantic versioning.
- Fixed an issue where it was possible to define a command with an option that has the same name or short name as built-in help or version options. Previously it would lead to the user-defined option being ignored in favor of the built-in option. Now this will throw an exception instead.
- Changed the underlying representation of `StreamReader`/`StreamWriter` objects used in `SystemConsole` and `VirtualConsole` to be thread-safe.
### 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.1" />
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.1" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
<PackageReference Include="coverlet.msbuild" Version="2.9.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" />
<ProjectReference Include="..\CliFx\CliFx.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,585 @@
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(
"Parameter with valid converter",
Analyzer.SupportedDiagnostics,
// language=cs
@"
public class MyConverter : IArgumentValueConverter
{
public object ConvertFrom(string value) => value;
}
[Command]
public class MyCommand : ICommand
{
[CommandParameter(0, Converter = typeof(MyConverter))]
public string Param { 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 Option { 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 Option { 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 OptionA { get; set; }
[CommandOption(""bar"")]
public string OptionB { 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 OptionA { get; set; }
[CommandOption('x')]
public string OptionB { 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 OptionA { get; set; }
[CommandOption('b', EnvironmentVariableName = ""env_var_b"")]
public string OptionB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Option with valid converter",
Analyzer.SupportedDiagnostics,
// language=cs
@"
public class MyConverter : IArgumentValueConverter
{
public object ConvertFrom(string value) => value;
}
[Command]
public class MyCommand : ICommand
{
[CommandOption('o', Converter = typeof(MyConverter))]
public string Option { 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(
"Parameter with invalid converter",
DiagnosticDescriptors.CliFx0025,
// language=cs
@"
public class MyConverter
{
public object ConvertFrom(string value) => value;
}
[Command]
public class MyCommand : ICommand
{
[CommandParameter(0, Converter = typeof(MyConverter))]
public string Param { 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 Option { 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 Option { 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 OptionA { get; set; }
[CommandOption(""foo"")]
public string OptionB { 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 OptionA { get; set; }
[CommandOption('f')]
public string OptionB { 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 OptionA { get; set; }
[CommandOption('b', EnvironmentVariableName = ""env_var"")]
public string OptionB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}"
)
};
yield return new object[]
{
new AnalyzerTestCase(
"Option with invalid converter",
DiagnosticDescriptors.CliFx0046,
// language=cs
@"
public class MyConverter
{
public object ConvertFrom(string value) => value;
}
[Command]
public class MyCommand : ICommand
{
[CommandOption('o', Converter = typeof(MyConverter))]
public string Option { 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,350 @@
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.CliFx0025,
DiagnosticDescriptors.CliFx0041,
DiagnosticDescriptors.CliFx0042,
DiagnosticDescriptors.CliFx0043,
DiagnosticDescriptors.CliFx0044,
DiagnosticDescriptors.CliFx0045,
DiagnosticDescriptors.CliFx0046
);
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;
var converter = attribute
.NamedArguments
.Where(a => a.Key == "Converter")
.Select(a => a.Value.Value)
.FirstOrDefault() as ITypeSymbol;
return new
{
Property = p,
Order = order,
Name = name,
Converter = converter
};
})
.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()
));
}
// Invalid converter
var invalidConverterParameters = parameters
.Where(p =>
p.Converter != null &&
!p.Converter.AllInterfaces.Any(KnownSymbols.IsArgumentValueConverterInterface))
.ToArray();
foreach (var parameter in invalidConverterParameters)
{
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.CliFx0025, parameter.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;
var converter = attribute
.NamedArguments
.Where(a => a.Key == "Converter")
.Select(a => a.Value.Value)
.FirstOrDefault() as ITypeSymbol;
return new
{
Property = p,
Name = name,
ShortName = shortName,
EnvironmentVariableName = envVarName,
Converter = converter
};
})
.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.Ordinal)
.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()
));
}
// Invalid converter
var invalidConverterOptions = options
.Where(o =>
o.Converter != null &&
!o.Converter.AllInterfaces.Any(KnownSymbols.IsArgumentValueConverterInterface))
.ToArray();
foreach (var option in invalidConverterOptions)
{
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.CliFx0046, option.Property.Locations.First()
));
}
}
private static void CheckCommandType(SymbolAnalysisContext context)
{
// Named type: MyCommand
if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol) ||
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,74 @@
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)
{
if (invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax &&
context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol)
{
// Direct call to System.Console (e.g. System.Console.WriteLine())
if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType))
{
return true;
}
// Indirect call to System.Console (e.g. System.Console.Error.WriteLine())
if (memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax &&
context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol)
{
return KnownSymbols.IsSystemConsole(propertySymbol.ContainingType);
}
}
return false;
}
private static void CheckSystemConsoleUsage(SyntaxNodeAnalysisContext context)
{
if (context.Node is InvocationExpressionSyntax invocationSyntax &&
IsSystemConsoleInvocation(context, invocationSyntax))
{
// Check if IConsole is available in scope as alternative to System.Console
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)
{
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,105 @@
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.Error, 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.Error, 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.Error, 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.Error, 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.Error, 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.Error, true
);
public static readonly DiagnosticDescriptor CliFx0025 =
new DiagnosticDescriptor(nameof(CliFx0025),
"Parameter converter must implement 'CliFx.IArgumentValueConverter'",
"Parameter converter must implement 'CliFx.IArgumentValueConverter'",
"Usage", DiagnosticSeverity.Error, 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.Error, 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.Error, 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.Error, 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.Error, 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.Error, true
);
public static readonly DiagnosticDescriptor CliFx0046 =
new DiagnosticDescriptor(nameof(CliFx0046),
"Option converter must implement 'CliFx.IArgumentValueConverter'",
"Option converter must implement 'CliFx.IArgumentValueConverter'",
"Usage", DiagnosticSeverity.Error, 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,40 @@
using CliFx.Analyzers.Internal;
using Microsoft.CodeAnalysis;
namespace CliFx.Analyzers
{
internal 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 IsArgumentValueConverterInterface(ISymbol symbol) =>
symbol.DisplayNameMatches("CliFx.IArgumentValueConverter");
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

@@ -18,7 +18,7 @@ namespace CliFx.Benchmarks
[Benchmark(Description = "CliFx", Baseline = true)]
public async ValueTask<int> ExecuteWithCliFx() =>
await new CliApplicationBuilder().AddCommand(typeof(CliFxCommand)).Build().RunAsync(Arguments, new Dictionary<string, string>());
await new CliApplicationBuilder().AddCommand<CliFxCommand>().Build().RunAsync(Arguments, new Dictionary<string, string>());
[Benchmark(Description = "System.CommandLine")]
public async Task<int> ExecuteWithSystemCommandLine() =>

View File

@@ -9,9 +9,9 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
<PackageReference Include="clipr" Version="1.6.1" />
<PackageReference Include="Cocona" Version="1.3.0" />
<PackageReference Include="CommandLineParser" Version="2.7.82" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.6.0" />
<PackageReference Include="Cocona" Version="1.5.0" />
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="3.0.0" />
<PackageReference Include="PowerArgs" Version="3.6.0" />
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" />
</ItemGroup>

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.8" />
<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

@@ -1,137 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests
{
public partial class ApplicationSpecs
{
[Command]
private class NonImplementedCommand
{
}
private class NonAnnotatedCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("dup")]
private class DuplicateNameCommandA : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("dup")]
private class DuplicateNameCommandB : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class DuplicateParameterOrderCommand : ICommand
{
[CommandParameter(13)]
public string? ParameterA { get; set; }
[CommandParameter(13)]
public string? ParameterB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class DuplicateParameterNameCommand : ICommand
{
[CommandParameter(0, Name = "param")]
public string? ParameterA { get; set; }
[CommandParameter(1, Name = "param")]
public string? ParameterB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class MultipleNonScalarParametersCommand : ICommand
{
[CommandParameter(0)]
public IReadOnlyList<string>? ParameterA { get; set; }
[CommandParameter(1)]
public IReadOnlyList<string>? ParameterB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class NonLastNonScalarParameterCommand : ICommand
{
[CommandParameter(0)]
public IReadOnlyList<string>? ParameterA { get; set; }
[CommandParameter(1)]
public string? ParameterB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class DuplicateOptionNamesCommand : ICommand
{
[CommandOption("fruits")]
public string? Apples { get; set; }
[CommandOption("fruits")]
public string? Oranges { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class DuplicateOptionShortNamesCommand : ICommand
{
[CommandOption('x')]
public string? OptionA { get; set; }
[CommandOption('x')]
public string? OptionB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class DuplicateOptionEnvironmentVariableNamesCommand : ICommand
{
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
public string? OptionA { get; set; }
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
public string? OptionB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class ValidCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("hidden", Description = "Description")]
private class HiddenPropertiesCommand : ICommand
{
[CommandParameter(13, Name = "param", Description = "Param description")]
public string? Parameter { get; set; }
[CommandOption("option", 'o', Description = "Option description", EnvironmentVariableName = "ENV")]
public string? Option { get; set; }
public string? HiddenA { get; set; }
public bool? HiddenB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
}
}

View File

@@ -1,14 +1,20 @@
using System;
using System.IO;
using CliFx.Domain;
using CliFx.Exceptions;
using System.Threading.Tasks;
using CliFx.Tests.Commands;
using CliFx.Tests.Commands.Invalid;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class ApplicationSpecs
public class ApplicationSpecs
{
private readonly ITestOutputHelper _output;
public ApplicationSpecs(ITestOutputHelper output) => _output = output;
[Fact]
public void Application_can_be_created_with_a_default_configuration()
{
@@ -26,10 +32,10 @@ namespace CliFx.Tests
{
// Act
var app = new CliApplicationBuilder()
.AddCommand(typeof(ValidCommand))
.AddCommandsFrom(typeof(ValidCommand).Assembly)
.AddCommands(new[] {typeof(ValidCommand)})
.AddCommandsFrom(new[] {typeof(ValidCommand).Assembly})
.AddCommand<DefaultCommand>()
.AddCommandsFrom(typeof(DefaultCommand).Assembly)
.AddCommands(new[] {typeof(DefaultCommand)})
.AddCommandsFrom(new[] {typeof(DefaultCommand).Assembly})
.AddCommandsFromThisAssembly()
.AllowDebugMode()
.AllowPreviewMode()
@@ -38,7 +44,7 @@ namespace CliFx.Tests
.UseVersionText("test")
.UseDescription("test")
.UseConsole(new VirtualConsole(Stream.Null))
.UseTypeActivator(Activator.CreateInstance)
.UseTypeActivator(Activator.CreateInstance!)
.Build();
// Assert
@@ -46,152 +52,364 @@ namespace CliFx.Tests
}
[Fact]
public void At_least_one_command_must_be_defined_in_an_application()
public async Task At_least_one_command_must_be_defined_in_an_application()
{
// Arrange
var commandTypes = Array.Empty<Type>();
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Commands_must_implement_the_corresponding_interface()
{
// Arrange
var commandTypes = new[] {typeof(NonImplementedCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Commands_must_be_annotated_by_an_attribute()
{
// Arrange
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Commands_must_have_unique_names()
{
// Arrange
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Command_parameters_must_have_unique_order()
{
// Arrange
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Command_parameters_must_have_unique_names()
{
// Arrange
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Command_parameter_can_be_non_scalar_only_if_no_other_such_parameter_is_present()
{
// Arrange
var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Command_parameter_can_be_non_scalar_only_if_it_is_the_last_in_order()
{
// Arrange
var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Command_options_must_have_unique_names()
{
// Arrange
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Command_options_must_have_unique_short_names()
{
// Arrange
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Command_options_must_have_unique_environment_variable_names()
{
// Arrange
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
// Act & assert
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
}
[Fact]
public void Command_options_and_parameters_must_be_annotated_by_corresponding_attributes()
{
// Arrange
var commandTypes = new[] {typeof(HiddenPropertiesCommand)};
var application = new CliApplicationBuilder()
.UseConsole(console)
.Build();
// Act
var schema = ApplicationSchema.Resolve(commandTypes);
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
schema.Should().BeEquivalentTo(new ApplicationSchema(new[]
{
new CommandSchema(
typeof(HiddenPropertiesCommand),
"hidden",
"Description",
new[]
{
new CommandParameterSchema(
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter)),
13,
"param",
"Param description")
},
new[]
{
new CommandOptionSchema(
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option)),
"option",
'o',
"ENV",
false,
"Option description")
})
}));
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
schema.ToString().Should().NotBeNullOrWhiteSpace(); // this is only for coverage, I'm sorry
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Commands_must_implement_the_corresponding_interface()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(NonImplementedCommand))
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Commands_must_be_annotated_by_an_attribute()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<NonAnnotatedCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Commands_must_have_unique_names()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<GenericExceptionCommand>()
.AddCommand<CommandExceptionCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_can_be_default_but_only_if_it_is_the_only_such_command()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DefaultCommand>()
.AddCommand<OtherDefaultCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_parameters_must_have_unique_order()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DuplicateParameterOrderCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_parameters_must_have_unique_names()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DuplicateParameterNameCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_parameter_can_be_non_scalar_only_if_no_other_such_parameter_is_present()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<MultipleNonScalarParametersCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_parameter_can_be_non_scalar_only_if_it_is_the_last_in_order()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<NonLastNonScalarParameterCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_parameter_custom_converter_must_implement_the_corresponding_interface()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<InvalidCustomConverterParameterCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_options_must_have_names_that_are_not_empty()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<EmptyOptionNameCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_options_must_have_names_that_are_longer_than_one_character()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<SingleCharacterOptionNameCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_options_must_have_unique_names()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DuplicateOptionNamesCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_options_must_have_unique_short_names()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DuplicateOptionShortNamesCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_options_must_have_unique_environment_variable_names()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DuplicateOptionEnvironmentVariableNamesCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_options_must_not_have_conflicts_with_the_implicit_help_option()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<ConflictWithHelpOptionCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_options_must_not_have_conflicts_with_the_implicit_version_option()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<ConflictWithVersionOptionCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_option_custom_converter_must_implement_the_corresponding_interface()
{
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<InvalidCustomConverterOptionCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().NotBe(0);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdErr.GetString());
}
}
}

View File

@@ -1,191 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests
{
public partial class ArgumentBindingSpecs
{
[Command]
private class AllSupportedTypesCommand : ICommand
{
[CommandOption(nameof(Object))]
public object? Object { get; set; } = 42;
[CommandOption(nameof(String))]
public string? String { get; set; } = "foo bar";
[CommandOption(nameof(Bool))]
public bool Bool { get; set; }
[CommandOption(nameof(Char))]
public char Char { get; set; }
[CommandOption(nameof(Sbyte))]
public sbyte Sbyte { get; set; }
[CommandOption(nameof(Byte))]
public byte Byte { get; set; }
[CommandOption(nameof(Short))]
public short Short { get; set; }
[CommandOption(nameof(Ushort))]
public ushort Ushort { get; set; }
[CommandOption(nameof(Int))]
public int Int { get; set; }
[CommandOption(nameof(Uint))]
public uint Uint { get; set; }
[CommandOption(nameof(Long))]
public long Long { get; set; }
[CommandOption(nameof(Ulong))]
public ulong Ulong { get; set; }
[CommandOption(nameof(Float))]
public float Float { get; set; }
[CommandOption(nameof(Double))]
public double Double { get; set; }
[CommandOption(nameof(Decimal))]
public decimal Decimal { get; set; }
[CommandOption(nameof(DateTime))]
public DateTime DateTime { get; set; }
[CommandOption(nameof(DateTimeOffset))]
public DateTimeOffset DateTimeOffset { get; set; }
[CommandOption(nameof(TimeSpan))]
public TimeSpan TimeSpan { get; set; }
[CommandOption(nameof(CustomEnum))]
public CustomEnum CustomEnum { get; set; }
[CommandOption(nameof(IntNullable))]
public int? IntNullable { get; set; }
[CommandOption(nameof(CustomEnumNullable))]
public CustomEnum? CustomEnumNullable { get; set; }
[CommandOption(nameof(TimeSpanNullable))]
public TimeSpan? TimeSpanNullable { get; set; }
[CommandOption(nameof(TestStringConstructable))]
public StringConstructable? TestStringConstructable { get; set; }
[CommandOption(nameof(TestStringParseable))]
public StringParseable? TestStringParseable { get; set; }
[CommandOption(nameof(TestStringParseableWithFormatProvider))]
public StringParseableWithFormatProvider? TestStringParseableWithFormatProvider { get; set; }
[CommandOption(nameof(ObjectArray))]
public object[]? ObjectArray { get; set; }
[CommandOption(nameof(StringArray))]
public string[]? StringArray { get; set; }
[CommandOption(nameof(IntArray))]
public int[]? IntArray { get; set; }
[CommandOption(nameof(CustomEnumArray))]
public CustomEnum[]? CustomEnumArray { get; set; }
[CommandOption(nameof(IntNullableArray))]
public int?[]? IntNullableArray { get; set; }
[CommandOption(nameof(TestStringConstructableArray))]
public StringConstructable[]? TestStringConstructableArray { get; set; }
[CommandOption(nameof(Enumerable))]
public IEnumerable? Enumerable { get; set; }
[CommandOption(nameof(StringEnumerable))]
public IEnumerable<string>? StringEnumerable { get; set; }
[CommandOption(nameof(StringReadOnlyList))]
public IReadOnlyList<string>? StringReadOnlyList { get; set; }
[CommandOption(nameof(StringList))]
public List<string>? StringList { get; set; }
[CommandOption(nameof(StringHashSet))]
public HashSet<string>? StringHashSet { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class ArrayOptionCommand : ICommand
{
[CommandOption("option", 'o')]
public IReadOnlyList<string>? Option { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class RequiredOptionCommand : ICommand
{
[CommandOption(nameof(OptionA))]
public string? OptionA { get; set; }
[CommandOption(nameof(OptionB), IsRequired = true)]
public string? OptionB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class ParametersCommand : ICommand
{
[CommandParameter(0)]
public string? ParameterA { get; set; }
[CommandParameter(1)]
public string? ParameterB { get; set; }
[CommandParameter(2)]
public IReadOnlyList<string>? ParameterC { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class UnsupportedPropertyTypeCommand : ICommand
{
[CommandOption(nameof(Option))]
public DummyType? Option { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class UnsupportedEnumerablePropertyTypeCommand : ICommand
{
[CommandOption(nameof(Option))]
public CustomEnumerable<string>? Option { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class NoParameterCommand : ICommand
{
[CommandOption(nameof(OptionA))]
public string? OptionA { get; set; }
[CommandOption(nameof(OptionB))]
public string? OptionB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
}
}

View File

@@ -1,64 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace CliFx.Tests
{
public partial class ArgumentBindingSpecs
{
private enum CustomEnum
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}
private class StringConstructable
{
public string Value { get; }
public StringConstructable(string value)
{
Value = value;
}
}
private class StringParseable
{
public string Value { get; }
private StringParseable(string value)
{
Value = value;
}
public static StringParseable Parse(string value) => new StringParseable(value);
}
private class StringParseableWithFormatProvider
{
public string Value { get; }
private StringParseableWithFormatProvider(string value)
{
Value = value;
}
public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
new StringParseableWithFormatProvider(value + " " + formatProvider);
}
private class DummyType
{
}
public class CustomEnumerable<T> : IEnumerable<T>
{
private readonly T[] _arr = new T[0];
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) _arr).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,315 +0,0 @@
using System;
using CliFx.Domain;
using FluentAssertions;
using Xunit;
namespace CliFx.Tests
{
public class ArgumentSyntaxSpecs
{
[Fact]
public void Input_is_empty_if_no_arguments_are_provided()
{
// Arrange
var args = Array.Empty<string>();
// Act
var input = CommandLineInput.Parse(args);
// Assert
input.Should().BeEquivalentTo(CommandLineInput.Empty);
}
public static object[][] DirectivesTestData => new[]
{
new object[]
{
new[] {"[preview]"},
new CommandLineInputBuilder()
.AddDirective("preview")
.Build()
},
new object[]
{
new[] {"[preview]", "[debug]"},
new CommandLineInputBuilder()
.AddDirective("preview")
.AddDirective("debug")
.Build()
}
};
[Theory]
[MemberData(nameof(DirectivesTestData))]
internal void Directive_can_be_enabled_by_specifying_its_name_in_square_brackets(string[] arguments, CommandLineInput expectedInput)
{
// Act
var input = CommandLineInput.Parse(arguments);
// Assert
input.Should().BeEquivalentTo(expectedInput);
}
public static object[][] OptionsTestData => new[]
{
new object[]
{
new[] {"--option"},
new CommandLineInputBuilder()
.AddOption("option")
.Build()
},
new object[]
{
new[] {"--option", "value"},
new CommandLineInputBuilder()
.AddOption("option", "value")
.Build()
},
new object[]
{
new[] {"--option", "value1", "value2"},
new CommandLineInputBuilder()
.AddOption("option", "value1", "value2")
.Build()
},
new object[]
{
new[] {"--option", "same value"},
new CommandLineInputBuilder()
.AddOption("option", "same value")
.Build()
},
new object[]
{
new[] {"--option1", "--option2"},
new CommandLineInputBuilder()
.AddOption("option1")
.AddOption("option2")
.Build()
},
new object[]
{
new[] {"--option1", "value1", "--option2", "value2"},
new CommandLineInputBuilder()
.AddOption("option1", "value1")
.AddOption("option2", "value2")
.Build()
},
new object[]
{
new[] {"--option1", "value1", "value2", "--option2", "value3", "value4"},
new CommandLineInputBuilder()
.AddOption("option1", "value1", "value2")
.AddOption("option2", "value3", "value4")
.Build()
},
new object[]
{
new[] {"--option1", "value1", "value2", "--option2"},
new CommandLineInputBuilder()
.AddOption("option1", "value1", "value2")
.AddOption("option2")
.Build()
}
};
[Theory]
[MemberData(nameof(OptionsTestData))]
internal void Option_can_be_set_by_specifying_its_name_after_two_dashes(string[] arguments, CommandLineInput expectedInput)
{
// Act
var input = CommandLineInput.Parse(arguments);
// Assert
input.Should().BeEquivalentTo(expectedInput);
}
public static object[][] ShortOptionsTestData => new[]
{
new object[]
{
new[] {"-o"},
new CommandLineInputBuilder()
.AddOption("o")
.Build()
},
new object[]
{
new[] {"-o", "value"},
new CommandLineInputBuilder()
.AddOption("o", "value")
.Build()
},
new object[]
{
new[] {"-o", "value1", "value2"},
new CommandLineInputBuilder()
.AddOption("o", "value1", "value2")
.Build()
},
new object[]
{
new[] {"-o", "same value"},
new CommandLineInputBuilder()
.AddOption("o", "same value")
.Build()
},
new object[]
{
new[] {"-a", "-b"},
new CommandLineInputBuilder()
.AddOption("a")
.AddOption("b")
.Build()
},
new object[]
{
new[] {"-a", "value1", "-b", "value2"},
new CommandLineInputBuilder()
.AddOption("a", "value1")
.AddOption("b", "value2")
.Build()
},
new object[]
{
new[] {"-a", "value1", "value2", "-b", "value3", "value4"},
new CommandLineInputBuilder()
.AddOption("a", "value1", "value2")
.AddOption("b", "value3", "value4")
.Build()
},
new object[]
{
new[] {"-a", "value1", "value2", "-b"},
new CommandLineInputBuilder()
.AddOption("a", "value1", "value2")
.AddOption("b")
.Build()
},
new object[]
{
new[] {"-abc"},
new CommandLineInputBuilder()
.AddOption("a")
.AddOption("b")
.AddOption("c")
.Build()
},
new object[]
{
new[] {"-abc", "value"},
new CommandLineInputBuilder()
.AddOption("a")
.AddOption("b")
.AddOption("c", "value")
.Build()
},
new object[]
{
new[] {"-abc", "value1", "value2"},
new CommandLineInputBuilder()
.AddOption("a")
.AddOption("b")
.AddOption("c", "value1", "value2")
.Build()
}
};
[Theory]
[MemberData(nameof(ShortOptionsTestData))]
internal void Option_can_be_set_by_specifying_its_short_name_after_a_single_dash(string[] arguments, CommandLineInput expectedInput)
{
// Act
var input = CommandLineInput.Parse(arguments);
// Assert
input.Should().BeEquivalentTo(expectedInput);
}
public static object[][] UnboundArgumentsTestData => new[]
{
new object[]
{
new[] {"foo"},
new CommandLineInputBuilder()
.AddUnboundArgument("foo")
.Build()
},
new object[]
{
new[] {"foo", "bar"},
new CommandLineInputBuilder()
.AddUnboundArgument("foo")
.AddUnboundArgument("bar")
.Build()
},
new object[]
{
new[] {"[preview]", "foo"},
new CommandLineInputBuilder()
.AddDirective("preview")
.AddUnboundArgument("foo")
.Build()
},
new object[]
{
new[] {"foo", "--option", "value", "-abc"},
new CommandLineInputBuilder()
.AddUnboundArgument("foo")
.AddOption("option", "value")
.AddOption("a")
.AddOption("b")
.AddOption("c")
.Build()
},
new object[]
{
new[] {"[preview]", "[debug]", "foo", "bar", "--option", "value", "-abc"},
new CommandLineInputBuilder()
.AddDirective("preview")
.AddDirective("debug")
.AddUnboundArgument("foo")
.AddUnboundArgument("bar")
.AddOption("option", "value")
.AddOption("a")
.AddOption("b")
.AddOption("c")
.Build()
}
};
[Theory]
[MemberData(nameof(UnboundArgumentsTestData))]
internal void Any_remaining_arguments_are_treated_as_unbound_arguments(string[] arguments, CommandLineInput expectedInput)
{
// Act
var input = CommandLineInput.Parse(arguments);
// Assert
input.Should().BeEquivalentTo(expectedInput);
}
}
}

View File

@@ -1,27 +0,0 @@
using System;
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests
{
public partial class CancellationSpecs
{
[Command("cancel")]
private class CancellableCommand : ICommand
{
public async ValueTask ExecuteAsync(IConsole console)
{
try
{
await Task.Delay(TimeSpan.FromSeconds(3), console.GetCancellationToken());
console.Output.WriteLine("Never printed");
}
catch (OperationCanceledException)
{
console.Output.WriteLine("Cancellation requested");
throw;
}
}
}
}
}

View File

@@ -1,41 +1,36 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CliFx.Tests.Commands;
using FluentAssertions;
using Xunit;
namespace CliFx.Tests
{
public partial class CancellationSpecs
public class CancellationSpecs
{
[Fact]
public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested()
{
// Can't test it with a real console because CliWrap can't send Ctrl+C
// Arrange
using var cts = new CancellationTokenSource();
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut, cancellationToken: cts.Token);
var (console, stdOut, _) = VirtualConsole.CreateBuffered(cts.Token);
var application = new CliApplicationBuilder()
.AddCommand(typeof(CancellableCommand))
.AddCommand<CancellableCommand>()
.UseConsole(console)
.Build();
// Act
cts.CancelAfter(TimeSpan.FromSeconds(0.2));
var exitCode = await application.RunAsync(
new[] {"cancel"},
new Dictionary<string, string>());
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
var exitCode = await application.RunAsync(new[] {"cmd"});
// Assert
exitCode.Should().NotBe(0);
stdOutData.Should().Be("Cancellation requested");
stdOut.GetString().Trim().Should().Be(CancellableCommand.CancellationOutputText);
}
}
}

View File

@@ -15,12 +15,14 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CliWrap" Version="3.0.0" />
<PackageReference Include="FluentAssertions" Version="5.10.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
<PackageReference Include="CliWrap" Version="3.2.0" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
<PackageReference Include="coverlet.msbuild" Version="2.9.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,31 @@
using System;
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class CancellableCommand : ICommand
{
public const string CompletionOutputText = "Finished";
public const string CancellationOutputText = "Canceled";
public async ValueTask ExecuteAsync(IConsole console)
{
try
{
await Task.Delay(
TimeSpan.FromSeconds(3),
console.GetCancellationToken()
);
console.Output.WriteLine(CompletionOutputText);
}
catch (OperationCanceledException)
{
console.Output.WriteLine(CancellationOutputText);
throw;
}
}
}
}

View File

@@ -0,0 +1,21 @@
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Exceptions;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class CommandExceptionCommand : ICommand
{
[CommandOption("code", 'c')]
public int ExitCode { get; set; } = 133;
[CommandOption("msg", 'm')]
public string? Message { get; set; }
[CommandOption("show-help")]
public bool ShowHelp { get; set; }
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode, ShowHelp);
}
}

View File

@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command(Description = "Default command description")]
public class DefaultCommand : ICommand
{
public const string ExpectedOutputText = nameof(DefaultCommand);
public ValueTask ExecuteAsync(IConsole console)
{
console.Output.WriteLine(ExpectedOutputText);
return default;
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class GenericExceptionCommand : ICommand
{
[CommandOption("msg", 'm')]
public string? Message { get; set; }
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class GenericInnerExceptionCommand : ICommand
{
[CommandOption("msg", 'm')]
public string? Message { get; set; }
[CommandOption("inner-msg", 'i')]
public string? InnerMessage { get; set; }
public ValueTask ExecuteAsync(IConsole console) =>
throw new Exception(Message, new Exception(InnerMessage));
}
}

View File

@@ -0,0 +1,11 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class ConflictWithHelpOptionCommand : SelfSerializeCommandBase
{
[CommandOption("option-h", 'h')]
public string? OptionH { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
// Must be default because version option is available only on default commands
[Command]
public class ConflictWithVersionOptionCommand : SelfSerializeCommandBase
{
[CommandOption("version")]
public string? Version { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class DuplicateOptionEnvironmentVariableNamesCommand : SelfSerializeCommandBase
{
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
public string? OptionA { get; set; }
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
public string? OptionB { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class DuplicateOptionNamesCommand : SelfSerializeCommandBase
{
[CommandOption("fruits")]
public string? Apples { get; set; }
[CommandOption("fruits")]
public string? Oranges { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class DuplicateOptionShortNamesCommand : SelfSerializeCommandBase
{
[CommandOption('x')]
public string? OptionA { get; set; }
[CommandOption('x')]
public string? OptionB { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class DuplicateParameterNameCommand : SelfSerializeCommandBase
{
[CommandParameter(0, Name = "param")]
public string? ParamA { get; set; }
[CommandParameter(1, Name = "param")]
public string? ParamB { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class DuplicateParameterOrderCommand : SelfSerializeCommandBase
{
[CommandParameter(13)]
public string? ParamA { get; set; }
[CommandParameter(13)]
public string? ParamB { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class EmptyOptionNameCommand : SelfSerializeCommandBase
{
[CommandOption("")]
public string? Apples { get; set; }
}
}

View File

@@ -0,0 +1,16 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command]
public class InvalidCustomConverterOptionCommand : SelfSerializeCommandBase
{
[CommandOption('f', Converter = typeof(Converter))]
public string? Option { get; set; }
public class Converter
{
public object ConvertFrom(string value) => value;
}
}
}

View File

@@ -0,0 +1,16 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command]
public class InvalidCustomConverterParameterCommand : SelfSerializeCommandBase
{
[CommandParameter(0, Converter = typeof(Converter))]
public string? Param { get; set; }
public class Converter
{
public object ConvertFrom(string value) => value;
}
}
}

View File

@@ -0,0 +1,15 @@
using System.Collections.Generic;
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class MultipleNonScalarParametersCommand : SelfSerializeCommandBase
{
[CommandParameter(0)]
public IReadOnlyList<string>? ParamA { get; set; }
[CommandParameter(1)]
public IReadOnlyList<string>? ParamB { get; set; }
}
}

View File

@@ -0,0 +1,6 @@
namespace CliFx.Tests.Commands.Invalid
{
public class NonAnnotatedCommand : SelfSerializeCommandBase
{
}
}

View File

@@ -0,0 +1,9 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command]
public class NonImplementedCommand
{
}
}

View File

@@ -0,0 +1,15 @@
using System.Collections.Generic;
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class NonLastNonScalarParameterCommand : SelfSerializeCommandBase
{
[CommandParameter(0)]
public IReadOnlyList<string>? ParamA { get; set; }
[CommandParameter(1)]
public string? ParamB { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command]
public class OtherDefaultCommand : SelfSerializeCommandBase
{
}
}

View File

@@ -0,0 +1,11 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands.Invalid
{
[Command("cmd")]
public class SingleCharacterOptionNameCommand : SelfSerializeCommandBase
{
[CommandOption("a")]
public string? Apples { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("named", Description = "Named command description")]
public class NamedCommand : ICommand
{
public const string ExpectedOutputText = nameof(NamedCommand);
public ValueTask ExecuteAsync(IConsole console)
{
console.Output.WriteLine(ExpectedOutputText);
return default;
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("named sub", Description = "Named sub command description")]
public class NamedSubCommand : ICommand
{
public const string ExpectedOutputText = nameof(NamedSubCommand);
public ValueTask ExecuteAsync(IConsole console)
{
console.Output.WriteLine(ExpectedOutputText);
return default;
}
}
}

View File

@@ -0,0 +1,14 @@
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace CliFx.Tests.Commands
{
public abstract class SelfSerializeCommandBase : ICommand
{
public ValueTask ExecuteAsync(IConsole console)
{
console.Output.WriteLine(JsonConvert.SerializeObject(this));
return default;
}
}
}

View File

@@ -0,0 +1,174 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using CliFx.Attributes;
using Newtonsoft.Json;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public partial class SupportedArgumentTypesCommand : SelfSerializeCommandBase
{
[CommandOption("obj")]
public object? Object { get; set; } = 42;
[CommandOption("str")]
public string? String { get; set; } = "foo bar";
[CommandOption("bool")]
public bool Bool { get; set; }
[CommandOption("char")]
public char Char { get; set; }
[CommandOption("sbyte")]
public sbyte Sbyte { get; set; }
[CommandOption("byte")]
public byte Byte { get; set; }
[CommandOption("short")]
public short Short { get; set; }
[CommandOption("ushort")]
public ushort Ushort { get; set; }
[CommandOption("int")]
public int Int { get; set; }
[CommandOption("uint")]
public uint Uint { get; set; }
[CommandOption("long")]
public long Long { get; set; }
[CommandOption("ulong")]
public ulong Ulong { get; set; }
[CommandOption("float")]
public float Float { get; set; }
[CommandOption("double")]
public double Double { get; set; }
[CommandOption("decimal")]
public decimal Decimal { get; set; }
[CommandOption("datetime")]
public DateTime DateTime { get; set; }
[CommandOption("datetime-offset")]
public DateTimeOffset DateTimeOffset { get; set; }
[CommandOption("timespan")]
public TimeSpan TimeSpan { get; set; }
[CommandOption("enum")]
public CustomEnum Enum { get; set; }
[CommandOption("int-nullable")]
public int? IntNullable { get; set; }
[CommandOption("enum-nullable")]
public CustomEnum? EnumNullable { get; set; }
[CommandOption("timespan-nullable")]
public TimeSpan? TimeSpanNullable { get; set; }
[CommandOption("str-constructible")]
public CustomStringConstructible? StringConstructible { get; set; }
[CommandOption("str-parseable")]
public CustomStringParseable? StringParseable { get; set; }
[CommandOption("str-parseable-format")]
public CustomStringParseableWithFormatProvider? StringParseableWithFormatProvider { get; set; }
[CommandOption("convertible", Converter = typeof(CustomConvertibleConverter))]
public CustomConvertible? Convertible { get; set; }
[CommandOption("obj-array")]
public object[]? ObjectArray { get; set; }
[CommandOption("str-array")]
public string[]? StringArray { get; set; }
[CommandOption("int-array")]
public int[]? IntArray { get; set; }
[CommandOption("enum-array")]
public CustomEnum[]? EnumArray { get; set; }
[CommandOption("int-nullable-array")]
public int?[]? IntNullableArray { get; set; }
[CommandOption("str-constructible-array")]
public CustomStringConstructible[]? StringConstructibleArray { get; set; }
[CommandOption("convertible-array", Converter = typeof(CustomConvertibleConverter))]
public CustomConvertible[]? ConvertibleArray { get; set; }
[CommandOption("str-enumerable")]
public IEnumerable<string>? StringEnumerable { get; set; }
[CommandOption("str-read-only-list")]
public IReadOnlyList<string>? StringReadOnlyList { get; set; }
[CommandOption("str-list")]
public List<string>? StringList { get; set; }
[CommandOption("str-set")]
public HashSet<string>? StringHashSet { get; set; }
}
public partial class SupportedArgumentTypesCommand
{
public enum CustomEnum
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}
public class CustomStringConstructible
{
public string Value { get; }
public CustomStringConstructible(string value) => Value = value;
}
public class CustomStringParseable
{
public string Value { get; }
[JsonConstructor]
private CustomStringParseable(string value) => Value = value;
public static CustomStringParseable Parse(string value) => new CustomStringParseable(value);
}
public class CustomStringParseableWithFormatProvider
{
public string Value { get; }
[JsonConstructor]
private CustomStringParseableWithFormatProvider(string value) => Value = value;
public static CustomStringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
new CustomStringParseableWithFormatProvider(value + " " + formatProvider);
}
public class CustomConvertible
{
public int Value { get; }
public CustomConvertible(int value) => Value = value;
}
public class CustomConvertibleConverter : IArgumentValueConverter
{
public object ConvertFrom(string value) =>
new CustomConvertible(int.Parse(value, CultureInfo.InvariantCulture));
}
}
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections;
using System.Collections.Generic;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public partial class UnsupportedArgumentTypesCommand : SelfSerializeCommandBase
{
[CommandOption("custom")]
public CustomType? CustomNonConvertible { get; set; }
[CommandOption("custom-enumerable")]
public CustomEnumerable<string>? CustomEnumerableNonConvertible { get; set; }
}
public partial class UnsupportedArgumentTypesCommand
{
public class CustomType
{
}
public class CustomEnumerable<T> : IEnumerable<T>
{
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class WithDefaultValuesCommand : SelfSerializeCommandBase
{
public enum CustomEnum { Value1, Value2, Value3 };
[CommandOption("obj")]
public object? Object { get; set; } = 42;
[CommandOption("str")]
public string? String { get; set; } = "foo";
[CommandOption("str-empty")]
public string StringEmpty { get; set; } = "";
[CommandOption("str-array")]
public string[]? StringArray { get; set; } = { "foo", "bar", "baz" };
[CommandOption("bool")]
public bool Bool { get; set; } = true;
[CommandOption("char")]
public char Char { get; set; } = 't';
[CommandOption("int")]
public int Int { get; set; } = 1337;
[CommandOption("int-nullable")]
public int? IntNullable { get; set; } = 1337;
[CommandOption("int-array")]
public int[]? IntArray { get; set; } = { 1, 2, 3 };
[CommandOption("timespan")]
public TimeSpan TimeSpan { get; set; } = TimeSpan.FromMinutes(123);
[CommandOption("enum")]
public CustomEnum Enum { get; set; } = CustomEnum.Value2;
}
}

View File

@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class WithDependenciesCommand : ICommand
{
public class DependencyA
{
}
public class DependencyB
{
}
private readonly DependencyA _dependencyA;
private readonly DependencyB _dependencyB;
public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB)
{
_dependencyA = dependencyA;
_dependencyB = dependencyB;
}
public ValueTask ExecuteAsync(IConsole console) => default;
}
}

View File

@@ -0,0 +1,19 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class WithEnumArgumentsCommand : SelfSerializeCommandBase
{
public enum CustomEnum { Value1, Value2, Value3 };
[CommandParameter(0, Name = "enum")]
public CustomEnum EnumParameter { get; set; }
[CommandOption("enum")]
public CustomEnum? EnumOption { get; set; }
[CommandOption("required-enum", IsRequired = true)]
public CustomEnum RequiredEnumOption { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
using System.Collections.Generic;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class WithEnvironmentVariablesCommand : SelfSerializeCommandBase
{
[CommandOption("opt-a", 'a', EnvironmentVariableName = "ENV_OPT_A")]
public string? OptA { get; set; }
[CommandOption("opt-b", 'b', EnvironmentVariableName = "ENV_OPT_B")]
public IReadOnlyList<string>? OptB { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class WithParametersCommand : SelfSerializeCommandBase
{
[CommandParameter(0)]
public string? ParamA { get; set; }
[CommandParameter(1)]
public int? ParamB { get; set; }
[CommandParameter(2)]
public IReadOnlyList<string>? ParamC { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class WithRequiredOptionsCommand : SelfSerializeCommandBase
{
[CommandOption("opt-a", 'a', IsRequired = true)]
public string? OptA { get; set; }
[CommandOption("opt-b", 'b')]
public int? OptB { get; set; }
[CommandOption("opt-c", 'c', IsRequired = true)]
public IReadOnlyList<char>? OptC { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class WithSingleParameterCommand : SelfSerializeCommandBase
{
[CommandParameter(0)]
public string? ParamA { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class WithSingleRequiredOptionCommand : SelfSerializeCommandBase
{
[CommandOption("opt-a")]
public string? OptA { get; set; }
[CommandOption("opt-b", IsRequired = true)]
public string? OptB { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
using System.Collections.Generic;
using CliFx.Attributes;
namespace CliFx.Tests.Commands
{
[Command("cmd")]
public class WithStringArrayOptionCommand : SelfSerializeCommandBase
{
[CommandOption("opt", 'o')]
public IReadOnlyList<string>? Opt { get; set; }
}
}

View File

@@ -38,7 +38,8 @@ namespace CliFx.Tests
var console = new VirtualConsole(
input: stdIn,
output: stdOut,
error: stdErr);
error: stdErr
);
// Act
console.Output.Write("output");
@@ -51,6 +52,8 @@ namespace CliFx.Tests
console.ResetColor();
console.ForegroundColor = ConsoleColor.DarkMagenta;
console.BackgroundColor = ConsoleColor.DarkMagenta;
console.CursorLeft = 42;
console.CursorTop = 24;
// Assert
stdInData.Should().Be("input");

View File

@@ -1,37 +0,0 @@
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests
{
public partial class DependencyInjectionSpecs
{
[Command]
private class WithoutDependenciesCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}
private class DependencyA
{
}
private class DependencyB
{
}
[Command]
private class WithDependenciesCommand : ICommand
{
private readonly DependencyA _dependencyA;
private readonly DependencyB _dependencyB;
public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB)
{
_dependencyA = dependencyA;
_dependencyB = dependencyB;
}
public ValueTask ExecuteAsync(IConsole console) => default;
}
}
}

View File

@@ -1,11 +1,17 @@
using CliFx.Exceptions;
using CliFx.Tests.Commands;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class DependencyInjectionSpecs
public 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()
{
@@ -13,10 +19,10 @@ namespace CliFx.Tests
var activator = new DefaultTypeActivator();
// Act
var obj = activator.CreateInstance(typeof(WithoutDependenciesCommand));
var obj = activator.CreateInstance(typeof(DefaultCommand));
// Assert
obj.Should().BeOfType<WithoutDependenciesCommand>();
obj.Should().BeOfType<DefaultCommand>();
}
[Fact]
@@ -26,8 +32,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]
@@ -35,7 +41,10 @@ namespace CliFx.Tests
{
// Arrange
var activator = new DelegateTypeActivator(_ =>
new WithDependenciesCommand(new DependencyA(), new DependencyB()));
new WithDependenciesCommand(
new WithDependenciesCommand.DependencyA(),
new WithDependenciesCommand.DependencyB())
);
// Act
var obj = activator.CreateInstance(typeof(WithDependenciesCommand));
@@ -48,11 +57,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

@@ -1,14 +0,0 @@
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests
{
public partial class DirectivesSpecs
{
[Command("cmd")]
private class NamedCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}
}
}

View File

@@ -1,36 +1,44 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using CliFx.Tests.Commands;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class DirectivesSpecs
public class DirectivesSpecs
{
private readonly ITestOutputHelper _output;
public DirectivesSpecs(ITestOutputHelper output) => _output = output;
[Fact]
public async Task Preview_directive_can_be_enabled_to_print_provided_arguments_as_they_were_parsed()
public async Task Preview_directive_can_be_specified_to_print_provided_arguments_as_they_were_parsed()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(NamedCommand))
.AddCommand<NamedCommand>()
.UseConsole(console)
.AllowPreviewMode()
.Build();
// Act
var exitCode = await application.RunAsync(
new[] {"[preview]", "cmd", "param", "-abc", "--option", "foo"},
new Dictionary<string, string>());
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
new[] {"[preview]", "named", "param", "-abc", "--option", "foo"},
new Dictionary<string, string>()
);
// Assert
exitCode.Should().Be(0);
stdOutData.Should().ContainAll("cmd", "<param>", "[-a]", "[-b]", "[-c]", "[--option foo]");
stdOut.GetString().Should().ContainAll(
"named", "<param>", "[-a]", "[-b]", "[-c]", "[--option \"foo\"]"
);
_output.WriteLine(stdOut.GetString());
}
}
}

View File

@@ -1,27 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests
{
public partial class EnvironmentVariablesSpecs
{
[Command]
private class EnvironmentVariableCollectionCommand : ICommand
{
[CommandOption("opt", EnvironmentVariableName = "ENV_OPT")]
public IReadOnlyList<string>? Option { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command]
private class EnvironmentVariableCommand : ICommand
{
[CommandOption("opt", EnvironmentVariableName = "ENV_OPT")]
public string? Option { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
}
}

View File

@@ -1,7 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using CliFx.Domain;
using CliFx.Tests.Commands;
using CliFx.Tests.Internal;
using CliWrap;
using CliWrap.Buffered;
using FluentAssertions;
@@ -9,11 +10,11 @@ using Xunit;
namespace CliFx.Tests
{
public partial class EnvironmentVariablesSpecs
public class EnvironmentVariablesSpecs
{
// This test uses a real application to make sure environment variables are actually read correctly
[Fact]
public async Task Option_can_use_a_specific_environment_variable_as_fallback()
public async Task Option_can_use_an_environment_variable_as_fallback()
{
// Arrange
var command = Cli.Wrap("dotnet")
@@ -26,12 +27,12 @@ namespace CliFx.Tests
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
// Assert
stdOut.TrimEnd().Should().Be("Hello Mars!");
stdOut.Trim().Should().Be("Hello Mars!");
}
// This test uses a real application to make sure environment variables are actually read correctly
[Fact]
public async Task Option_only_uses_environment_variable_as_fallback_if_the_value_was_not_directly_provided()
public async Task Option_only_uses_an_environment_variable_as_fallback_if_the_value_is_not_directly_provided()
{
// Arrange
var command = Cli.Wrap("dotnet")
@@ -46,50 +47,94 @@ namespace CliFx.Tests
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
// Assert
stdOut.TrimEnd().Should().Be("Hello Jupiter!");
stdOut.Trim().Should().Be("Hello Jupiter!");
}
[Fact]
public void Option_of_non_scalar_type_can_take_multiple_separated_values_from_an_environment_variable()
public async Task Option_only_uses_an_environment_variable_as_fallback_if_the_name_matches_case_sensitively()
{
// Arrange
var schema = ApplicationSchema.Resolve(new[] {typeof(EnvironmentVariableCollectionCommand)});
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var input = CommandLineInput.Empty;
var envVars = new Dictionary<string, string>
{
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
};
var application = new CliApplicationBuilder()
.AddCommand<WithEnvironmentVariablesCommand>()
.UseConsole(console)
.Build();
// Act
var command = schema.InitializeEntryPoint(input, envVars);
var exitCode = await application.RunAsync(
new[] {"cmd"},
new Dictionary<string, string>
{
["ENV_opt_A"] = "incorrect",
["ENV_OPT_A"] = "correct"
}
);
var commandInstance = stdOut.GetString().DeserializeJson<WithEnvironmentVariablesCommand>();
// Assert
command.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand
exitCode.Should().Be(0);
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
{
Option = new[] {"foo", "bar"}
OptA = "correct"
});
}
[Fact]
public void Option_of_scalar_type_can_only_take_a_single_value_from_an_environment_variable_even_if_it_contains_separators()
public async Task Option_of_non_scalar_type_can_use_an_environment_variable_as_fallback_and_extract_multiple_values()
{
// Arrange
var schema = ApplicationSchema.Resolve(new[] {typeof(EnvironmentVariableCommand)});
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var input = CommandLineInput.Empty;
var envVars = new Dictionary<string, string>
{
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
};
var application = new CliApplicationBuilder()
.AddCommand<WithEnvironmentVariablesCommand>()
.UseConsole(console)
.Build();
// Act
var command = schema.InitializeEntryPoint(input, envVars);
var exitCode = await application.RunAsync(
new[] {"cmd"},
new Dictionary<string, string>
{
["ENV_OPT_B"] = $"foo{Path.PathSeparator}bar"
}
);
var commandInstance = stdOut.GetString().DeserializeJson<WithEnvironmentVariablesCommand>();
// Assert
command.Should().BeEquivalentTo(new EnvironmentVariableCommand
exitCode.Should().Be(0);
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
{
Option = $"foo{Path.PathSeparator}bar"
OptB = new[] {"foo", "bar"}
});
}
[Fact]
public async Task Option_of_scalar_type_can_use_an_environment_variable_as_fallback_regardless_of_separators()
{
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<WithEnvironmentVariablesCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(
new[] {"cmd"},
new Dictionary<string, string>
{
["ENV_OPT_A"] = $"foo{Path.PathSeparator}bar"
}
);
var commandInstance = stdOut.GetString().DeserializeJson<WithEnvironmentVariablesCommand>();
// Assert
exitCode.Should().Be(0);
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
{
OptA = $"foo{Path.PathSeparator}bar"
});
}
}

View File

@@ -1,31 +0,0 @@
using System;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Exceptions;
namespace CliFx.Tests
{
public partial class ErrorReportingSpecs
{
[Command("exc")]
private class GenericExceptionCommand : ICommand
{
[CommandOption("msg", 'm')]
public string? Message { get; set; }
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
}
[Command("exc")]
private class CommandExceptionCommand : ICommand
{
[CommandOption("code", 'c')]
public int ExitCode { get; set; } = 1337;
[CommandOption("msg", 'm')]
public string? Message { get; set; }
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
}
}
}

View File

@@ -1,84 +1,174 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Tests.Commands;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class ErrorReportingSpecs
public 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();
var console = new VirtualConsole(error: stdErr);
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(GenericExceptionCommand))
.AddCommand<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();
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput"});
// Assert
exitCode.Should().NotBe(0);
stdErrData.Should().Contain("Kaput");
stdErrData.Length.Should().BeGreaterThan("Kaput".Length);
stdOut.GetString().Should().BeEmpty();
stdErr.GetString().Should().ContainAll(
"System.Exception:",
"Kaput", "at",
"CliFx.Tests"
);
_output.WriteLine(stdOut.GetString());
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_may_throw_a_generic_exception_with_inner_exception_which_exits_and_prints_error_message_and_stack_trace()
{
// Arrange
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<GenericInnerExceptionCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "-i", "FooBar"});
// Assert
exitCode.Should().NotBe(0);
stdOut.GetString().Should().BeEmpty();
stdErr.GetString().Should().ContainAll(
"System.Exception:",
"FooBar",
"Kaput", "at",
"CliFx.Tests"
);
_output.WriteLine(stdOut.GetString());
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_may_throw_a_specialized_exception_which_exits_with_custom_code_and_prints_minimal_error_details()
{
// Arrange
await using var stdErr = new MemoryStream();
var console = new VirtualConsole(error: stdErr);
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(CommandExceptionCommand))
.AddCommand<CommandExceptionCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(
new[] {"exc", "-m", "Kaput", "-c", "69"},
new Dictionary<string, string>());
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "-c", "69"});
// Assert
exitCode.Should().Be(69);
stdErrData.Should().Be("Kaput");
stdOut.GetString().Should().BeEmpty();
stdErr.GetString().Trim().Should().Be("Kaput");
_output.WriteLine(stdOut.GetString());
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_may_throw_a_specialized_exception_without_error_message_which_exits_and_prints_full_error_details()
{
// Arrange
await using var stdErr = new MemoryStream();
var console = new VirtualConsole(error: stdErr);
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(CommandExceptionCommand))
.AddCommand<CommandExceptionCommand>()
.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();
var exitCode = await application.RunAsync(new[] {"cmd"});
// Assert
exitCode.Should().NotBe(0);
stdErrData.Should().NotBeEmpty();
stdOut.GetString().Should().BeEmpty();
stdErr.GetString().Should().ContainAll(
"CliFx.Exceptions.CommandException:",
"at",
"CliFx.Tests"
);
_output.WriteLine(stdOut.GetString());
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_may_throw_a_specialized_exception_which_exits_and_prints_help_text()
{
// Arrange
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<CommandExceptionCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "--show-help"});
// Assert
exitCode.Should().NotBe(0);
stdOut.GetString().Should().ContainAll(
"Usage",
"Options",
"-h|--help"
);
stdErr.GetString().Trim().Should().Be("Kaput");
_output.WriteLine(stdOut.GetString());
_output.WriteLine(stdErr.GetString());
}
[Fact]
public async Task Command_shows_help_text_on_invalid_user_input()
{
// Arrange
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DefaultCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"not-a-valid-command", "-r", "foo"});
// Assert
exitCode.Should().NotBe(0);
stdOut.GetString().Should().ContainAll(
"Usage",
"Options",
"-h|--help"
);
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
_output.WriteLine(stdOut.GetString());
_output.WriteLine(stdErr.GetString());
}
}
}

View File

@@ -1,96 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests
{
public partial class HelpTextSpecs
{
[Command(Description = "DefaultCommand description.")]
private class DefaultCommand : ICommand
{
[CommandOption("option-a", 'a', Description = "OptionA description.")]
public string? OptionA { get; set; }
[CommandOption("option-b", 'b', Description = "OptionB description.")]
public string? OptionB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("cmd", Description = "NamedCommand description.")]
private class NamedCommand : ICommand
{
[CommandParameter(0, Name = "param-a", Description = "ParameterA description.")]
public string? ParameterA { get; set; }
[CommandOption("option-c", 'c', Description = "OptionC description.")]
public string? OptionC { get; set; }
[CommandOption("option-d", 'd', Description = "OptionD description.")]
public string? OptionD { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("cmd sub", Description = "NamedSubCommand description.")]
private class NamedSubCommand : ICommand
{
[CommandParameter(0, Name = "param-b", Description = "ParameterB description.")]
public string? ParameterB { get; set; }
[CommandParameter(1, Name = "param-c", Description = "ParameterC description.")]
public string? ParameterC { get; set; }
[CommandOption("option-e", 'e', Description = "OptionE description.")]
public string? OptionE { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("cmd-with-params")]
private class ParametersCommand : ICommand
{
[CommandParameter(0, Name = "first")]
public string? ParameterA { get; set; }
[CommandParameter(10)]
public int? ParameterB { get; set; }
[CommandParameter(20, Description = "A list of numbers", Name = "third list")]
public IEnumerable<int>? ParameterC { get; set; }
[CommandOption("option", 'o')]
public string? Option { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("cmd-with-req-opts")]
private class RequiredOptionsCommand : ICommand
{
[CommandOption("option-f", 'f', IsRequired = true)]
public string? OptionF { get; set; }
[CommandOption("option-g", 'g', IsRequired = true)]
public IEnumerable<int>? OptionG { get; set; }
[CommandOption("option-h", 'h')]
public string? OptionH { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
[Command("cmd-with-env-vars")]
private class EnvironmentVariableCommand : ICommand
{
[CommandOption("option-a", 'a', IsRequired = true, EnvironmentVariableName = "ENV_OPT_A")]
public string? OptionA { get; set; }
[CommandOption("option-b", 'b', EnvironmentVariableName = "ENV_OPT_B")]
public string? OptionB { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
}
}

View File

@@ -1,246 +1,180 @@
using System.IO;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Tests.Commands;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class HelpTextSpecs
public class HelpTextSpecs
{
[Fact]
public async Task Version_information_can_be_requested_by_providing_the_version_option_without_other_arguments()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
private readonly ITestOutputHelper _output;
var application = new CliApplicationBuilder()
.AddCommand(typeof(DefaultCommand))
.AddCommand(typeof(NamedCommand))
.AddCommand(typeof(NamedSubCommand))
.UseVersionText("v6.9")
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"--version"});
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
// Assert
exitCode.Should().Be(0);
stdOutData.Should().Be("v6.9");
}
[Fact]
public async Task Help_text_can_be_requested_by_providing_the_help_option()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var application = new CliApplicationBuilder()
.AddCommand(typeof(DefaultCommand))
.AddCommand(typeof(NamedCommand))
.AddCommand(typeof(NamedSubCommand))
.UseTitle("AppTitle")
.UseVersionText("AppVer")
.UseDescription("AppDesc")
.UseExecutableName("AppExe")
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] {"--help"});
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
// Assert
stdOutData.Should().ContainAll(
"AppTitle", "AppVer",
"AppDesc",
"Usage",
"AppExe", "[command]", "[options]",
"Options",
"-a|--option-a", "OptionA description.",
"-b|--option-b", "OptionB description.",
"-h|--help", "Shows help text.",
"--version", "Shows version information.",
"Commands",
"cmd", "NamedCommand description.",
"You can run", "to show help on a specific command."
);
}
[Fact]
public async Task Help_text_can_be_requested_on_a_specific_named_command()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var application = new CliApplicationBuilder()
.AddCommand(typeof(DefaultCommand))
.AddCommand(typeof(NamedCommand))
.AddCommand(typeof(NamedSubCommand))
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] {"cmd", "--help"});
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
// Assert
stdOutData.Should().ContainAll(
"Description",
"NamedCommand description.",
"Usage",
"cmd", "[command]", "<param-a>", "[options]",
"Parameters",
"* param-a", "ParameterA description.",
"Options",
"-c|--option-c", "OptionC description.",
"-d|--option-d", "OptionD description.",
"-h|--help", "Shows help text.",
"Commands",
"sub", "SubCommand description.",
"You can run", "to show help on a specific command."
);
}
[Fact]
public async Task Help_text_can_be_requested_on_a_specific_named_sub_command()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var application = new CliApplicationBuilder()
.AddCommand(typeof(DefaultCommand))
.AddCommand(typeof(NamedCommand))
.AddCommand(typeof(NamedSubCommand))
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] {"cmd", "sub", "--help"});
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
// Assert
stdOutData.Should().ContainAll(
"Description",
"SubCommand description.",
"Usage",
"cmd sub", "<param-b>", "<param-c>", "[options]",
"Parameters",
"* param-b", "ParameterB description.",
"* param-c", "ParameterC description.",
"Options",
"-e|--option-e", "OptionE description.",
"-h|--help", "Shows help text."
);
}
[Fact]
public async Task Help_text_can_be_requested_without_specifying_command_even_if_default_command_is_not_defined()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var application = new CliApplicationBuilder()
.AddCommand(typeof(NamedCommand))
.AddCommand(typeof(NamedSubCommand))
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] {"--help"});
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
// Assert
stdOutData.Should().ContainAll(
"Usage",
"[command]",
"Options",
"-h|--help", "Shows help text.",
"--version", "Shows version information.",
"Commands",
"cmd", "NamedCommand description.",
"You can run", "to show help on a specific command."
);
}
public HelpTextSpecs(ITestOutputHelper output) => _output = output;
[Fact]
public async Task Help_text_shows_usage_format_which_lists_all_parameters()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(ParametersCommand))
.AddCommand<WithParametersCommand>()
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] {"cmd-with-params", "--help"});
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
// Assert
stdOutData.Should().ContainAll(
exitCode.Should().Be(0);
stdOut.GetString().Should().ContainAll(
"Usage",
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
"cmd", "<parama>", "<paramb>", "<paramc...>"
);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_shows_usage_format_which_lists_all_required_options()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(RequiredOptionsCommand))
.AddCommand<WithRequiredOptionsCommand>()
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] {"cmd-with-req-opts", "--help"});
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
// Assert
stdOutData.Should().ContainAll(
exitCode.Should().Be(0);
stdOut.GetString().Should().ContainAll(
"Usage",
"cmd-with-req-opts", "--option-f <value>", "--option-g <values...>", "[options]",
"cmd", "--opt-a <value>", "--opt-c <values...>", "[options]",
"Options",
"* -f|--option-f",
"* -g|--option-g",
"-h|--option-h"
"* -a|--opt-a",
"-b|--opt-b",
"* -c|--opt-c"
);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_lists_environment_variable_names_for_options_that_have_them_defined()
public async Task Help_text_shows_usage_format_which_lists_available_sub_commands()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(EnvironmentVariableCommand))
.AddCommand<DefaultCommand>()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseConsole(console)
.Build();
// Act
await application.RunAsync(new[] {"cmd-with-env-vars", "--help"});
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
var exitCode = await application.RunAsync(new[] {"--help"});
// Assert
stdOutData.Should().ContainAll(
"Options",
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
exitCode.Should().Be(0);
stdOut.GetString().Should().ContainAll(
"Usage",
"... named",
"... named sub"
);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_shows_all_valid_values_for_enum_arguments()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<WithEnumArgumentsCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Should().ContainAll(
"Parameters",
"enum", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
"Options",
"--enum", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
"* --required-enum", "Valid values: \"Value1\", \"Value2\", \"Value3\"."
);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_shows_environment_variable_names_for_options_that_have_them_defined()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<WithEnvironmentVariablesCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Should().ContainAll(
"Options",
"-a|--opt-a", "Environment variable:", "ENV_OPT_A",
"-b|--opt-b", "Environment variable:", "ENV_OPT_B"
);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_shows_default_values_for_non_required_options()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<WithDefaultValuesCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Should().ContainAll(
"Options",
"--obj", "Default: \"42\"",
"--str", "Default: \"foo\"",
"--str-empty", "Default: \"\"",
"--str-array", "Default: \"foo\" \"bar\" \"baz\"",
"--bool", "Default: \"True\"",
"--char", "Default: \"t\"",
"--int", "Default: \"1337\"",
"--int-nullable", "Default: \"1337\"",
"--int-array", "Default: \"1\" \"2\" \"3\"",
"--timespan", "Default: \"02:03:00\"",
"--enum", "Default: \"Value2\""
);
_output.WriteLine(stdOut.GetString());
}
}
}

View File

@@ -0,0 +1,10 @@
using Newtonsoft.Json;
namespace CliFx.Tests.Internal
{
internal static class JsonExtensions
{
public static T DeserializeJson<T>(this string json) =>
JsonConvert.DeserializeObject<T>(json);
}
}

View File

@@ -1,51 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx.Attributes;
namespace CliFx.Tests
{
public partial class RoutingSpecs
{
[Command]
private class DefaultCommand : ICommand
{
public ValueTask ExecuteAsync(IConsole console)
{
console.Output.WriteLine("Hello world!");
return default;
}
}
[Command("concat", Description = "Concatenate strings.")]
private class ConcatCommand : ICommand
{
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
public IReadOnlyList<string> Inputs { get; set; }
[CommandOption('s', Description = "String separator.")]
public string Separator { get; set; } = "";
public ValueTask ExecuteAsync(IConsole console)
{
console.Output.WriteLine(string.Join(Separator, Inputs));
return default;
}
}
[Command("div", Description = "Divide one number by another.")]
private class DivideCommand : ICommand
{
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
public double Dividend { get; set; }
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
public double Divisor { get; set; }
public ValueTask ExecuteAsync(IConsole console)
{
console.Output.WriteLine(Dividend / Divisor);
return default;
}
}
}
}

View File

@@ -1,90 +1,235 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using CliFx.Tests.Commands;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace CliFx.Tests
{
public partial class RoutingSpecs
public class RoutingSpecs
{
private readonly ITestOutputHelper _output;
public RoutingSpecs(ITestOutputHelper testOutput) => _output = testOutput;
[Fact]
public async Task Default_command_is_executed_if_provided_arguments_do_not_match_any_named_command()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(DefaultCommand))
.AddCommand(typeof(ConcatCommand))
.AddCommand(typeof(DivideCommand))
.AddCommand<DefaultCommand>()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(
Array.Empty<string>(),
new Dictionary<string, string>());
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().Be(0);
stdOutData.Should().Be("Hello world!");
}
stdOut.GetString().Trim().Should().Be(DefaultCommand.ExpectedOutputText);
[Fact]
public async Task Help_text_is_printed_if_no_arguments_were_provided_and_default_command_is_not_defined()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var application = new CliApplicationBuilder()
.AddCommand(typeof(ConcatCommand))
.AddCommand(typeof(DivideCommand))
.UseConsole(console)
.UseDescription("This will be visible in help")
.Build();
// Act
var exitCode = await application.RunAsync(
Array.Empty<string>(),
new Dictionary<string, string>());
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
// Assert
exitCode.Should().Be(0);
stdOutData.Should().Contain("This will be visible in help");
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Specific_named_command_is_executed_if_provided_arguments_match_its_name()
{
// Arrange
await using var stdOut = new MemoryStream();
var console = new VirtualConsole(output: stdOut);
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand(typeof(DefaultCommand))
.AddCommand(typeof(ConcatCommand))
.AddCommand(typeof(DivideCommand))
.AddCommand<DefaultCommand>()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(
new[] {"concat", "-i", "foo", "bar", "-s", ", "},
new Dictionary<string, string>());
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
var exitCode = await application.RunAsync(new[] {"named"});
// Assert
exitCode.Should().Be(0);
stdOutData.Should().Be("foo, bar");
stdOut.GetString().Trim().Should().Be(NamedCommand.ExpectedOutputText);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Specific_named_sub_command_is_executed_if_provided_arguments_match_its_name()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DefaultCommand>()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"named", "sub"});
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Trim().Should().Be(NamedSubCommand.ExpectedOutputText);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_is_printed_if_no_arguments_were_provided_and_default_command_is_not_defined()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseConsole(console)
.UseDescription("This will be visible in help")
.Build();
// Act
var exitCode = await application.RunAsync(Array.Empty<string>());
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Should().Contain("This will be visible in help");
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_is_printed_if_provided_arguments_contain_the_help_option()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DefaultCommand>()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"--help"});
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Should().ContainAll(
"Default command description",
"Usage"
);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_is_printed_if_provided_arguments_contain_the_help_option_even_if_default_command_is_not_defined()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseDescription("This will be visible in help")
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"--help"});
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Should().Contain("This will be visible in help");
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_for_a_specific_named_command_is_printed_if_provided_arguments_match_its_name_and_contain_the_help_option()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DefaultCommand>()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"named", "--help"});
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Should().ContainAll(
"Named command description",
"Usage",
"named"
);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Help_text_for_a_specific_named_sub_command_is_printed_if_provided_arguments_match_its_name_and_contain_the_help_option()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DefaultCommand>()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"named", "sub", "--help"});
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Should().ContainAll(
"Named sub command description",
"Usage",
"named", "sub"
);
_output.WriteLine(stdOut.GetString());
}
[Fact]
public async Task Version_is_printed_if_the_only_provided_argument_is_the_version_option()
{
// Arrange
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
var application = new CliApplicationBuilder()
.AddCommand<DefaultCommand>()
.AddCommand<NamedCommand>()
.AddCommand<NamedSubCommand>()
.UseVersionText("v6.9")
.UseConsole(console)
.Build();
// Act
var exitCode = await application.RunAsync(new[] {"--version"});
// Assert
exitCode.Should().Be(0);
stdOut.GetString().Trim().Should().Be("v6.9");
_output.WriteLine(stdOut.GetString());
}
}
}

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,11 +1,12 @@
<Project>
<PropertyGroup>
<Version>1.1</Version>
<Version>1.5</Version>
<Company>Tyrrrz</Company>
<Copyright>Copyright (C) Alexey Golub</Copyright>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>
</Project>

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

@@ -28,7 +28,8 @@ namespace CliFx
/// </summary>
public ApplicationConfiguration(
IReadOnlyList<Type> commandTypes,
bool isDebugModeAllowed, bool isPreviewModeAllowed)
bool isDebugModeAllowed,
bool isPreviewModeAllowed)
{
CommandTypes = commandTypes;
IsDebugModeAllowed = isDebugModeAllowed;

View File

@@ -28,7 +28,11 @@
/// <summary>
/// Initializes an instance of <see cref="ApplicationMetadata"/>.
/// </summary>
public ApplicationMetadata(string title, string executableName, string versionText, string? description)
public ApplicationMetadata(
string title,
string executableName,
string versionText,
string? description)
{
Title = title;
ExecutableName = executableName;

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>
@@ -37,6 +37,12 @@ namespace CliFx.Attributes
/// </summary>
public string? EnvironmentVariableName { get; set; }
/// <summary>
/// Type of converter to use when mapping the argument value.
/// Converter must implement <see cref="IArgumentValueConverter"/>.
/// </summary>
public Type? Converter { get; set; }
/// <summary>
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
/// </summary>

View File

@@ -26,6 +26,12 @@ namespace CliFx.Attributes
/// </summary>
public string? Description { get; set; }
/// <summary>
/// Type of converter to use when mapping the argument value.
/// Converter must implement <see cref="IArgumentValueConverter"/>.
/// </summary>
public Type? Converter { get; set; }
/// <summary>
/// Initializes an instance of <see cref="CommandParameterAttribute"/>.
/// </summary>

View File

@@ -1,18 +1,21 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Domain;
using CliFx.Exceptions;
using CliFx.Internal;
namespace CliFx
{
/// <summary>
/// Command line application facade.
/// </summary>
public class CliApplication
public partial class CliApplication
{
private readonly ApplicationMetadata _metadata;
private readonly ApplicationConfiguration _configuration;
@@ -36,42 +39,34 @@ namespace CliFx
_helpTextWriter = new HelpTextWriter(metadata, console);
}
private async ValueTask<int?> HandleDebugDirectiveAsync(CommandLineInput commandLineInput)
private async ValueTask LaunchAndWaitForDebuggerAsync()
{
var isDebugMode = _configuration.IsDebugModeAllowed && commandLineInput.IsDebugDirectiveSpecified;
if (!isDebugMode)
return null;
var processId = ProcessEx.GetCurrentProcessId();
_console.WithForegroundColor(ConsoleColor.Green, () =>
_console.Output.WriteLine($"Attach debugger to PID {Process.GetCurrentProcess().Id} to continue."));
_console.Output.WriteLine($"Attach debugger to PID {processId} to continue."));
Debugger.Launch();
while (!Debugger.IsAttached)
{
await Task.Delay(100);
return null;
}
}
private int? HandlePreviewDirective(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
private void WriteCommandLineInput(CommandInput input)
{
var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified;
if (!isPreviewMode)
return null;
var commandSchema = applicationSchema.TryFindCommand(commandLineInput, out var argumentOffset);
_console.Output.WriteLine("Parser preview:");
// Command name
if (commandSchema != null && argumentOffset > 0)
if (!string.IsNullOrWhiteSpace(input.CommandName))
{
_console.WithForegroundColor(ConsoleColor.Cyan, () =>
_console.Output.Write(commandSchema.Name));
_console.Output.Write(input.CommandName));
_console.Output.Write(' ');
}
// Parameters
foreach (var parameter in commandLineInput.UnboundArguments.Skip(argumentOffset))
foreach (var parameter in input.Parameters)
{
_console.Output.Write('<');
@@ -83,100 +78,145 @@ namespace CliFx
}
// Options
foreach (var option in commandLineInput.Options)
foreach (var option in input.Options)
{
_console.Output.Write('[');
_console.WithForegroundColor(ConsoleColor.White, () =>
_console.Output.Write(option));
{
// Alias
_console.Output.Write(option.GetRawAlias());
// Values
if (option.Values.Any())
{
_console.Output.Write(' ');
_console.Output.Write(option.GetRawValues());
}
});
_console.Output.Write(']');
_console.Output.Write(' ');
}
_console.Output.WriteLine();
return 0;
}
private int? HandleVersionOption(CommandLineInput commandLineInput)
{
// Version option is available only on the default command (i.e. when arguments are not specified)
var shouldRenderVersion = !commandLineInput.UnboundArguments.Any() && commandLineInput.IsVersionOptionSpecified;
if (!shouldRenderVersion)
return null;
_console.Output.WriteLine(_metadata.VersionText);
return 0;
}
private int? HandleHelpOption(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
{
// Help is rendered either when it's requested or when the user provides no arguments and there is no default command
var shouldRenderHelp =
commandLineInput.IsHelpOptionSpecified ||
!applicationSchema.Commands.Any(c => c.IsDefault) && !commandLineInput.UnboundArguments.Any() && !commandLineInput.Options.Any();
if (!shouldRenderHelp)
return null;
// Get the command schema that matches the input or use a dummy default command as a fallback
var commandSchema =
applicationSchema.TryFindCommand(commandLineInput) ??
CommandSchema.StubDefaultCommand;
_helpTextWriter.Write(applicationSchema, commandSchema);
return 0;
}
private async ValueTask<int> HandleCommandExecutionAsync(
ApplicationSchema applicationSchema,
CommandLineInput commandLineInput,
IReadOnlyDictionary<string, string> environmentVariables)
{
await applicationSchema
.InitializeEntryPoint(commandLineInput, environmentVariables, _typeActivator)
.ExecuteAsync(_console);
return 0;
}
private ICommand GetCommandInstance(CommandSchema command) =>
command != FallbackDefaultCommand.Schema
? (ICommand) _typeActivator.CreateInstance(command.Type)
: new FallbackDefaultCommand();
/// <summary>
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
/// </summary>
/// <remarks>
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
/// this method will be handled and routed to the console as well.
/// </remarks>
public async ValueTask<int> RunAsync(
IReadOnlyList<string> commandLineArguments,
IReadOnlyDictionary<string, string> environmentVariables)
{
try
{
var applicationSchema = ApplicationSchema.Resolve(_configuration.CommandTypes);
var commandLineInput = CommandLineInput.Parse(commandLineArguments);
var root = RootSchema.Resolve(_configuration.CommandTypes);
var input = CommandInput.Parse(commandLineArguments, root.GetCommandNames());
return
await HandleDebugDirectiveAsync(commandLineInput) ??
HandlePreviewDirective(applicationSchema, commandLineInput) ??
HandleVersionOption(commandLineInput) ??
HandleHelpOption(applicationSchema, commandLineInput) ??
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
// Debug mode
if (_configuration.IsDebugModeAllowed && input.IsDebugDirectiveSpecified)
{
await LaunchAndWaitForDebuggerAsync();
}
// Preview mode
if (_configuration.IsPreviewModeAllowed && input.IsPreviewDirectiveSpecified)
{
WriteCommandLineInput(input);
return ExitCode.Success;
}
// Try to get the command matching the input or fallback to default
var command =
root.TryFindCommand(input.CommandName) ??
root.TryFindDefaultCommand() ??
FallbackDefaultCommand.Schema;
// Version option
if (command.IsVersionOptionAvailable && input.IsVersionOptionSpecified)
{
_console.Output.WriteLine(_metadata.VersionText);
return ExitCode.Success;
}
// Get command instance (also used in help text)
var instance = GetCommandInstance(command);
// To avoid instantiating the command twice, we need to get default values
// before the arguments are bound to the properties
var defaultValues = command.GetArgumentValues(instance);
// Help option
if (command.IsHelpOptionAvailable && input.IsHelpOptionSpecified ||
command == FallbackDefaultCommand.Schema && !input.Parameters.Any() && !input.Options.Any())
{
_helpTextWriter.Write(root, command, defaultValues);
return ExitCode.Success;
}
// Bind arguments
try
{
command.Bind(instance, input, environmentVariables);
}
// This may throw exceptions which are useful only to the end-user
catch (CliFxException ex)
{
_console.WithForegroundColor(ConsoleColor.Red, () =>
_console.Error.WriteLine(ex.ToString())
);
_helpTextWriter.Write(root, command, defaultValues);
return ExitCode.FromException(ex);
}
// Execute the command
try
{
await instance.ExecuteAsync(_console);
return ExitCode.Success;
}
// Swallow command exceptions and route them to the console
catch (CommandException ex)
{
_console.WithForegroundColor(ConsoleColor.Red, () =>
_console.Error.WriteLine(ex.ToString())
);
if (ex.ShowHelp)
{
_helpTextWriter.Write(root, command, defaultValues);
}
return ex.ExitCode;
}
}
catch (Exception ex)
// To prevent the app from showing the annoying Windows troubleshooting dialog,
// we handle all exceptions and route them to the console nicely.
// However, we don't want to swallow unhandled exceptions when the debugger is attached,
// because we still want the IDE to show them to the developer.
catch (Exception ex) when (!Debugger.IsAttached)
{
// 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.
_console.WithColors(ConsoleColor.White, ConsoleColor.DarkRed, () =>
_console.Error.Write("ERROR:")
);
// 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.Error.Write(" ");
_console.WriteException(ex);
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(errorMessage));
return ex is CommandException commandException
? commandException.ExitCode
: ex.HResult;
return ExitCode.FromException(ex);
}
}
@@ -184,11 +224,17 @@ namespace CliFx
/// Runs the application with specified command line arguments and returns the exit code.
/// Environment variables are retrieved automatically.
/// </summary>
/// <remarks>
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
/// this method will be handled and routed to the console as well.
/// </remarks>
public async ValueTask<int> RunAsync(IReadOnlyList<string> commandLineArguments)
{
// Environment variable names are case-insensitive on Windows but are case-sensitive on Linux and macOS
var environmentVariables = Environment.GetEnvironmentVariables()
.Cast<DictionaryEntry>()
.ToDictionary(e => (string) e.Key, e => (string) e.Value, StringComparer.OrdinalIgnoreCase);
.ToDictionary(e => (string) e.Key, e => (string) e.Value, StringComparer.Ordinal);
return await RunAsync(commandLineArguments, environmentVariables);
}
@@ -197,6 +243,11 @@ namespace CliFx
/// Runs the application and returns the exit code.
/// Command line arguments and environment variables are retrieved automatically.
/// </summary>
/// <remarks>
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
/// this method will be handled and routed to the console as well.
/// </remarks>
public async ValueTask<int> RunAsync()
{
var commandLineArguments = Environment.GetCommandLineArgs()
@@ -206,4 +257,28 @@ namespace CliFx
return await RunAsync(commandLineArguments);
}
}
public partial class CliApplication
{
private static class ExitCode
{
public const int Success = 0;
public static int FromException(Exception ex) =>
ex is CommandException cmdEx
? cmdEx.ExitCode
: 1;
}
// Fallback default command used when none is defined in the application
[Command]
private class FallbackDefaultCommand : ICommand
{
public static CommandSchema Schema { get; } = CommandSchema.TryResolve(typeof(FallbackDefaultCommand))!;
// Never actually executed
[ExcludeFromCodeCoverage]
public ValueTask ExecuteAsync(IConsole console) => default;
}
}
}

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Reflection;
using CliFx.Domain;
using CliFx.Internal.Extensions;
namespace CliFx
{
@@ -33,6 +34,12 @@ namespace CliFx
return this;
}
/// <summary>
/// Adds a command of specified type to the application.
/// </summary>
public CliApplicationBuilder AddCommand<TCommand>() where TCommand : ICommand =>
AddCommand(typeof(TCommand));
/// <summary>
/// Adds multiple commands to the application.
/// </summary>
@@ -158,9 +165,9 @@ namespace CliFx
/// </summary>
public CliApplication Build()
{
_title ??= GetDefaultTitle() ?? "App";
_executableName ??= GetDefaultExecutableName() ?? "app";
_versionText ??= GetDefaultVersionText() ?? "v1.0";
_title ??= GetDefaultTitle();
_executableName ??= GetDefaultExecutableName();
_versionText ??= GetDefaultVersionText();
_console ??= new SystemConsole();
_typeActivator ??= new DefaultTypeActivator();
@@ -173,30 +180,34 @@ 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;
private static string GetDefaultTitle() => EntryAssembly?.GetName().Name?? "App";
private static string? GetDefaultExecutableName()
private static string GetDefaultExecutableName()
{
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);
var name = isDll
? "dotnet " + Path.GetFileName(entryAssemblyLocation)
: Path.GetFileNameWithoutExtension(entryAssemblyLocation);
return name ?? "app";
}
private static string? GetDefaultVersionText() =>
private static string GetDefaultVersionText() =>
EntryAssembly != null
? $"v{EntryAssembly.GetName().Version}"
: null;
? $"v{EntryAssembly.GetName().Version.ToSemanticString()}"
: "v1.0";
}
}

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,35 +10,42 @@
<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>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>$(AssemblyName).Analyzers</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<None Include="../favicon.png" Pack="True" PackagePath="" />
<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" />
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net45'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</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

@@ -1,6 +1,6 @@
using System;
using System.Text;
using CliFx.Exceptions;
using CliFx.Internal.Extensions;
namespace CliFx
{
@@ -14,15 +14,11 @@ namespace CliFx
{
try
{
return Activator.CreateInstance(type);
return type.CreateInstance();
}
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

@@ -1,5 +1,4 @@
using System;
using System.Text;
using CliFx.Exceptions;
namespace CliFx
@@ -18,10 +17,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.DelegateActivatorReturnedNull(type);
}
}

View File

@@ -1,267 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Internal;
namespace CliFx.Domain
{
internal partial class ApplicationSchema
{
public IReadOnlyList<CommandSchema> Commands { get; }
public ApplicationSchema(IReadOnlyList<CommandSchema> commands)
{
Commands = commands;
}
public CommandSchema? TryFindParentCommand(string? childCommandName)
{
// Default command has no parent
if (string.IsNullOrWhiteSpace(childCommandName))
return null;
// Try to find the parent command by repeatedly biting off chunks of its name
var route = childCommandName.Split(' ');
for (var i = route.Length - 1; i >= 1; i--)
{
var potentialParentCommandName = string.Join(" ", route.Take(i));
var matchingParentCommand = Commands.FirstOrDefault(c => c.MatchesName(potentialParentCommandName));
if (matchingParentCommand != null)
return matchingParentCommand;
}
// If there's no parent - fall back to default command
return Commands.FirstOrDefault(c => c.IsDefault);
}
public IReadOnlyList<CommandSchema> GetChildCommands(string? parentCommandName) =>
!string.IsNullOrWhiteSpace(parentCommandName) || Commands.Any(c => c.IsDefault)
? Commands.Where(c => TryFindParentCommand(c.Name)?.MatchesName(parentCommandName) == true).ToArray()
: Commands.Where(c => !string.IsNullOrWhiteSpace(c.Name) && TryFindParentCommand(c.Name) == null).ToArray();
// TODO: this out parameter is not a really nice design
public CommandSchema? TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset)
{
// Try to find the command that contains the most of the input arguments in its name
for (var i = commandLineInput.UnboundArguments.Count; i >= 0; i--)
{
var potentialCommandName = string.Join(" ", commandLineInput.UnboundArguments.Take(i));
var matchingCommand = Commands.FirstOrDefault(c => c.MatchesName(potentialCommandName));
if (matchingCommand != null)
{
argumentOffset = i;
return matchingCommand;
}
}
argumentOffset = 0;
return Commands.FirstOrDefault(c => c.IsDefault);
}
public CommandSchema? TryFindCommand(CommandLineInput commandLineInput) =>
TryFindCommand(commandLineInput, out _);
public ICommand InitializeEntryPoint(
CommandLineInput commandLineInput,
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 parameterValues = argumentOffset == 0
? commandLineInput.UnboundArguments.Select(a => a.Value).ToArray()
: commandLineInput.UnboundArguments.Skip(argumentOffset).Select(a => a.Value).ToArray();
return command.CreateInstance(parameterValues, commandLineInput.Options, environmentVariables, activator);
}
public ICommand InitializeEntryPoint(
CommandLineInput commandLineInput,
IReadOnlyDictionary<string, string> environmentVariables) =>
InitializeEntryPoint(commandLineInput, environmentVariables, new DefaultTypeActivator());
public ICommand InitializeEntryPoint(CommandLineInput commandLineInput) =>
InitializeEntryPoint(commandLineInput, new Dictionary<string, string>());
public override string ToString() => string.Join(Environment.NewLine, Commands);
}
internal partial class ApplicationSchema
{
private static void ValidateParameters(CommandSchema command)
{
var duplicateOrderGroup = command.Parameters
.GroupBy(a => a.Order)
.FirstOrDefault(g => g.Count() > 1);
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());
}
var duplicateNameGroup = command.Parameters
.Where(a => !string.IsNullOrWhiteSpace(a.Name))
.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());
}
var nonScalarParameters = command.Parameters
.Where(p => !p.IsScalar)
.ToArray();
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());
}
var nonLastNonScalarParameter = command.Parameters
.OrderByDescending(a => a.Order)
.Skip(1)
.LastOrDefault(p => !p.IsScalar);
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());
}
}
private static void ValidateOptions(CommandSchema command)
{
var duplicateNameGroup = command.Options
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
.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());
}
var duplicateShortNameGroup = command.Options
.Where(o => o.ShortName != null)
.GroupBy(o => o.ShortName)
.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());
}
var duplicateEnvironmentVariableNameGroup = command.Options
.Where(o => !string.IsNullOrWhiteSpace(o.EnvironmentVariableName))
.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());
}
}
private static void ValidateCommands(IReadOnlyList<CommandSchema> commands)
{
if (!commands.Any())
{
throw new CliFxException("There are no commands configured for this application.");
}
var duplicateNameGroup = commands
.GroupBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
.FirstOrDefault(g => g.Count() > 1);
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());
}
}
public static ApplicationSchema Resolve(IReadOnlyList<Type> commandTypes)
{
var commands = new List<CommandSchema>();
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());
}
ValidateParameters(command);
ValidateOptions(command);
commands.Add(command);
}
ValidateCommands(commands);
return new ApplicationSchema(commands);
}
}
}

View File

@@ -3,153 +3,93 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using CliFx.Exceptions;
using CliFx.Internal;
using CliFx.Internal.Extensions;
namespace CliFx.Domain
{
internal abstract partial class CommandArgumentSchema
{
public PropertyInfo Property { get; }
// Property can be null on built-in arguments (help and version options)
public PropertyInfo? Property { get; }
public string? Description { get; }
public bool IsScalar => GetEnumerableArgumentUnderlyingType() == null;
public bool IsScalar => TryGetEnumerableArgumentUnderlyingType() == null;
protected CommandArgumentSchema(PropertyInfo property, string? description)
public Type? ConverterType { get; }
protected CommandArgumentSchema(PropertyInfo? property, string? description, Type? converterType)
{
Property = property;
Description = description;
ConverterType = converterType;
}
private Type? GetEnumerableArgumentUnderlyingType() =>
Property.PropertyType != typeof(string)
? Property.PropertyType.GetEnumerableUnderlyingType()
private Type? TryGetEnumerableArgumentUnderlyingType() =>
Property != null && Property.PropertyType != typeof(string)
? Property.PropertyType.TryGetEnumerableUnderlyingType()
: null;
private object Convert(IReadOnlyList<string> values)
{
var targetType = Property.PropertyType;
var enumerableUnderlyingType = GetEnumerableArgumentUnderlyingType();
// 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);
}
// Non-scalar
else
{
return ConvertNonScalar(values, targetType, enumerableUnderlyingType);
}
}
public void Inject(ICommand command, IReadOnlyList<string> values) =>
Property.SetValue(command, Convert(values));
public void Inject(ICommand command, params string[] values) =>
Inject(command, (IReadOnlyList<string>) values);
}
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>>
{
[typeof(object)] = v => v,
[typeof(string)] = v => v,
[typeof(bool)] = v => string.IsNullOrWhiteSpace(v) || bool.Parse(v),
[typeof(char)] = v => v.Single(),
[typeof(sbyte)] = v => sbyte.Parse(v, ConversionFormatProvider),
[typeof(byte)] = v => byte.Parse(v, ConversionFormatProvider),
[typeof(short)] = v => short.Parse(v, ConversionFormatProvider),
[typeof(ushort)] = v => ushort.Parse(v, ConversionFormatProvider),
[typeof(int)] = v => int.Parse(v, ConversionFormatProvider),
[typeof(uint)] = v => uint.Parse(v, ConversionFormatProvider),
[typeof(long)] = v => long.Parse(v, ConversionFormatProvider),
[typeof(ulong)] = v => ulong.Parse(v, ConversionFormatProvider),
[typeof(float)] = v => float.Parse(v, ConversionFormatProvider),
[typeof(double)] = v => double.Parse(v, ConversionFormatProvider),
[typeof(decimal)] = v => decimal.Parse(v, ConversionFormatProvider),
[typeof(DateTime)] = v => DateTime.Parse(v, ConversionFormatProvider),
[typeof(DateTimeOffset)] = v => DateTimeOffset.Parse(v, ConversionFormatProvider),
[typeof(TimeSpan)] = v => TimeSpan.Parse(v, ConversionFormatProvider),
};
private static ConstructorInfo? GetStringConstructor(Type type) =>
type.GetConstructor(new[] {typeof(string)});
private static MethodInfo? GetStaticParseMethod(Type type) =>
type.GetMethod("Parse",
BindingFlags.Public | BindingFlags.Static,
null, new[] {typeof(string)}, null);
private static MethodInfo? GetStaticParseMethodWithFormatProvider(Type type) =>
type.GetMethod("Parse",
BindingFlags.Public | BindingFlags.Static,
null, new[] {typeof(string), typeof(IFormatProvider)}, null);
private static object ConvertScalar(string? value, Type targetType)
private object? ConvertScalar(string? value, Type targetType)
{
try
{
// Primitive
// Custom conversion (always takes highest priority)
if (ConverterType != null)
return ConverterType.CreateInstance<IArgumentValueConverter>().ConvertFrom(value!);
// No conversion necessary
if (targetType == typeof(object) || targetType == typeof(string))
return value;
// Bool conversion (special case)
if (targetType == typeof(bool))
return string.IsNullOrWhiteSpace(value) || bool.Parse(value);
// Primitive conversion
var primitiveConverter = PrimitiveConverters.GetValueOrDefault(targetType);
if (primitiveConverter != null)
if (primitiveConverter != null && !string.IsNullOrWhiteSpace(value))
return primitiveConverter(value);
// Enum
if (targetType.IsEnum)
// Enum conversion
if (targetType.IsEnum && !string.IsNullOrWhiteSpace(value))
return Enum.Parse(targetType, value, true);
// Nullable
var nullableUnderlyingType = targetType.GetNullableUnderlyingType();
// Nullable<T> conversion
var nullableUnderlyingType = targetType.TryGetNullableUnderlyingType();
if (nullableUnderlyingType != null)
return !string.IsNullOrWhiteSpace(value)
? ConvertScalar(value, nullableUnderlyingType)
: null;
// String-constructable
var stringConstructor = GetStringConstructor(targetType);
// String-constructible conversion
var stringConstructor = targetType.GetConstructor(new[] {typeof(string)});
if (stringConstructor != null)
return stringConstructor.Invoke(new object[] {value});
return stringConstructor.Invoke(new object[] {value!});
// String-parseable (with format provider)
var parseMethodWithFormatProvider = GetStaticParseMethodWithFormatProvider(targetType);
// String-parseable conversion (with format provider)
var parseMethodWithFormatProvider = targetType.TryGetStaticParseMethod(true);
if (parseMethodWithFormatProvider != null)
return parseMethodWithFormatProvider.Invoke(null, new object[] {value, ConversionFormatProvider});
return parseMethodWithFormatProvider.Invoke(null, new object[] {value!, FormatProvider});
// String-parseable (without format provider)
var parseMethod = GetStaticParseMethod(targetType);
// String-parseable conversion (without format provider)
var parseMethod = targetType.TryGetStaticParseMethod();
if (parseMethod != null)
return parseMethod.Invoke(null, new object[] {value});
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 CliFxException.CannotConvertToType(this, value, targetType, 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());
throw CliFxException.CannotConvertToType(this, value, targetType);
}
private static object ConvertNonScalar(IReadOnlyList<string> values, Type targetEnumerableType, Type targetElementType)
private object ConvertNonScalar(
IReadOnlyList<string> values,
Type targetEnumerableType,
Type targetElementType)
{
var array = values
.Select(v => ConvertScalar(v, targetElementType))
@@ -161,16 +101,82 @@ namespace CliFx.Domain
if (targetEnumerableType.IsAssignableFrom(arrayType))
return array;
// Constructable from an array
// Constructible 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());
throw CliFxException.CannotConvertNonScalar(this, values, targetEnumerableType);
}
private object? Convert(IReadOnlyList<string> values)
{
// Short-circuit built-in arguments
if (Property == null)
return null;
var targetType = Property.PropertyType;
var enumerableUnderlyingType = TryGetEnumerableArgumentUnderlyingType();
// Scalar
if (enumerableUnderlyingType == null)
{
return values.Count <= 1
? ConvertScalar(values.SingleOrDefault(), targetType)
: throw CliFxException.CannotConvertMultipleValuesToNonScalar(this, values);
}
// Non-scalar
else
{
return ConvertNonScalar(values, targetType, enumerableUnderlyingType);
}
}
public void BindOn(ICommand command, IReadOnlyList<string> values) =>
Property?.SetValue(command, Convert(values));
public void BindOn(ICommand command, params string[] values) =>
BindOn(command, (IReadOnlyList<string>) values);
public IReadOnlyList<string> GetValidValues()
{
if (Property == null)
return Array.Empty<string>();
var underlyingType =
Property.PropertyType.TryGetNullableUnderlyingType() ??
Property.PropertyType;
// Enum
if (underlyingType.IsEnum)
return Enum.GetNames(underlyingType);
return Array.Empty<string>();
}
}
internal partial class CommandArgumentSchema
{
private static readonly IFormatProvider FormatProvider = CultureInfo.InvariantCulture;
private static readonly IReadOnlyDictionary<Type, Func<string, object?>> PrimitiveConverters =
new Dictionary<Type, Func<string, object?>>
{
[typeof(char)] = v => v.Single(),
[typeof(sbyte)] = v => sbyte.Parse(v, FormatProvider),
[typeof(byte)] = v => byte.Parse(v, FormatProvider),
[typeof(short)] = v => short.Parse(v, FormatProvider),
[typeof(ushort)] = v => ushort.Parse(v, FormatProvider),
[typeof(int)] = v => int.Parse(v, FormatProvider),
[typeof(uint)] = v => uint.Parse(v, FormatProvider),
[typeof(long)] = v => long.Parse(v, FormatProvider),
[typeof(ulong)] = v => ulong.Parse(v, FormatProvider),
[typeof(float)] = v => float.Parse(v, FormatProvider),
[typeof(double)] = v => double.Parse(v, FormatProvider),
[typeof(decimal)] = v => decimal.Parse(v, FormatProvider),
[typeof(DateTime)] = v => DateTime.Parse(v, FormatProvider),
[typeof(DateTimeOffset)] = v => DateTimeOffset.Parse(v, FormatProvider),
[typeof(TimeSpan)] = v => TimeSpan.Parse(v, FormatProvider),
};
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
namespace CliFx.Domain
{
@@ -10,11 +11,9 @@ namespace CliFx.Domain
public bool IsPreviewDirective => string.Equals(Name, "preview", StringComparison.OrdinalIgnoreCase);
public CommandDirectiveInput(string name)
{
Name = name;
}
public CommandDirectiveInput(string name) => Name = name;
[ExcludeFromCodeCoverage]
public override string ToString() => $"[{Name}]";
}
}

View File

@@ -0,0 +1,240 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using CliFx.Internal.Extensions;
namespace CliFx.Domain
{
internal partial class CommandInput
{
public IReadOnlyList<CommandDirectiveInput> Directives { get; }
public string? CommandName { get; }
public IReadOnlyList<CommandParameterInput> Parameters { get; }
public IReadOnlyList<CommandOptionInput> Options { get; }
public bool IsDebugDirectiveSpecified => Directives.Any(d => d.IsDebugDirective);
public bool IsPreviewDirectiveSpecified => Directives.Any(d => d.IsPreviewDirective);
public bool IsHelpOptionSpecified => Options.Any(o => o.IsHelpOption);
public bool IsVersionOptionSpecified => Options.Any(o => o.IsVersionOption);
public CommandInput(
IReadOnlyList<CommandDirectiveInput> directives,
string? commandName,
IReadOnlyList<CommandParameterInput> parameters,
IReadOnlyList<CommandOptionInput> options)
{
Directives = directives;
CommandName = commandName;
Parameters = parameters;
Options = options;
}
[ExcludeFromCodeCoverage]
public override string ToString()
{
var buffer = new StringBuilder();
foreach (var directive in Directives)
{
buffer
.AppendIfNotEmpty(' ')
.Append(directive);
}
if (!string.IsNullOrWhiteSpace(CommandName))
{
buffer
.AppendIfNotEmpty(' ')
.Append(CommandName);
}
foreach (var parameter in Parameters)
{
buffer
.AppendIfNotEmpty(' ')
.Append(parameter);
}
foreach (var option in Options)
{
buffer
.AppendIfNotEmpty(' ')
.Append(option);
}
return buffer.ToString();
}
}
internal partial class CommandInput
{
private static IReadOnlyList<CommandDirectiveInput> ParseDirectives(
IReadOnlyList<string> commandLineArguments,
ref int index)
{
var result = new List<CommandDirectiveInput>();
for (; index < commandLineArguments.Count; index++)
{
var argument = commandLineArguments[index];
if (!argument.StartsWith('[') || !argument.EndsWith(']'))
break;
var name = argument.Substring(1, argument.Length - 2);
result.Add(new CommandDirectiveInput(name));
}
return result;
}
private static string? ParseCommandName(
IReadOnlyList<string> commandLineArguments,
ISet<string> commandNames,
ref int index)
{
var buffer = new List<string>();
var commandName = default(string?);
var lastIndex = index;
// We need to look ahead to see if we can match as many consecutive arguments to a command name as possible
for (var i = index; i < commandLineArguments.Count; i++)
{
var argument = commandLineArguments[i];
buffer.Add(argument);
var potentialCommandName = buffer.JoinToString(" ");
if (commandNames.Contains(potentialCommandName))
{
commandName = potentialCommandName;
lastIndex = i;
}
}
// Update the index only if command name was found in the arguments
if (!string.IsNullOrWhiteSpace(commandName))
index = lastIndex + 1;
return commandName;
}
private static IReadOnlyList<CommandParameterInput> ParseParameters(
IReadOnlyList<string> commandLineArguments,
ref int index)
{
var result = new List<CommandParameterInput>();
for (; index < commandLineArguments.Count; index++)
{
var argument = commandLineArguments[index];
if (argument.StartsWith('-'))
break;
result.Add(new CommandParameterInput(argument));
}
return result;
}
private static IReadOnlyList<CommandOptionInput> ParseOptions(
IReadOnlyList<string> commandLineArguments,
ref int index)
{
var result = new List<CommandOptionInput>();
var currentOptionAlias = default(string?);
var currentOptionValues = new List<string>();
for (; index < commandLineArguments.Count; index++)
{
var argument = commandLineArguments[index];
// Name
if (argument.StartsWith("--", StringComparison.Ordinal))
{
// Flush previous
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
result.Add(new CommandOptionInput(currentOptionAlias, currentOptionValues));
currentOptionAlias = argument.Substring(2);
currentOptionValues = new List<string>();
}
// Short name
else if (argument.StartsWith('-'))
{
foreach (var alias in argument.Substring(1))
{
// Flush previous
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
result.Add(new CommandOptionInput(currentOptionAlias, currentOptionValues));
currentOptionAlias = alias.AsString();
currentOptionValues = new List<string>();
}
}
// Value
else if (!string.IsNullOrWhiteSpace(currentOptionAlias))
{
currentOptionValues.Add(argument);
}
}
// Flush last option
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
result.Add(new CommandOptionInput(currentOptionAlias, currentOptionValues));
return result;
}
public static CommandInput Parse(IReadOnlyList<string> commandLineArguments, IReadOnlyList<string> availableCommandNames)
{
var availableCommandNamesSet = availableCommandNames.ToHashSet(StringComparer.OrdinalIgnoreCase);
var index = 0;
var directives = ParseDirectives(
commandLineArguments,
ref index
);
var commandName = ParseCommandName(
commandLineArguments,
availableCommandNamesSet,
ref index
);
var parameters = ParseParameters(
commandLineArguments,
ref index
);
var options = ParseOptions(
commandLineArguments,
ref index
);
return new CommandInput(directives, commandName, parameters, options);
}
}
internal partial class CommandInput
{
public static CommandInput Empty { get; } = new CommandInput(
Array.Empty<CommandDirectiveInput>(),
null,
Array.Empty<CommandParameterInput>(),
Array.Empty<CommandOptionInput>()
);
}
}

View File

@@ -1,163 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CliFx.Internal;
namespace CliFx.Domain
{
internal partial class CommandLineInput
{
public IReadOnlyList<CommandDirectiveInput> Directives { get; }
public IReadOnlyList<CommandUnboundArgumentInput> UnboundArguments { get; }
public IReadOnlyList<CommandOptionInput> Options { get; }
public bool IsDebugDirectiveSpecified => Directives.Any(d => d.IsDebugDirective);
public bool IsPreviewDirectiveSpecified => Directives.Any(d => d.IsPreviewDirective);
public bool IsHelpOptionSpecified => Options.Any(o => o.IsHelpOption);
public bool IsVersionOptionSpecified => Options.Any(o => o.IsVersionOption);
public CommandLineInput(
IReadOnlyList<CommandDirectiveInput> directives,
IReadOnlyList<CommandUnboundArgumentInput> unboundArguments,
IReadOnlyList<CommandOptionInput> options)
{
Directives = directives;
UnboundArguments = unboundArguments;
Options = options;
}
public override string ToString()
{
var buffer = new StringBuilder();
foreach (var directive in Directives)
{
buffer.AppendIfNotEmpty(' ');
buffer.Append(directive);
}
foreach (var argument in UnboundArguments)
{
buffer.AppendIfNotEmpty(' ');
buffer.Append(argument);
}
foreach (var option in Options)
{
buffer.AppendIfNotEmpty(' ');
buffer.Append(option);
}
return buffer.ToString();
}
}
internal partial class CommandLineInput
{
public static CommandLineInput Parse(IReadOnlyList<string> commandLineArguments)
{
var builder = new CommandLineInputBuilder();
var currentOptionAlias = "";
var currentOptionValues = new List<string>();
bool TryParseDirective(string argument)
{
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
return false;
if (!argument.StartsWith("[", StringComparison.OrdinalIgnoreCase) ||
!argument.EndsWith("]", StringComparison.OrdinalIgnoreCase))
return false;
var directive = argument.Substring(1, argument.Length - 2);
builder.AddDirective(directive);
return true;
}
bool TryParseArgument(string argument)
{
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
return false;
builder.AddUnboundArgument(argument);
return true;
}
bool TryParseOptionName(string argument)
{
if (!argument.StartsWith("--", StringComparison.OrdinalIgnoreCase))
return false;
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
builder.AddOption(currentOptionAlias, currentOptionValues);
currentOptionAlias = argument.Substring(2);
currentOptionValues = new List<string>();
return true;
}
bool TryParseOptionShortName(string argument)
{
if (!argument.StartsWith("-", StringComparison.OrdinalIgnoreCase))
return false;
foreach (var c in argument.Substring(1))
{
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
builder.AddOption(currentOptionAlias, currentOptionValues);
currentOptionAlias = c.AsString();
currentOptionValues = new List<string>();
}
return true;
}
bool TryParseOptionValue(string argument)
{
if (string.IsNullOrWhiteSpace(currentOptionAlias))
return false;
currentOptionValues.Add(argument);
return true;
}
foreach (var argument in commandLineArguments)
{
var _ =
TryParseOptionName(argument) ||
TryParseOptionShortName(argument) ||
TryParseDirective(argument) ||
TryParseArgument(argument) ||
TryParseOptionValue(argument);
}
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
builder.AddOption(currentOptionAlias, currentOptionValues);
return builder.Build();
}
}
internal partial class CommandLineInput
{
private static IReadOnlyList<CommandDirectiveInput> EmptyDirectives { get; } = new CommandDirectiveInput[0];
private static IReadOnlyList<CommandUnboundArgumentInput> EmptyUnboundArguments { get; } = new CommandUnboundArgumentInput[0];
private static IReadOnlyList<CommandOptionInput> EmptyOptions { get; } = new CommandOptionInput[0];
public static CommandLineInput Empty { get; } = new CommandLineInput(EmptyDirectives, EmptyUnboundArguments, EmptyOptions);
}
}

View File

@@ -1,43 +0,0 @@
using System.Collections.Generic;
namespace CliFx.Domain
{
internal class CommandLineInputBuilder
{
private readonly List<CommandDirectiveInput> _directives = new List<CommandDirectiveInput>();
private readonly List<CommandUnboundArgumentInput> _unboundArguments = new List<CommandUnboundArgumentInput>();
private readonly List<CommandOptionInput> _options = new List<CommandOptionInput>();
public CommandLineInputBuilder AddDirective(CommandDirectiveInput directive)
{
_directives.Add(directive);
return this;
}
public CommandLineInputBuilder AddDirective(string directive) =>
AddDirective(new CommandDirectiveInput(directive));
public CommandLineInputBuilder AddUnboundArgument(CommandUnboundArgumentInput unboundArgument)
{
_unboundArguments.Add(unboundArgument);
return this;
}
public CommandLineInputBuilder AddUnboundArgument(string unboundArgument) =>
AddUnboundArgument(new CommandUnboundArgumentInput(unboundArgument));
public CommandLineInputBuilder AddOption(CommandOptionInput option)
{
_options.Add(option);
return this;
}
public CommandLineInputBuilder AddOption(string optionAlias, IReadOnlyList<string> values) =>
AddOption(new CommandOptionInput(optionAlias, values));
public CommandLineInputBuilder AddOption(string optionAlias, params string[] values) =>
AddOption(optionAlias, (IReadOnlyList<string>) values);
public CommandLineInput Build() => new CommandLineInput(_directives, _unboundArguments, _options);
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Text;
using CliFx.Internal;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using CliFx.Internal.Extensions;
namespace CliFx.Domain
{
@@ -20,29 +21,16 @@ namespace CliFx.Domain
Values = values;
}
public override string ToString()
public string GetRawAlias() => Alias switch
{
var buffer = new StringBuilder();
{Length: 0} => Alias,
{Length: 1} => $"-{Alias}",
_ => $"--{Alias}"
};
buffer.Append(Alias.Length > 1 ? "--" : "-");
buffer.Append(Alias);
public string GetRawValues() => Values.Select(v => v.Quote()).JoinToString(" ");
foreach (var value in Values)
{
buffer.AppendIfNotEmpty(' ');
var isEscaped = value.Contains(" ");
if (isEscaped)
buffer.Append('"');
buffer.Append(value);
if (isEscaped)
buffer.Append('"');
}
return buffer.ToString();
}
[ExcludeFromCodeCoverage]
public override string ToString() => $"{GetRawAlias()} {GetRawValues()}";
}
}

View File

@@ -1,9 +1,9 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using CliFx.Attributes;
using CliFx.Internal;
namespace CliFx.Domain
{
@@ -13,22 +13,19 @@ namespace CliFx.Domain
public char? ShortName { get; }
public string DisplayName => !string.IsNullOrWhiteSpace(Name)
? Name
: ShortName?.AsString()!;
public string? EnvironmentVariableName { get; }
public bool IsRequired { get; }
public CommandOptionSchema(
PropertyInfo property,
PropertyInfo? property,
string? name,
char? shortName,
string? environmentVariableName,
bool isRequired,
string? description)
: base(property, description)
string? description,
Type? converterType)
: base(property, description, converterType)
{
Name = name;
ShortName = shortName;
@@ -40,7 +37,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;
@@ -50,29 +47,38 @@ namespace CliFx.Domain
public bool MatchesEnvironmentVariableName(string environmentVariableName) =>
!string.IsNullOrWhiteSpace(EnvironmentVariableName) &&
string.Equals(EnvironmentVariableName, environmentVariableName, StringComparison.OrdinalIgnoreCase);
string.Equals(EnvironmentVariableName, environmentVariableName, StringComparison.Ordinal);
public override string ToString()
public string GetUserFacingDisplayString()
{
var buffer = new StringBuilder();
if (!string.IsNullOrWhiteSpace(Name))
{
buffer.Append("--");
buffer.Append(Name);
buffer
.Append("--")
.Append(Name);
}
if (!string.IsNullOrWhiteSpace(Name) && ShortName != null)
{
buffer.Append('|');
}
if (ShortName != null)
{
buffer.Append('-');
buffer.Append(ShortName);
buffer
.Append('-')
.Append(ShortName);
}
return buffer.ToString();
}
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ('{GetUserFacingDisplayString()}')";
[ExcludeFromCodeCoverage]
public override string ToString() => GetInternalDisplayString();
}
internal partial class CommandOptionSchema
@@ -83,13 +89,17 @@ namespace CliFx.Domain
if (attribute == null)
return null;
// The user may mistakenly specify dashes, thinking it's required, so trim them
var name = attribute.Name?.TrimStart('-');
return new CommandOptionSchema(
property,
attribute.Name,
name,
attribute.ShortName,
attribute.EnvironmentVariableName,
attribute.IsRequired,
attribute.Description
attribute.Description,
attribute.Converter
);
}
}
@@ -97,9 +107,9 @@ namespace CliFx.Domain
internal partial class CommandOptionSchema
{
public static CommandOptionSchema HelpOption { get; } =
new CommandOptionSchema(null!, "help", 'h', null, false, "Shows help text.");
new CommandOptionSchema(null, "help", 'h', null, false, "Shows help text.", null);
public static CommandOptionSchema VersionOption { get; } =
new CommandOptionSchema(null!, "version", null, null, false, "Shows version information.");
new CommandOptionSchema(null, "version", null, null, false, "Shows version information.", null);
}
}

View File

@@ -0,0 +1,14 @@
using System.Diagnostics.CodeAnalysis;
namespace CliFx.Domain
{
internal class CommandParameterInput
{
public string Value { get; }
public CommandParameterInput(string value) => Value = value;
[ExcludeFromCodeCoverage]
public override string ToString() => Value;
}
}

View File

@@ -1,4 +1,6 @@
using System.Reflection;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text;
using CliFx.Attributes;
@@ -8,30 +10,36 @@ namespace CliFx.Domain
{
public int Order { get; }
public string? Name { get; }
public string Name { get; }
public string DisplayName => !string.IsNullOrWhiteSpace(Name)
? Name
: Property.Name.ToLowerInvariant();
public CommandParameterSchema(PropertyInfo property, int order, string? name, string? description)
: base(property, description)
public CommandParameterSchema(
PropertyInfo? property,
int order,
string name,
string? description,
Type? converterType)
: base(property, description, converterType)
{
Order = order;
Name = name;
}
public override string ToString()
public string GetUserFacingDisplayString()
{
var buffer = new StringBuilder();
buffer
.Append('<')
.Append(DisplayName)
.Append(Name)
.Append('>');
return buffer.ToString();
}
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ([{Order}] {GetUserFacingDisplayString()})";
[ExcludeFromCodeCoverage]
public override string ToString() => GetInternalDisplayString();
}
internal partial class CommandParameterSchema
@@ -42,11 +50,14 @@ namespace CliFx.Domain
if (attribute == null)
return null;
var name = attribute.Name ?? property.Name.ToLowerInvariant();
return new CommandParameterSchema(
property,
attribute.Order,
attribute.Name,
attribute.Description
name,
attribute.Description,
attribute.Converter
);
}
}

Some files were not shown because too many files have changed in this diff Show More