mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60a3b26fd1 | ||
|
|
3abdfb1acf | ||
|
|
9557d386e2 | ||
|
|
d0d024c427 | ||
|
|
f765af6061 | ||
|
|
7f2202e869 | ||
|
|
14ad9d5738 | ||
|
|
b120138de3 | ||
|
|
8df1d607c1 | ||
|
|
c06f2810b9 | ||
|
|
d52a205f13 | ||
|
|
0ec12e57c1 | ||
|
|
c322b7029c | ||
|
|
6a38c04c11 | ||
|
|
5e53107def | ||
|
|
36cea937de | ||
|
|
438d6b98ac | ||
|
|
8e1488c395 | ||
|
|
65d321b476 | ||
|
|
c6d2359d6b | ||
|
|
0d32876bad | ||
|
|
c063251d89 | ||
|
|
3831cfc7c0 |
4
.github/workflows/CD.yml
vendored
4
.github/workflows/CD.yml
vendored
@@ -11,10 +11,10 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2.3.3
|
||||||
|
|
||||||
- name: Install .NET Core
|
- name: Install .NET Core
|
||||||
uses: actions/setup-dotnet@v1.4.0
|
uses: actions/setup-dotnet@v1.7.2
|
||||||
with:
|
with:
|
||||||
dotnet-version: 3.1.100
|
dotnet-version: 3.1.100
|
||||||
|
|
||||||
|
|||||||
4
.github/workflows/CI.yml
vendored
4
.github/workflows/CI.yml
vendored
@@ -12,10 +12,10 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2.3.3
|
||||||
|
|
||||||
- name: Install .NET Core
|
- name: Install .NET Core
|
||||||
uses: actions/setup-dotnet@v1.4.0
|
uses: actions/setup-dotnet@v1.7.2
|
||||||
with:
|
with:
|
||||||
dotnet-version: 3.1.100
|
dotnet-version: 3.1.100
|
||||||
|
|
||||||
|
|||||||
11
Changelog.md
11
Changelog.md
@@ -1,3 +1,14 @@
|
|||||||
|
### 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)
|
### 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 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))
|
||||||
|
|||||||
@@ -11,14 +11,14 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Gu.Roslyn.Asserts" Version="3.3.0" />
|
<PackageReference Include="Gu.Roslyn.Asserts" Version="3.3.1" />
|
||||||
<PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" />
|
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.1" />
|
||||||
<PackageReference Include="FluentAssertions" Version="5.10.2" />
|
<PackageReference Include="FluentAssertions" Version="5.10.3" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||||
<PackageReference Include="xunit" Version="2.4.0" />
|
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" />
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" />
|
||||||
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
|
<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>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -140,6 +140,30 @@ public class MyCommand : ICommand
|
|||||||
[CommandParameter(2)]
|
[CommandParameter(2)]
|
||||||
public IReadOnlyList<string> ParamB { get; set; }
|
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;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
)
|
)
|
||||||
@@ -157,7 +181,7 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption(""foo"")]
|
[CommandOption(""foo"")]
|
||||||
public string Param { get; set; }
|
public string Option { get; set; }
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
@@ -176,7 +200,7 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption(""foo"", 'f')]
|
[CommandOption(""foo"", 'f')]
|
||||||
public string Param { get; set; }
|
public string Option { get; set; }
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
@@ -195,10 +219,10 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption(""foo"")]
|
[CommandOption(""foo"")]
|
||||||
public string ParamA { get; set; }
|
public string OptionA { get; set; }
|
||||||
|
|
||||||
[CommandOption(""bar"")]
|
[CommandOption(""bar"")]
|
||||||
public string ParamB { get; set; }
|
public string OptionB { get; set; }
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
@@ -217,10 +241,10 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption('f')]
|
[CommandOption('f')]
|
||||||
public string ParamA { get; set; }
|
public string OptionA { get; set; }
|
||||||
|
|
||||||
[CommandOption('x')]
|
[CommandOption('x')]
|
||||||
public string ParamB { get; set; }
|
public string OptionB { get; set; }
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
@@ -239,11 +263,35 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption('a', EnvironmentVariableName = ""env_var_a"")]
|
[CommandOption('a', EnvironmentVariableName = ""env_var_a"")]
|
||||||
public string ParamA { get; set; }
|
public string OptionA { get; set; }
|
||||||
|
|
||||||
[CommandOption('b', EnvironmentVariableName = ""env_var_b"")]
|
[CommandOption('b', EnvironmentVariableName = ""env_var_b"")]
|
||||||
public string ParamB { get; set; }
|
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 ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
)
|
)
|
||||||
@@ -366,6 +414,30 @@ public class MyCommand : ICommand
|
|||||||
[CommandParameter(2)]
|
[CommandParameter(2)]
|
||||||
public string ParamB { get; set; }
|
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;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
)
|
)
|
||||||
@@ -383,7 +455,7 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption("""")]
|
[CommandOption("""")]
|
||||||
public string Param { get; set; }
|
public string Option { get; set; }
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
@@ -402,7 +474,7 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption(""a"")]
|
[CommandOption(""a"")]
|
||||||
public string Param { get; set; }
|
public string Option { get; set; }
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
@@ -421,10 +493,10 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption(""foo"")]
|
[CommandOption(""foo"")]
|
||||||
public string ParamA { get; set; }
|
public string OptionA { get; set; }
|
||||||
|
|
||||||
[CommandOption(""foo"")]
|
[CommandOption(""foo"")]
|
||||||
public string ParamB { get; set; }
|
public string OptionB { get; set; }
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
@@ -443,10 +515,10 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption('f')]
|
[CommandOption('f')]
|
||||||
public string ParamA { get; set; }
|
public string OptionA { get; set; }
|
||||||
|
|
||||||
[CommandOption('f')]
|
[CommandOption('f')]
|
||||||
public string ParamB { get; set; }
|
public string OptionB { get; set; }
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
@@ -465,11 +537,35 @@ public class MyCommand : ICommand
|
|||||||
public class MyCommand : ICommand
|
public class MyCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption('a', EnvironmentVariableName = ""env_var"")]
|
[CommandOption('a', EnvironmentVariableName = ""env_var"")]
|
||||||
public string ParamA { get; set; }
|
public string OptionA { get; set; }
|
||||||
|
|
||||||
[CommandOption('b', EnvironmentVariableName = ""env_var"")]
|
[CommandOption('b', EnvironmentVariableName = ""env_var"")]
|
||||||
public string ParamB { get; set; }
|
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;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}"
|
}"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -17,16 +17,19 @@ namespace CliFx.Analyzers
|
|||||||
DiagnosticDescriptors.CliFx0022,
|
DiagnosticDescriptors.CliFx0022,
|
||||||
DiagnosticDescriptors.CliFx0023,
|
DiagnosticDescriptors.CliFx0023,
|
||||||
DiagnosticDescriptors.CliFx0024,
|
DiagnosticDescriptors.CliFx0024,
|
||||||
|
DiagnosticDescriptors.CliFx0025,
|
||||||
DiagnosticDescriptors.CliFx0041,
|
DiagnosticDescriptors.CliFx0041,
|
||||||
DiagnosticDescriptors.CliFx0042,
|
DiagnosticDescriptors.CliFx0042,
|
||||||
DiagnosticDescriptors.CliFx0043,
|
DiagnosticDescriptors.CliFx0043,
|
||||||
DiagnosticDescriptors.CliFx0044,
|
DiagnosticDescriptors.CliFx0044,
|
||||||
DiagnosticDescriptors.CliFx0045
|
DiagnosticDescriptors.CliFx0045,
|
||||||
|
DiagnosticDescriptors.CliFx0046
|
||||||
);
|
);
|
||||||
|
|
||||||
private static bool IsScalarType(ITypeSymbol typeSymbol) =>
|
private static bool IsScalarType(ITypeSymbol typeSymbol) =>
|
||||||
KnownSymbols.IsSystemString(typeSymbol) ||
|
KnownSymbols.IsSystemString(typeSymbol) ||
|
||||||
!typeSymbol.AllInterfaces.Select(i => i.ConstructedFrom).Any(KnownSymbols.IsSystemCollectionsGenericIEnumerable);
|
!typeSymbol.AllInterfaces.Select(i => i.ConstructedFrom)
|
||||||
|
.Any(KnownSymbols.IsSystemCollectionsGenericIEnumerable);
|
||||||
|
|
||||||
private static void CheckCommandParameterProperties(
|
private static void CheckCommandParameterProperties(
|
||||||
SymbolAnalysisContext context,
|
SymbolAnalysisContext context,
|
||||||
@@ -50,11 +53,18 @@ namespace CliFx.Analyzers
|
|||||||
.Select(a => a.Value.Value)
|
.Select(a => a.Value.Value)
|
||||||
.FirstOrDefault() as string;
|
.FirstOrDefault() as string;
|
||||||
|
|
||||||
|
var converter = attribute
|
||||||
|
.NamedArguments
|
||||||
|
.Where(a => a.Key == "Converter")
|
||||||
|
.Select(a => a.Value.Value)
|
||||||
|
.FirstOrDefault() as ITypeSymbol;
|
||||||
|
|
||||||
return new
|
return new
|
||||||
{
|
{
|
||||||
Property = p,
|
Property = p,
|
||||||
Order = order,
|
Order = order,
|
||||||
Name = name
|
Name = name,
|
||||||
|
Converter = converter
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.ToArray();
|
.ToArray();
|
||||||
@@ -69,8 +79,9 @@ namespace CliFx.Analyzers
|
|||||||
|
|
||||||
foreach (var parameter in duplicateOrderParameters)
|
foreach (var parameter in duplicateOrderParameters)
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0021, parameter.Property.Locations.First()));
|
DiagnosticDescriptors.CliFx0021, parameter.Property.Locations.First()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Duplicate name
|
// Duplicate name
|
||||||
@@ -83,8 +94,9 @@ namespace CliFx.Analyzers
|
|||||||
|
|
||||||
foreach (var parameter in duplicateNameParameters)
|
foreach (var parameter in duplicateNameParameters)
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0022, parameter.Property.Locations.First()));
|
DiagnosticDescriptors.CliFx0022, parameter.Property.Locations.First()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multiple non-scalar
|
// Multiple non-scalar
|
||||||
@@ -96,8 +108,9 @@ namespace CliFx.Analyzers
|
|||||||
{
|
{
|
||||||
foreach (var parameter in nonScalarParameters)
|
foreach (var parameter in nonScalarParameters)
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0023, parameter.Property.Locations.First()));
|
DiagnosticDescriptors.CliFx0023, parameter.Property.Locations.First()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,8 +122,23 @@ namespace CliFx.Analyzers
|
|||||||
|
|
||||||
if (nonLastNonScalarParameter != null)
|
if (nonLastNonScalarParameter != null)
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0024, nonLastNonScalarParameter.Property.Locations.First()));
|
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()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,12 +171,19 @@ namespace CliFx.Analyzers
|
|||||||
.Select(a => a.Value.Value)
|
.Select(a => a.Value.Value)
|
||||||
.FirstOrDefault() as string;
|
.FirstOrDefault() as string;
|
||||||
|
|
||||||
|
var converter = attribute
|
||||||
|
.NamedArguments
|
||||||
|
.Where(a => a.Key == "Converter")
|
||||||
|
.Select(a => a.Value.Value)
|
||||||
|
.FirstOrDefault() as ITypeSymbol;
|
||||||
|
|
||||||
return new
|
return new
|
||||||
{
|
{
|
||||||
Property = p,
|
Property = p,
|
||||||
Name = name,
|
Name = name,
|
||||||
ShortName = shortName,
|
ShortName = shortName,
|
||||||
EnvironmentVariableName = envVarName
|
EnvironmentVariableName = envVarName,
|
||||||
|
Converter = converter
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.ToArray();
|
.ToArray();
|
||||||
@@ -160,8 +195,9 @@ namespace CliFx.Analyzers
|
|||||||
|
|
||||||
foreach (var option in noNameOptions)
|
foreach (var option in noNameOptions)
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0041, option.Property.Locations.First()));
|
DiagnosticDescriptors.CliFx0041, option.Property.Locations.First()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Too short name
|
// Too short name
|
||||||
@@ -171,8 +207,9 @@ namespace CliFx.Analyzers
|
|||||||
|
|
||||||
foreach (var option in invalidNameLengthOptions)
|
foreach (var option in invalidNameLengthOptions)
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0042, option.Property.Locations.First()));
|
DiagnosticDescriptors.CliFx0042, option.Property.Locations.First()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Duplicate name
|
// Duplicate name
|
||||||
@@ -185,8 +222,9 @@ namespace CliFx.Analyzers
|
|||||||
|
|
||||||
foreach (var option in duplicateNameOptions)
|
foreach (var option in duplicateNameOptions)
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0043, option.Property.Locations.First()));
|
DiagnosticDescriptors.CliFx0043, option.Property.Locations.First()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Duplicate name
|
// Duplicate name
|
||||||
@@ -199,8 +237,9 @@ namespace CliFx.Analyzers
|
|||||||
|
|
||||||
foreach (var option in duplicateShortNameOptions)
|
foreach (var option in duplicateShortNameOptions)
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0044, option.Property.Locations.First()));
|
DiagnosticDescriptors.CliFx0044, option.Property.Locations.First()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Duplicate environment variable name
|
// Duplicate environment variable name
|
||||||
@@ -213,19 +252,31 @@ namespace CliFx.Analyzers
|
|||||||
|
|
||||||
foreach (var option in duplicateEnvironmentVariableNameOptions)
|
foreach (var option in duplicateEnvironmentVariableNameOptions)
|
||||||
{
|
{
|
||||||
context.ReportDiagnostic(
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0045, option.Property.Locations.First()));
|
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)
|
private static void CheckCommandType(SymbolAnalysisContext context)
|
||||||
{
|
{
|
||||||
// Named type: MyCommand
|
// Named type: MyCommand
|
||||||
if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol))
|
if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol) ||
|
||||||
return;
|
namedTypeSymbol.TypeKind != TypeKind.Class)
|
||||||
|
|
||||||
// Only classes
|
|
||||||
if (namedTypeSymbol.TypeKind != TypeKind.Class)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Implements ICommand?
|
// Implements ICommand?
|
||||||
@@ -252,10 +303,12 @@ namespace CliFx.Analyzers
|
|||||||
var isAlmostValidCommandType = implementsCommandInterface ^ hasCommandAttribute;
|
var isAlmostValidCommandType = implementsCommandInterface ^ hasCommandAttribute;
|
||||||
|
|
||||||
if (isAlmostValidCommandType && !implementsCommandInterface)
|
if (isAlmostValidCommandType && !implementsCommandInterface)
|
||||||
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0001, namedTypeSymbol.Locations.First()));
|
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0001,
|
||||||
|
namedTypeSymbol.Locations.First()));
|
||||||
|
|
||||||
if (isAlmostValidCommandType && !hasCommandAttribute)
|
if (isAlmostValidCommandType && !hasCommandAttribute)
|
||||||
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0002, namedTypeSymbol.Locations.First()));
|
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0002,
|
||||||
|
namedTypeSymbol.Locations.First()));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,55 +18,49 @@ namespace CliFx.Analyzers
|
|||||||
SyntaxNodeAnalysisContext context,
|
SyntaxNodeAnalysisContext context,
|
||||||
InvocationExpressionSyntax invocationSyntax)
|
InvocationExpressionSyntax invocationSyntax)
|
||||||
{
|
{
|
||||||
// Get the method member access (Console.WriteLine or Console.Error.WriteLine)
|
if (invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax &&
|
||||||
if (!(invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax))
|
context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol)
|
||||||
return false;
|
{
|
||||||
|
// Direct call to System.Console (e.g. System.Console.WriteLine())
|
||||||
|
if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Get the semantic model for the invoked method
|
// Indirect call to System.Console (e.g. System.Console.Error.WriteLine())
|
||||||
if (!(context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol))
|
if (memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax &&
|
||||||
return false;
|
context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol)
|
||||||
|
{
|
||||||
// Check if contained within System.Console
|
return KnownSymbols.IsSystemConsole(propertySymbol.ContainingType);
|
||||||
if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType))
|
}
|
||||||
return true;
|
}
|
||||||
|
|
||||||
// In case with Console.Error.WriteLine that wouldn't work, we need to check parent member access too
|
|
||||||
if (!(memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Get the semantic model for the parent member
|
|
||||||
if (!(context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Check if contained within System.Console
|
|
||||||
if (KnownSymbols.IsSystemConsole(propertySymbol.ContainingType))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CheckSystemConsoleUsage(SyntaxNodeAnalysisContext context)
|
private static void CheckSystemConsoleUsage(SyntaxNodeAnalysisContext context)
|
||||||
{
|
{
|
||||||
if (!(context.Node is InvocationExpressionSyntax invocationSyntax))
|
if (context.Node is InvocationExpressionSyntax invocationSyntax &&
|
||||||
return;
|
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 (!IsSystemConsoleInvocation(context, invocationSyntax))
|
if (isConsoleInterfaceAvailable)
|
||||||
return;
|
{
|
||||||
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
// Check if IConsole is available in the scope as a viable alternative
|
DiagnosticDescriptors.CliFx0100,
|
||||||
var isConsoleInterfaceAvailable = invocationSyntax
|
invocationSyntax.GetLocation()
|
||||||
.Ancestors()
|
));
|
||||||
.OfType<MethodDeclarationSyntax>()
|
}
|
||||||
.SelectMany(m => m.ParameterList.Parameters)
|
}
|
||||||
.Select(p => p.Type)
|
|
||||||
.Select(t => context.SemanticModel.GetSymbolInfo(t).Symbol)
|
|
||||||
.Where(s => s != null)
|
|
||||||
.Any(KnownSymbols.IsConsoleInterface!);
|
|
||||||
|
|
||||||
if (!isConsoleInterfaceAvailable)
|
|
||||||
return;
|
|
||||||
|
|
||||||
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0100, invocationSyntax.GetLocation()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Initialize(AnalysisContext context)
|
public override void Initialize(AnalysisContext context)
|
||||||
|
|||||||
@@ -8,72 +8,98 @@ namespace CliFx.Analyzers
|
|||||||
new DiagnosticDescriptor(nameof(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",
|
||||||
"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);
|
"Usage", DiagnosticSeverity.Error, true
|
||||||
|
);
|
||||||
|
|
||||||
public static readonly DiagnosticDescriptor CliFx0002 =
|
public static readonly DiagnosticDescriptor CliFx0002 =
|
||||||
new DiagnosticDescriptor(nameof(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",
|
||||||
"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);
|
"Usage", DiagnosticSeverity.Error, true
|
||||||
|
);
|
||||||
|
|
||||||
public static readonly DiagnosticDescriptor CliFx0021 =
|
public static readonly DiagnosticDescriptor CliFx0021 =
|
||||||
new DiagnosticDescriptor(nameof(CliFx0021),
|
new DiagnosticDescriptor(nameof(CliFx0021),
|
||||||
"Parameter order must be unique within its command",
|
"Parameter order must be unique within its command",
|
||||||
"Parameter order must be unique within its command",
|
"Parameter order must be unique within its command",
|
||||||
"Usage", DiagnosticSeverity.Error, true);
|
"Usage", DiagnosticSeverity.Error, true
|
||||||
|
);
|
||||||
|
|
||||||
public static readonly DiagnosticDescriptor CliFx0022 =
|
public static readonly DiagnosticDescriptor CliFx0022 =
|
||||||
new DiagnosticDescriptor(nameof(CliFx0022),
|
new DiagnosticDescriptor(nameof(CliFx0022),
|
||||||
"Parameter order must have unique name within its command",
|
"Parameter order must have unique name within its command",
|
||||||
"Parameter order must have unique name within its command",
|
"Parameter order must have unique name within its command",
|
||||||
"Usage", DiagnosticSeverity.Error, true);
|
"Usage", DiagnosticSeverity.Error, true
|
||||||
|
);
|
||||||
|
|
||||||
public static readonly DiagnosticDescriptor CliFx0023 =
|
public static readonly DiagnosticDescriptor CliFx0023 =
|
||||||
new DiagnosticDescriptor(nameof(CliFx0023),
|
new DiagnosticDescriptor(nameof(CliFx0023),
|
||||||
"Only one non-scalar parameter per command is allowed",
|
"Only one non-scalar parameter per command is allowed",
|
||||||
"Only one non-scalar parameter per command is allowed",
|
"Only one non-scalar parameter per command is allowed",
|
||||||
"Usage", DiagnosticSeverity.Error, true);
|
"Usage", DiagnosticSeverity.Error, true
|
||||||
|
);
|
||||||
|
|
||||||
public static readonly DiagnosticDescriptor CliFx0024 =
|
public static readonly DiagnosticDescriptor CliFx0024 =
|
||||||
new DiagnosticDescriptor(nameof(CliFx0024),
|
new DiagnosticDescriptor(nameof(CliFx0024),
|
||||||
"Non-scalar parameter must be last in order",
|
"Non-scalar parameter must be last in order",
|
||||||
"Non-scalar parameter must be last in order",
|
"Non-scalar parameter must be last in order",
|
||||||
"Usage", DiagnosticSeverity.Error, true);
|
"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 =
|
public static readonly DiagnosticDescriptor CliFx0041 =
|
||||||
new DiagnosticDescriptor(nameof(CliFx0041),
|
new DiagnosticDescriptor(nameof(CliFx0041),
|
||||||
"Option must have a name or short name specified",
|
"Option must have a name or short name specified",
|
||||||
"Option must have a name or short name specified",
|
"Option must have a name or short name specified",
|
||||||
"Usage", DiagnosticSeverity.Error, true);
|
"Usage", DiagnosticSeverity.Error, true
|
||||||
|
);
|
||||||
|
|
||||||
public static readonly DiagnosticDescriptor CliFx0042 =
|
public static readonly DiagnosticDescriptor CliFx0042 =
|
||||||
new DiagnosticDescriptor(nameof(CliFx0042),
|
new DiagnosticDescriptor(nameof(CliFx0042),
|
||||||
"Option name must be at least 2 characters long",
|
"Option name must be at least 2 characters long",
|
||||||
"Option name must be at least 2 characters long",
|
"Option name must be at least 2 characters long",
|
||||||
"Usage", DiagnosticSeverity.Error, true);
|
"Usage", DiagnosticSeverity.Error, true
|
||||||
|
);
|
||||||
|
|
||||||
public static readonly DiagnosticDescriptor CliFx0043 =
|
public static readonly DiagnosticDescriptor CliFx0043 =
|
||||||
new DiagnosticDescriptor(nameof(CliFx0043),
|
new DiagnosticDescriptor(nameof(CliFx0043),
|
||||||
"Option name must be unique within its command",
|
"Option name must be unique within its command",
|
||||||
"Option name must be unique within its command",
|
"Option name must be unique within its command",
|
||||||
"Usage", DiagnosticSeverity.Error, true);
|
"Usage", DiagnosticSeverity.Error, true
|
||||||
|
);
|
||||||
|
|
||||||
public static readonly DiagnosticDescriptor CliFx0044 =
|
public static readonly DiagnosticDescriptor CliFx0044 =
|
||||||
new DiagnosticDescriptor(nameof(CliFx0044),
|
new DiagnosticDescriptor(nameof(CliFx0044),
|
||||||
"Option short name must be unique within its command",
|
"Option short name must be unique within its command",
|
||||||
"Option short name must be unique within its command",
|
"Option short name must be unique within its command",
|
||||||
"Usage", DiagnosticSeverity.Error, true);
|
"Usage", DiagnosticSeverity.Error, true
|
||||||
|
);
|
||||||
|
|
||||||
public static readonly DiagnosticDescriptor CliFx0045 =
|
public static readonly DiagnosticDescriptor CliFx0045 =
|
||||||
new DiagnosticDescriptor(nameof(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",
|
||||||
"Option environment variable name must be unique within its command",
|
"Option environment variable name must be unique within its command",
|
||||||
"Usage", DiagnosticSeverity.Error, true);
|
"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 =
|
public static readonly DiagnosticDescriptor CliFx0100 =
|
||||||
new DiagnosticDescriptor(nameof(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",
|
||||||
"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);
|
"Usage", DiagnosticSeverity.Warning, true
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@ using Microsoft.CodeAnalysis;
|
|||||||
|
|
||||||
namespace CliFx.Analyzers
|
namespace CliFx.Analyzers
|
||||||
{
|
{
|
||||||
public static class KnownSymbols
|
internal static class KnownSymbols
|
||||||
{
|
{
|
||||||
public static bool IsSystemString(ISymbol symbol) =>
|
public static bool IsSystemString(ISymbol symbol) =>
|
||||||
symbol.DisplayNameMatches("string") ||
|
symbol.DisplayNameMatches("string") ||
|
||||||
@@ -25,6 +25,9 @@ namespace CliFx.Analyzers
|
|||||||
public static bool IsCommandInterface(ISymbol symbol) =>
|
public static bool IsCommandInterface(ISymbol symbol) =>
|
||||||
symbol.DisplayNameMatches("CliFx.ICommand");
|
symbol.DisplayNameMatches("CliFx.ICommand");
|
||||||
|
|
||||||
|
public static bool IsArgumentValueConverterInterface(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("CliFx.IArgumentValueConverter");
|
||||||
|
|
||||||
public static bool IsCommandAttribute(ISymbol symbol) =>
|
public static bool IsCommandAttribute(ISymbol symbol) =>
|
||||||
symbol.DisplayNameMatches("CliFx.Attributes.CommandAttribute");
|
symbol.DisplayNameMatches("CliFx.Attributes.CommandAttribute");
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CliFx.Benchmarks
|
|||||||
|
|
||||||
[Benchmark(Description = "CliFx", Baseline = true)]
|
[Benchmark(Description = "CliFx", Baseline = true)]
|
||||||
public async ValueTask<int> ExecuteWithCliFx() =>
|
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")]
|
[Benchmark(Description = "System.CommandLine")]
|
||||||
public async Task<int> ExecuteWithSystemCommandLine() =>
|
public async Task<int> ExecuteWithSystemCommandLine() =>
|
||||||
|
|||||||
@@ -9,9 +9,9 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
|
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
|
||||||
<PackageReference Include="clipr" Version="1.6.1" />
|
<PackageReference Include="clipr" Version="1.6.1" />
|
||||||
<PackageReference Include="Cocona" Version="1.3.0" />
|
<PackageReference Include="Cocona" Version="1.5.0" />
|
||||||
<PackageReference Include="CommandLineParser" Version="2.7.82" />
|
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
||||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.6.0" />
|
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="3.0.0" />
|
||||||
<PackageReference Include="PowerArgs" Version="3.6.0" />
|
<PackageReference Include="PowerArgs" Version="3.6.0" />
|
||||||
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" />
|
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.8" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -1,179 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
public partial class ApplicationSpecs
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
private class DefaultCommand : ICommand
|
|
||||||
{
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command]
|
|
||||||
private class AnotherDefaultCommand : ICommand
|
|
||||||
{
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
|
|
||||||
[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 EmptyOptionNameCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("")]
|
|
||||||
public string? Apples { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command]
|
|
||||||
private class SingleCharacterOptionNameCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("a")]
|
|
||||||
public string? Apples { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command]
|
|
||||||
private class DuplicateOptionNamesCommand : ICommand
|
|
||||||
{
|
|
||||||
[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 ConflictWithHelpOptionCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("option-h", 'h')]
|
|
||||||
public string? OptionH { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command]
|
|
||||||
private class ConflictWithVersionOptionCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("version")]
|
|
||||||
public string? Version { 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("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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using CliFx.Domain;
|
using System.Threading.Tasks;
|
||||||
using CliFx.Exceptions;
|
using CliFx.Tests.Commands;
|
||||||
|
using CliFx.Tests.Commands.Invalid;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Xunit.Abstractions;
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class ApplicationSpecs
|
public class ApplicationSpecs
|
||||||
{
|
{
|
||||||
private readonly ITestOutputHelper _output;
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ namespace CliFx.Tests
|
|||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
var app = new CliApplicationBuilder()
|
var app = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(DefaultCommand))
|
.AddCommand<DefaultCommand>()
|
||||||
.AddCommandsFrom(typeof(DefaultCommand).Assembly)
|
.AddCommandsFrom(typeof(DefaultCommand).Assembly)
|
||||||
.AddCommands(new[] {typeof(DefaultCommand)})
|
.AddCommands(new[] {typeof(DefaultCommand)})
|
||||||
.AddCommandsFrom(new[] {typeof(DefaultCommand).Assembly})
|
.AddCommandsFrom(new[] {typeof(DefaultCommand).Assembly})
|
||||||
@@ -51,219 +52,364 @@ namespace CliFx.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[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 (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
var commandTypes = Array.Empty<Type>();
|
|
||||||
|
|
||||||
// Act & assert
|
var application = new CliApplicationBuilder()
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
.UseConsole(console)
|
||||||
_output.WriteLine(ex.Message);
|
.Build();
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Commands_must_implement_the_corresponding_interface()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(NonImplementedCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Commands_must_be_annotated_by_an_attribute()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Commands_must_have_unique_names()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_can_be_default_but_only_if_it_is_the_only_such_command()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(DefaultCommand), typeof(AnotherDefaultCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_parameters_must_have_unique_order()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_parameters_must_have_unique_names()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[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
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[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
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_options_must_have_names_that_are_not_empty()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(EmptyOptionNameCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_options_must_have_names_that_are_longer_than_one_character()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(SingleCharacterOptionNameCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_options_must_have_unique_names()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_options_must_have_unique_short_names()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_options_must_not_have_conflicts_with_the_implicit_help_option()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(ConflictWithHelpOptionCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_options_must_not_have_conflicts_with_the_implicit_version_option()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(ConflictWithVersionOptionCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_options_must_have_unique_environment_variable_names()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
|
|
||||||
|
|
||||||
// Act & assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
|
||||||
_output.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Command_options_and_parameters_must_be_annotated_by_corresponding_attributes()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(HiddenPropertiesCommand)};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var schema = RootSchema.Resolve(commandTypes);
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
schema.Should().BeEquivalentTo(new RootSchema(new[]
|
exitCode.Should().NotBe(0);
|
||||||
{
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
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"),
|
|
||||||
CommandOptionSchema.HelpOption
|
|
||||||
})
|
|
||||||
}));
|
|
||||||
|
|
||||||
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,197 +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(Option), IsRequired = true)]
|
|
||||||
public string? Option { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command]
|
|
||||||
private class RequiredArrayOptionCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption(nameof(Option), IsRequired = true)]
|
|
||||||
public IReadOnlyList<string>? Option { 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,53 +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>
|
|
||||||
{
|
|
||||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
1441
CliFx.Tests/ArgumentConversionSpecs.cs
Normal file
1441
CliFx.Tests/ArgumentConversionSpecs.cs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,378 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Domain;
|
|
||||||
using CliFx.Tests.Internal;
|
|
||||||
using FluentAssertions;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
public class ArgumentSyntaxSpecs
|
|
||||||
{
|
|
||||||
[Fact]
|
|
||||||
public void Input_is_empty_if_no_arguments_are_provided()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var arguments = Array.Empty<string>();
|
|
||||||
var commandNames = Array.Empty<string>();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var input = CommandInput.Parse(arguments, commandNames);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
input.Should().BeEquivalentTo(CommandInput.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static object[][] DirectivesTestData => new[]
|
|
||||||
{
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"[preview]"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddDirective("preview")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"[preview]", "[debug]"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddDirective("preview")
|
|
||||||
.AddDirective("debug")
|
|
||||||
.Build()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[MemberData(nameof(DirectivesTestData))]
|
|
||||||
internal void Directive_can_be_enabled_by_specifying_its_name_in_square_brackets(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandNames = Array.Empty<string>();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var input = CommandInput.Parse(arguments, commandNames);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
input.Should().BeEquivalentTo(expectedInput);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static object[][] OptionsTestData => new[]
|
|
||||||
{
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"--option"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("option")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"--option", "value"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("option", "value")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"--option", "value1", "value2"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("option", "value1", "value2")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"--option", "same value"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("option", "same value")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"--option1", "--option2"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("option1")
|
|
||||||
.AddOption("option2")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"--option1", "value1", "--option2", "value2"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("option1", "value1")
|
|
||||||
.AddOption("option2", "value2")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"--option1", "value1", "value2", "--option2", "value3", "value4"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("option1", "value1", "value2")
|
|
||||||
.AddOption("option2", "value3", "value4")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"--option1", "value1", "value2", "--option2"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("option1", "value1", "value2")
|
|
||||||
.AddOption("option2")
|
|
||||||
.Build()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[MemberData(nameof(OptionsTestData))]
|
|
||||||
internal void Option_can_be_set_by_specifying_its_name_after_two_dashes(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandNames = Array.Empty<string>();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var input = CommandInput.Parse(arguments, commandNames);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
input.Should().BeEquivalentTo(expectedInput);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static object[][] ShortOptionsTestData => new[]
|
|
||||||
{
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-o"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("o")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-o", "value"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("o", "value")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-o", "value1", "value2"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("o", "value1", "value2")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-o", "same value"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("o", "same value")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-a", "-b"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("a")
|
|
||||||
.AddOption("b")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-a", "value1", "-b", "value2"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("a", "value1")
|
|
||||||
.AddOption("b", "value2")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-a", "value1", "value2", "-b", "value3", "value4"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("a", "value1", "value2")
|
|
||||||
.AddOption("b", "value3", "value4")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-a", "value1", "value2", "-b"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("a", "value1", "value2")
|
|
||||||
.AddOption("b")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-abc"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("a")
|
|
||||||
.AddOption("b")
|
|
||||||
.AddOption("c")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-abc", "value"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddOption("a")
|
|
||||||
.AddOption("b")
|
|
||||||
.AddOption("c", "value")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"-abc", "value1", "value2"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.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(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandNames = Array.Empty<string>();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var input = CommandInput.Parse(arguments, commandNames);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
input.Should().BeEquivalentTo(expectedInput);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static object[][] ParametersTestData => new[]
|
|
||||||
{
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"foo"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddParameter("foo")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"foo", "bar"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddParameter("foo")
|
|
||||||
.AddParameter("bar")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"[preview]", "foo"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddDirective("preview")
|
|
||||||
.AddParameter("foo")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"foo", "--option", "value", "-abc"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddParameter("foo")
|
|
||||||
.AddOption("option", "value")
|
|
||||||
.AddOption("a")
|
|
||||||
.AddOption("b")
|
|
||||||
.AddOption("c")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"[preview]", "[debug]", "foo", "bar", "--option", "value", "-abc"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.AddDirective("preview")
|
|
||||||
.AddDirective("debug")
|
|
||||||
.AddParameter("foo")
|
|
||||||
.AddParameter("bar")
|
|
||||||
.AddOption("option", "value")
|
|
||||||
.AddOption("a")
|
|
||||||
.AddOption("b")
|
|
||||||
.AddOption("c")
|
|
||||||
.Build()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[MemberData(nameof(ParametersTestData))]
|
|
||||||
internal void Parameter_can_be_set_by_specifying_the_value_directly(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandNames = Array.Empty<string>();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var input = CommandInput.Parse(arguments, commandNames);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
input.Should().BeEquivalentTo(expectedInput);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static object[][] CommandNameTestData => new[]
|
|
||||||
{
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"cmd"},
|
|
||||||
new[] {"cmd"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.SetCommandName("cmd")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"cmd"},
|
|
||||||
new[] {"cmd", "foo", "bar", "-o", "value"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.SetCommandName("cmd")
|
|
||||||
.AddParameter("foo")
|
|
||||||
.AddParameter("bar")
|
|
||||||
.AddOption("o", "value")
|
|
||||||
.Build()
|
|
||||||
},
|
|
||||||
|
|
||||||
new object[]
|
|
||||||
{
|
|
||||||
new[] {"cmd", "cmd sub"},
|
|
||||||
new[] {"cmd", "sub", "foo"},
|
|
||||||
new CommandInputBuilder()
|
|
||||||
.SetCommandName("cmd sub")
|
|
||||||
.AddParameter("foo")
|
|
||||||
.Build()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[MemberData(nameof(CommandNameTestData))]
|
|
||||||
internal void Command_name_is_matched_from_arguments_that_come_before_parameters(
|
|
||||||
IReadOnlyList<string> commandNames,
|
|
||||||
IReadOnlyList<string> arguments,
|
|
||||||
CommandInput expectedInput)
|
|
||||||
{
|
|
||||||
// Act
|
|
||||||
var input = CommandInput.Parse(arguments, commandNames);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
input.Should().BeEquivalentTo(expectedInput);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class CancellationSpecs
|
public class CancellationSpecs
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested()
|
public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested()
|
||||||
@@ -17,27 +16,21 @@ namespace CliFx.Tests
|
|||||||
|
|
||||||
// Arrange
|
// Arrange
|
||||||
using var cts = new CancellationTokenSource();
|
using var cts = new CancellationTokenSource();
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered(cts.Token);
|
||||||
await using var stdOut = new MemoryStream();
|
|
||||||
var console = new VirtualConsole(output: stdOut, cancellationToken: cts.Token);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(CancellableCommand))
|
.AddCommand<CancellableCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
cts.CancelAfter(TimeSpan.FromSeconds(0.2));
|
cts.CancelAfter(TimeSpan.FromSeconds(0.2));
|
||||||
|
|
||||||
var exitCode = await application.RunAsync(
|
var exitCode = await application.RunAsync(new[] {"cmd"});
|
||||||
new[] {"cancel"},
|
|
||||||
new Dictionary<string, string>());
|
|
||||||
|
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().NotBe(0);
|
exitCode.Should().NotBe(0);
|
||||||
stdOutData.Should().Be("Cancellation requested");
|
stdOut.GetString().Trim().Should().Be(CancellableCommand.CancellationOutputText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,13 +15,14 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CliWrap" Version="3.0.0" />
|
<PackageReference Include="CliWrap" Version="3.2.0" />
|
||||||
<PackageReference Include="FluentAssertions" Version="5.10.2" />
|
<PackageReference Include="FluentAssertions" Version="5.10.3" />
|
||||||
<PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" />
|
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.1" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
<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" 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.8.0" PrivateAssets="all" />
|
<PackageReference Include="coverlet.msbuild" Version="2.9.0" PrivateAssets="all" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
31
CliFx.Tests/Commands/CancellableCommand.cs
Normal file
31
CliFx.Tests/Commands/CancellableCommand.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
CliFx.Tests/Commands/CommandExceptionCommand.cs
Normal file
21
CliFx.Tests/Commands/CommandExceptionCommand.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
CliFx.Tests/Commands/DefaultCommand.cs
Normal file
17
CliFx.Tests/Commands/DefaultCommand.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
CliFx.Tests/Commands/GenericExceptionCommand.cs
Normal file
15
CliFx.Tests/Commands/GenericExceptionCommand.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
19
CliFx.Tests/Commands/GenericInnerExceptionCommand.cs
Normal file
19
CliFx.Tests/Commands/GenericInnerExceptionCommand.cs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
14
CliFx.Tests/Commands/Invalid/DuplicateOptionNamesCommand.cs
Normal file
14
CliFx.Tests/Commands/Invalid/DuplicateOptionNamesCommand.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
11
CliFx.Tests/Commands/Invalid/EmptyOptionNameCommand.cs
Normal file
11
CliFx.Tests/Commands/Invalid/EmptyOptionNameCommand.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
6
CliFx.Tests/Commands/Invalid/NonAnnotatedCommand.cs
Normal file
6
CliFx.Tests/Commands/Invalid/NonAnnotatedCommand.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
public class NonAnnotatedCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
9
CliFx.Tests/Commands/Invalid/NonImplementedCommand.cs
Normal file
9
CliFx.Tests/Commands/Invalid/NonImplementedCommand.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
public class NonImplementedCommand
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
9
CliFx.Tests/Commands/Invalid/OtherDefaultCommand.cs
Normal file
9
CliFx.Tests/Commands/Invalid/OtherDefaultCommand.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
public class OtherDefaultCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
17
CliFx.Tests/Commands/NamedCommand.cs
Normal file
17
CliFx.Tests/Commands/NamedCommand.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
CliFx.Tests/Commands/NamedSubCommand.cs
Normal file
17
CliFx.Tests/Commands/NamedSubCommand.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
CliFx.Tests/Commands/SelfSerializeCommandBase.cs
Normal file
14
CliFx.Tests/Commands/SelfSerializeCommandBase.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
174
CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs
Normal file
174
CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
CliFx.Tests/Commands/UnsupportedArgumentTypesCommand.cs
Normal file
31
CliFx.Tests/Commands/UnsupportedArgumentTypesCommand.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
44
CliFx.Tests/Commands/WithDefaultValuesCommand.cs
Normal file
44
CliFx.Tests/Commands/WithDefaultValuesCommand.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
28
CliFx.Tests/Commands/WithDependenciesCommand.cs
Normal file
28
CliFx.Tests/Commands/WithDependenciesCommand.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
CliFx.Tests/Commands/WithEnumArgumentsCommand.cs
Normal file
19
CliFx.Tests/Commands/WithEnumArgumentsCommand.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
15
CliFx.Tests/Commands/WithEnvironmentVariablesCommand.cs
Normal file
15
CliFx.Tests/Commands/WithEnvironmentVariablesCommand.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
18
CliFx.Tests/Commands/WithParametersCommand.cs
Normal file
18
CliFx.Tests/Commands/WithParametersCommand.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
18
CliFx.Tests/Commands/WithRequiredOptionsCommand.cs
Normal file
18
CliFx.Tests/Commands/WithRequiredOptionsCommand.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
11
CliFx.Tests/Commands/WithSingleParameterCommand.cs
Normal file
11
CliFx.Tests/Commands/WithSingleParameterCommand.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
14
CliFx.Tests/Commands/WithSingleRequiredOptionCommand.cs
Normal file
14
CliFx.Tests/Commands/WithSingleRequiredOptionCommand.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
12
CliFx.Tests/Commands/WithStringArrayOptionCommand.cs
Normal file
12
CliFx.Tests/Commands/WithStringArrayOptionCommand.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,7 +38,8 @@ namespace CliFx.Tests
|
|||||||
var console = new VirtualConsole(
|
var console = new VirtualConsole(
|
||||||
input: stdIn,
|
input: stdIn,
|
||||||
output: stdOut,
|
output: stdOut,
|
||||||
error: stdErr);
|
error: stdErr
|
||||||
|
);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
console.Output.Write("output");
|
console.Output.Write("output");
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Xunit.Abstractions;
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class DependencyInjectionSpecs
|
public class DependencyInjectionSpecs
|
||||||
{
|
{
|
||||||
private readonly ITestOutputHelper _output;
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
@@ -18,10 +19,10 @@ namespace CliFx.Tests
|
|||||||
var activator = new DefaultTypeActivator();
|
var activator = new DefaultTypeActivator();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var obj = activator.CreateInstance(typeof(WithoutDependenciesCommand));
|
var obj = activator.CreateInstance(typeof(DefaultCommand));
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
obj.Should().BeOfType<WithoutDependenciesCommand>();
|
obj.Should().BeOfType<DefaultCommand>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -40,7 +41,10 @@ namespace CliFx.Tests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var activator = new DelegateTypeActivator(_ =>
|
var activator = new DelegateTypeActivator(_ =>
|
||||||
new WithDependenciesCommand(new DependencyA(), new DependencyB()));
|
new WithDependenciesCommand(
|
||||||
|
new WithDependenciesCommand.DependencyA(),
|
||||||
|
new WithDependenciesCommand.DependencyB())
|
||||||
|
);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var obj = activator.CreateInstance(typeof(WithDependenciesCommand));
|
var obj = activator.CreateInstance(typeof(WithDependenciesCommand));
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +1,44 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class DirectivesSpecs
|
public class DirectivesSpecs
|
||||||
{
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public DirectivesSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Preview_directive_can_be_specified_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
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(output: stdOut);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(NamedCommand))
|
.AddCommand<NamedCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.AllowPreviewMode()
|
.AllowPreviewMode()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exitCode = await application.RunAsync(
|
var exitCode = await application.RunAsync(
|
||||||
new[] {"[preview]", "cmd", "param", "-abc", "--option", "foo"},
|
new[] {"[preview]", "named", "param", "-abc", "--option", "foo"},
|
||||||
new Dictionary<string, string>());
|
new Dictionary<string, string>()
|
||||||
|
);
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().Be(0);
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CliFx.Domain;
|
using CliFx.Tests.Commands;
|
||||||
using CliFx.Tests.Internal;
|
using CliFx.Tests.Internal;
|
||||||
using CliWrap;
|
using CliWrap;
|
||||||
using CliWrap.Buffered;
|
using CliWrap.Buffered;
|
||||||
@@ -10,11 +10,11 @@ using Xunit;
|
|||||||
|
|
||||||
namespace CliFx.Tests
|
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
|
// This test uses a real application to make sure environment variables are actually read correctly
|
||||||
[Fact]
|
[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
|
// Arrange
|
||||||
var command = Cli.Wrap("dotnet")
|
var command = Cli.Wrap("dotnet")
|
||||||
@@ -27,12 +27,12 @@ namespace CliFx.Tests
|
|||||||
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||||
|
|
||||||
// Assert
|
// 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
|
// This test uses a real application to make sure environment variables are actually read correctly
|
||||||
[Fact]
|
[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
|
// Arrange
|
||||||
var command = Cli.Wrap("dotnet")
|
var command = Cli.Wrap("dotnet")
|
||||||
@@ -47,69 +47,95 @@ namespace CliFx.Tests
|
|||||||
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
stdOut.TrimEnd().Should().Be("Hello Jupiter!");
|
stdOut.Trim().Should().Be("Hello Jupiter!");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[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 (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var input = CommandInput.Empty;
|
|
||||||
var envVars = new Dictionary<string, string>
|
var application = new CliApplicationBuilder()
|
||||||
{
|
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||||
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
.UseConsole(console)
|
||||||
};
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var instance = CommandHelper.ResolveCommand<EnvironmentVariableCollectionCommand>(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
|
// Assert
|
||||||
instance.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand
|
exitCode.Should().Be(0);
|
||||||
|
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
|
||||||
{
|
{
|
||||||
Option = new[] {"foo", "bar"}
|
OptA = "correct"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[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 (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var input = CommandInput.Empty;
|
|
||||||
var envVars = new Dictionary<string, string>
|
var application = new CliApplicationBuilder()
|
||||||
{
|
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||||
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
.UseConsole(console)
|
||||||
};
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var instance = CommandHelper.ResolveCommand<EnvironmentVariableCommand>(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
|
// Assert
|
||||||
instance.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
exitCode.Should().Be(0);
|
||||||
|
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
|
||||||
{
|
{
|
||||||
Option = $"foo{Path.PathSeparator}bar"
|
OptB = new[] {"foo", "bar"}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Option_can_use_a_specific_environment_variable_as_fallback_while_respecting_case()
|
public async Task Option_of_scalar_type_can_use_an_environment_variable_as_fallback_regardless_of_separators()
|
||||||
{
|
{
|
||||||
// Arrange
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
const string expected = "foobar";
|
|
||||||
var input = CommandInput.Empty;
|
var application = new CliApplicationBuilder()
|
||||||
var envVars = new Dictionary<string, string>
|
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||||
{
|
.UseConsole(console)
|
||||||
["ENV_OPT"] = expected,
|
.Build();
|
||||||
["env_opt"] = "2"
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var instance = CommandHelper.ResolveCommand<EnvironmentVariableCommand>(input, envVars);
|
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
|
// Assert
|
||||||
instance.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
exitCode.Should().Be(0);
|
||||||
|
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
|
||||||
{
|
{
|
||||||
Option = expected
|
OptA = $"foo{Path.PathSeparator}bar"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,34 +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; } = 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
using System.Collections.Generic;
|
using System.Threading.Tasks;
|
||||||
using System.IO;
|
using CliFx.Tests.Commands;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Xunit.Abstractions;
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class ErrorReportingSpecs
|
public class ErrorReportingSpecs
|
||||||
{
|
{
|
||||||
private readonly ITestOutputHelper _output;
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
@@ -17,156 +16,159 @@ namespace CliFx.Tests
|
|||||||
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_error_message_and_stack_trace()
|
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_error_message_and_stack_trace()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdErr = new MemoryStream();
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(error: stdErr);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(GenericExceptionCommand))
|
.AddCommand<GenericExceptionCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exitCode = await application.RunAsync(
|
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput"});
|
||||||
new[] {"exc", "-m", "Kaput"},
|
|
||||||
new Dictionary<string, string>());
|
|
||||||
|
|
||||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().NotBe(0);
|
exitCode.Should().NotBe(0);
|
||||||
stdErrData.Should().ContainAll(
|
stdOut.GetString().Should().BeEmpty();
|
||||||
|
stdErr.GetString().Should().ContainAll(
|
||||||
"System.Exception:",
|
"System.Exception:",
|
||||||
"Kaput", "at",
|
"Kaput", "at",
|
||||||
"CliFx.Tests");
|
"CliFx.Tests"
|
||||||
|
);
|
||||||
|
|
||||||
_output.WriteLine(stdErrData);
|
_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]
|
[Fact]
|
||||||
public async Task Command_may_throw_a_specialized_exception_which_exits_with_custom_code_and_prints_minimal_error_details()
|
public async Task Command_may_throw_a_specialized_exception_which_exits_with_custom_code_and_prints_minimal_error_details()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdErr = new MemoryStream();
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(error: stdErr);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(CommandExceptionCommand))
|
.AddCommand<CommandExceptionCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exitCode = await application.RunAsync(
|
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "-c", "69"});
|
||||||
new[] {"exc", "-m", "Kaput", "-c", "69"},
|
|
||||||
new Dictionary<string, string>());
|
|
||||||
|
|
||||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().Be(69);
|
exitCode.Should().Be(69);
|
||||||
stdErrData.Should().Be("Kaput");
|
stdOut.GetString().Should().BeEmpty();
|
||||||
|
stdErr.GetString().Trim().Should().Be("Kaput");
|
||||||
|
|
||||||
_output.WriteLine(stdErrData);
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Command_may_throw_a_specialized_exception_without_error_message_which_exits_and_prints_full_error_details()
|
public async Task Command_may_throw_a_specialized_exception_without_error_message_which_exits_and_prints_full_error_details()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdErr = new MemoryStream();
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(error: stdErr);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(CommandExceptionCommand))
|
.AddCommand<CommandExceptionCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exitCode = await application.RunAsync(
|
var exitCode = await application.RunAsync(new[] {"cmd"});
|
||||||
new[] {"exc"},
|
|
||||||
new Dictionary<string, string>());
|
|
||||||
|
|
||||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().NotBe(0);
|
exitCode.Should().NotBe(0);
|
||||||
stdErrData.Should().ContainAll(
|
stdOut.GetString().Should().BeEmpty();
|
||||||
|
stdErr.GetString().Should().ContainAll(
|
||||||
"CliFx.Exceptions.CommandException:",
|
"CliFx.Exceptions.CommandException:",
|
||||||
"at",
|
"at",
|
||||||
"CliFx.Tests");
|
"CliFx.Tests"
|
||||||
|
);
|
||||||
|
|
||||||
_output.WriteLine(stdErrData);
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Command_may_throw_a_specialized_exception_which_exits_and_prints_help_text()
|
public async Task Command_may_throw_a_specialized_exception_which_exits_and_prints_help_text()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
await using var stdErr = new MemoryStream();
|
|
||||||
|
|
||||||
var console = new VirtualConsole(output: stdOut, error: stdErr);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(CommandExceptionCommand))
|
.AddCommand<CommandExceptionCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exitCode = await application.RunAsync(
|
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "--show-help"});
|
||||||
new[] {"exc", "-m", "Kaput", "--show-help"},
|
|
||||||
new Dictionary<string, string>());
|
|
||||||
|
|
||||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().NotBe(0);
|
exitCode.Should().NotBe(0);
|
||||||
stdErrData.Should().Be("Kaput");
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
|
||||||
stdOutData.Should().ContainAll(
|
|
||||||
"Usage",
|
"Usage",
|
||||||
"Options",
|
"Options",
|
||||||
"-h|--help", "Shows help text."
|
"-h|--help"
|
||||||
);
|
);
|
||||||
|
stdErr.GetString().Trim().Should().Be("Kaput");
|
||||||
|
|
||||||
_output.WriteLine(stdErrData);
|
_output.WriteLine(stdOut.GetString());
|
||||||
_output.WriteLine(stdOutData);
|
_output.WriteLine(stdErr.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Command_shows_help_text_on_invalid_user_input()
|
public async Task Command_shows_help_text_on_invalid_user_input()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
await using var stdErr = new MemoryStream();
|
|
||||||
var console = new VirtualConsole(output: stdOut, error: stdErr);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(CommandExceptionCommand))
|
.AddCommand<DefaultCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exitCode = await application.RunAsync(
|
var exitCode = await application.RunAsync(new[] {"not-a-valid-command", "-r", "foo"});
|
||||||
new[] {"not-a-valid-command", "-r", "foo"},
|
|
||||||
new Dictionary<string, string>());
|
|
||||||
|
|
||||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().NotBe(0);
|
exitCode.Should().NotBe(0);
|
||||||
stdErrData.Should().NotBeNullOrWhiteSpace();
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
|
||||||
stdOutData.Should().ContainAll(
|
|
||||||
"Usage",
|
"Usage",
|
||||||
"[command]",
|
|
||||||
"Options",
|
"Options",
|
||||||
"-h|--help", "Shows help text."
|
"-h|--help"
|
||||||
);
|
);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
_output.WriteLine(stdErrData);
|
_output.WriteLine(stdOut.GetString());
|
||||||
_output.WriteLine(stdOutData);
|
_output.WriteLine(stdErr.GetString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,155 +0,0 @@
|
|||||||
using System;
|
|
||||||
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-a", 'a', IsRequired = true)]
|
|
||||||
public string? OptionA { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("option-b", 'b', IsRequired = true)]
|
|
||||||
public IEnumerable<int>? OptionB { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("option-c", 'c')]
|
|
||||||
public string? OptionC { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command("cmd-with-enum-args")]
|
|
||||||
private class EnumArgumentsCommand : ICommand
|
|
||||||
{
|
|
||||||
public enum CustomEnum { Value1, Value2, Value3 };
|
|
||||||
|
|
||||||
[CommandParameter(0, Name = "value", Description = "Enum parameter.")]
|
|
||||||
public CustomEnum ParamA { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("value", Description = "Enum option.", IsRequired = true)]
|
|
||||||
public CustomEnum OptionA { get; set; } = CustomEnum.Value1;
|
|
||||||
|
|
||||||
[CommandOption("nullable-value", Description = "Nullable enum option.")]
|
|
||||||
public CustomEnum? OptionB { 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command("cmd-with-defaults")]
|
|
||||||
private class ArgumentsWithDefaultValuesCommand : ICommand
|
|
||||||
{
|
|
||||||
public enum CustomEnum { Value1, Value2, Value3 };
|
|
||||||
|
|
||||||
[CommandOption(nameof(Object))]
|
|
||||||
public object? Object { get; set; } = 42;
|
|
||||||
|
|
||||||
[CommandOption(nameof(String))]
|
|
||||||
public string? String { get; set; } = "foo";
|
|
||||||
|
|
||||||
[CommandOption(nameof(EmptyString))]
|
|
||||||
public string EmptyString { get; set; } = "";
|
|
||||||
|
|
||||||
[CommandOption(nameof(Bool))]
|
|
||||||
public bool Bool { get; set; } = true;
|
|
||||||
|
|
||||||
[CommandOption(nameof(Char))]
|
|
||||||
public char Char { get; set; } = 't';
|
|
||||||
|
|
||||||
[CommandOption(nameof(Int))]
|
|
||||||
public int Int { get; set; } = 1337;
|
|
||||||
|
|
||||||
[CommandOption(nameof(TimeSpan))]
|
|
||||||
public TimeSpan TimeSpan { get; set; } = TimeSpan.FromMinutes(123);
|
|
||||||
|
|
||||||
[CommandOption(nameof(Enum))]
|
|
||||||
public CustomEnum Enum { get; set; } = CustomEnum.Value2;
|
|
||||||
|
|
||||||
[CommandOption(nameof(IntNullable))]
|
|
||||||
public int? IntNullable { get; set; } = 1337;
|
|
||||||
|
|
||||||
[CommandOption(nameof(StringArray))]
|
|
||||||
public string[]? StringArray { get; set; } = { "foo", "bar", "baz" };
|
|
||||||
|
|
||||||
[CommandOption(nameof(IntArray))]
|
|
||||||
public int[]? IntArray { get; set; } = { 1, 2, 3 };
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,334 +1,180 @@
|
|||||||
using System.IO;
|
using System.Threading.Tasks;
|
||||||
using System.Threading.Tasks;
|
using CliFx.Tests.Commands;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Xunit.Abstractions;
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class HelpTextSpecs
|
public class HelpTextSpecs
|
||||||
{
|
{
|
||||||
private readonly ITestOutputHelper _output;
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
public HelpTextSpecs(ITestOutputHelper output) => _output = output;
|
public HelpTextSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
[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);
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
|
||||||
}
|
|
||||||
|
|
||||||
[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."
|
|
||||||
);
|
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
|
||||||
}
|
|
||||||
|
|
||||||
[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."
|
|
||||||
);
|
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
|
||||||
}
|
|
||||||
|
|
||||||
[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."
|
|
||||||
);
|
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
|
||||||
}
|
|
||||||
|
|
||||||
[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."
|
|
||||||
);
|
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Help_text_shows_usage_format_which_lists_all_parameters()
|
public async Task Help_text_shows_usage_format_which_lists_all_parameters()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(output: stdOut);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(ParametersCommand))
|
.AddCommand<WithParametersCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await application.RunAsync(new[] {"cmd-with-params", "--help"});
|
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
stdOutData.Should().ContainAll(
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
"Usage",
|
"Usage",
|
||||||
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
|
"cmd", "<parama>", "<paramb>", "<paramc...>"
|
||||||
);
|
);
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
_output.WriteLine(stdOut.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Help_text_shows_usage_format_which_lists_all_required_options()
|
public async Task Help_text_shows_usage_format_which_lists_all_required_options()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(output: stdOut);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(RequiredOptionsCommand))
|
.AddCommand<WithRequiredOptionsCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await application.RunAsync(new[] {"cmd-with-req-opts", "--help"});
|
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
stdOutData.Should().ContainAll(
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
"Usage",
|
"Usage",
|
||||||
"cmd-with-req-opts", "--option-a <value>", "--option-b <values...>", "[options]",
|
"cmd", "--opt-a <value>", "--opt-c <values...>", "[options]",
|
||||||
"Options",
|
"Options",
|
||||||
"* -a|--option-a",
|
"* -a|--opt-a",
|
||||||
"* -b|--option-b",
|
"-b|--opt-b",
|
||||||
"-c|--option-c"
|
"* -c|--opt-c"
|
||||||
);
|
);
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
_output.WriteLine(stdOut.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Help_text_lists_all_valid_values_for_enum_arguments()
|
public async Task Help_text_shows_usage_format_which_lists_available_sub_commands()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(output: stdOut);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(EnumArgumentsCommand))
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await application.RunAsync(new[] {"cmd-with-enum-args", "--help"});
|
var exitCode = await application.RunAsync(new[] {"--help"});
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
stdOutData.Should().ContainAll(
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
"Usage",
|
"Usage",
|
||||||
"cmd-with-enum-args", "[options]",
|
"... 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",
|
"Parameters",
|
||||||
"value", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
"enum", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
||||||
"Options",
|
"Options",
|
||||||
"* --value", "Enum option.", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
"--enum", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
||||||
"--nullable-value", "Nullable enum option.", "Valid values: \"Value1\", \"Value2\", \"Value3\"."
|
"* --required-enum", "Valid values: \"Value1\", \"Value2\", \"Value3\"."
|
||||||
);
|
);
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
_output.WriteLine(stdOut.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Help_text_lists_environment_variable_names_for_options_that_have_them_defined()
|
public async Task Help_text_shows_environment_variable_names_for_options_that_have_them_defined()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(output: stdOut);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(EnvironmentVariableCommand))
|
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await application.RunAsync(new[] {"cmd-with-env-vars", "--help"});
|
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
stdOutData.Should().ContainAll(
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
"Options",
|
"Options",
|
||||||
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
|
"-a|--opt-a", "Environment variable:", "ENV_OPT_A",
|
||||||
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
|
"-b|--opt-b", "Environment variable:", "ENV_OPT_B"
|
||||||
);
|
);
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
_output.WriteLine(stdOut.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Help_text_shows_default_values_for_non_required_options()
|
public async Task Help_text_shows_default_values_for_non_required_options()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(output: stdOut);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(ArgumentsWithDefaultValuesCommand))
|
.AddCommand<WithDefaultValuesCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await application.RunAsync(new[] {"cmd-with-defaults", "--help"});
|
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
stdOutData.Should().ContainAll(
|
exitCode.Should().Be(0);
|
||||||
"Usage",
|
stdOut.GetString().Should().ContainAll(
|
||||||
"cmd-with-defaults", "[options]",
|
|
||||||
"Options",
|
"Options",
|
||||||
"--Object", "Default: \"42\"",
|
"--obj", "Default: \"42\"",
|
||||||
"--String", "Default: \"foo\"",
|
"--str", "Default: \"foo\"",
|
||||||
"--EmptyString", "Default: \"\"",
|
"--str-empty", "Default: \"\"",
|
||||||
"--Bool", "Default: \"True\"",
|
"--str-array", "Default: \"foo\" \"bar\" \"baz\"",
|
||||||
"--Char", "Default: \"t\"",
|
"--bool", "Default: \"True\"",
|
||||||
"--Int", "Default: \"1337\"",
|
"--char", "Default: \"t\"",
|
||||||
"--TimeSpan", "Default: \"02:03:00\"",
|
"--int", "Default: \"1337\"",
|
||||||
"--Enum", "Default: \"Value2\"",
|
"--int-nullable", "Default: \"1337\"",
|
||||||
"--IntNullable", "Default: \"1337\"",
|
"--int-array", "Default: \"1\" \"2\" \"3\"",
|
||||||
"--StringArray", "Default: \"foo\" \"bar\" \"baz\"",
|
"--timespan", "Default: \"02:03:00\"",
|
||||||
"--IntArray", "Default: \"1\" \"2\" \"3\""
|
"--enum", "Default: \"Value2\""
|
||||||
);
|
);
|
||||||
|
|
||||||
_output.WriteLine(stdOutData);
|
_output.WriteLine(stdOut.GetString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Domain;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Internal
|
|
||||||
{
|
|
||||||
internal static class CommandHelper
|
|
||||||
{
|
|
||||||
public static TCommand ResolveCommand<TCommand>(CommandInput input, IReadOnlyDictionary<string, string> environmentVariables)
|
|
||||||
where TCommand : ICommand, new()
|
|
||||||
{
|
|
||||||
var schema = CommandSchema.TryResolve(typeof(TCommand))!;
|
|
||||||
|
|
||||||
var instance = new TCommand();
|
|
||||||
schema.Bind(instance, input, environmentVariables);
|
|
||||||
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TCommand ResolveCommand<TCommand>(CommandInput input)
|
|
||||||
where TCommand : ICommand, new() =>
|
|
||||||
ResolveCommand<TCommand>(input, new Dictionary<string, string>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Domain;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Internal
|
|
||||||
{
|
|
||||||
internal class CommandInputBuilder
|
|
||||||
{
|
|
||||||
private readonly List<CommandDirectiveInput> _directives = new List<CommandDirectiveInput>();
|
|
||||||
private readonly List<CommandParameterInput> _parameters = new List<CommandParameterInput>();
|
|
||||||
private readonly List<CommandOptionInput> _options = new List<CommandOptionInput>();
|
|
||||||
|
|
||||||
private string? _commandName;
|
|
||||||
|
|
||||||
public CommandInputBuilder SetCommandName(string commandName)
|
|
||||||
{
|
|
||||||
_commandName = commandName;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandInputBuilder AddDirective(string directive)
|
|
||||||
{
|
|
||||||
_directives.Add(new CommandDirectiveInput(directive));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandInputBuilder AddParameter(string parameter)
|
|
||||||
{
|
|
||||||
_parameters.Add(new CommandParameterInput(parameter));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandInputBuilder AddOption(string alias, params string[] values)
|
|
||||||
{
|
|
||||||
_options.Add(new CommandOptionInput(alias, values));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandInput Build() => new CommandInput(
|
|
||||||
_directives,
|
|
||||||
_commandName,
|
|
||||||
_parameters,
|
|
||||||
_options
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
10
CliFx.Tests/Internal/JsonExtensions.cs
Normal file
10
CliFx.Tests/Internal/JsonExtensions.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Internal
|
|
||||||
{
|
|
||||||
internal static class TaskExtensions
|
|
||||||
{
|
|
||||||
public static async Task IgnoreCancellation(this Task task)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await task;
|
|
||||||
}
|
|
||||||
catch (OperationCanceledException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
using System;
|
|
||||||
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; } = Array.Empty<string>();
|
|
||||||
|
|
||||||
[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; } = 0;
|
|
||||||
|
|
||||||
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
|
||||||
public double Divisor { get; set; } = 0;
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console)
|
|
||||||
{
|
|
||||||
console.Output.WriteLine(Dividend / Divisor);
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,90 +1,235 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class RoutingSpecs
|
public class RoutingSpecs
|
||||||
{
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public RoutingSpecs(ITestOutputHelper testOutput) => _output = testOutput;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Default_command_is_executed_if_provided_arguments_do_not_match_any_named_command()
|
public async Task Default_command_is_executed_if_provided_arguments_do_not_match_any_named_command()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(output: stdOut);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(DefaultCommand))
|
.AddCommand<DefaultCommand>()
|
||||||
.AddCommand(typeof(ConcatCommand))
|
.AddCommand<NamedCommand>()
|
||||||
.AddCommand(typeof(DivideCommand))
|
.AddCommand<NamedSubCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exitCode = await application.RunAsync(
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
Array.Empty<string>(),
|
|
||||||
new Dictionary<string, string>());
|
|
||||||
|
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().Be(0);
|
exitCode.Should().Be(0);
|
||||||
stdOutData.Should().Be("Hello world!");
|
stdOut.GetString().Trim().Should().Be(DefaultCommand.ExpectedOutputText);
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
_output.WriteLine(stdOut.GetString());
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Specific_named_command_is_executed_if_provided_arguments_match_its_name()
|
public async Task Specific_named_command_is_executed_if_provided_arguments_match_its_name()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdOut = new MemoryStream();
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
var console = new VirtualConsole(output: stdOut);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
var application = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(DefaultCommand))
|
.AddCommand<DefaultCommand>()
|
||||||
.AddCommand(typeof(ConcatCommand))
|
.AddCommand<NamedCommand>()
|
||||||
.AddCommand(typeof(DivideCommand))
|
.AddCommand<NamedSubCommand>()
|
||||||
.UseConsole(console)
|
.UseConsole(console)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exitCode = await application.RunAsync(
|
var exitCode = await application.RunAsync(new[] {"named"});
|
||||||
new[] {"concat", "-i", "foo", "bar", "-s", ", "},
|
|
||||||
new Dictionary<string, string>());
|
|
||||||
|
|
||||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().Be(0);
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
<Project>
|
<Project>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Version>1.3.2</Version>
|
<Version>1.5</Version>
|
||||||
<Company>Tyrrrz</Company>
|
<Company>Tyrrrz</Company>
|
||||||
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<WarningsAsErrors>nullable</WarningsAsErrors>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -28,7 +28,11 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="ApplicationMetadata"/>.
|
/// Initializes an instance of <see cref="ApplicationMetadata"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ApplicationMetadata(string title, string executableName, string versionText, string? description)
|
public ApplicationMetadata(
|
||||||
|
string title,
|
||||||
|
string executableName,
|
||||||
|
string versionText,
|
||||||
|
string? description)
|
||||||
{
|
{
|
||||||
Title = title;
|
Title = title;
|
||||||
ExecutableName = executableName;
|
ExecutableName = executableName;
|
||||||
|
|||||||
@@ -37,6 +37,12 @@ namespace CliFx.Attributes
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string? EnvironmentVariableName { get; set; }
|
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>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
|
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -26,6 +26,12 @@ namespace CliFx.Attributes
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string? Description { get; set; }
|
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>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="CommandParameterAttribute"/>.
|
/// Initializes an instance of <see cref="CommandParameterAttribute"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
@@ -38,9 +39,6 @@ namespace CliFx
|
|||||||
_helpTextWriter = new HelpTextWriter(metadata, console);
|
_helpTextWriter = new HelpTextWriter(metadata, console);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteError(string message) => _console.WithForegroundColor(ConsoleColor.Red, () =>
|
|
||||||
_console.Error.WriteLine(message));
|
|
||||||
|
|
||||||
private async ValueTask LaunchAndWaitForDebuggerAsync()
|
private async ValueTask LaunchAndWaitForDebuggerAsync()
|
||||||
{
|
{
|
||||||
var processId = ProcessEx.GetCurrentProcessId();
|
var processId = ProcessEx.GetCurrentProcessId();
|
||||||
@@ -51,7 +49,9 @@ namespace CliFx
|
|||||||
Debugger.Launch();
|
Debugger.Launch();
|
||||||
|
|
||||||
while (!Debugger.IsAttached)
|
while (!Debugger.IsAttached)
|
||||||
|
{
|
||||||
await Task.Delay(100);
|
await Task.Delay(100);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteCommandLineInput(CommandInput input)
|
private void WriteCommandLineInput(CommandInput input)
|
||||||
@@ -103,9 +103,9 @@ namespace CliFx
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ICommand GetCommandInstance(CommandSchema command) =>
|
private ICommand GetCommandInstance(CommandSchema command) =>
|
||||||
command != StubDefaultCommand.Schema
|
command != FallbackDefaultCommand.Schema
|
||||||
? (ICommand) _typeActivator.CreateInstance(command.Type)
|
? (ICommand) _typeActivator.CreateInstance(command.Type)
|
||||||
: new StubDefaultCommand();
|
: new FallbackDefaultCommand();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
|
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
|
||||||
@@ -141,7 +141,7 @@ namespace CliFx
|
|||||||
var command =
|
var command =
|
||||||
root.TryFindCommand(input.CommandName) ??
|
root.TryFindCommand(input.CommandName) ??
|
||||||
root.TryFindDefaultCommand() ??
|
root.TryFindDefaultCommand() ??
|
||||||
StubDefaultCommand.Schema;
|
FallbackDefaultCommand.Schema;
|
||||||
|
|
||||||
// Version option
|
// Version option
|
||||||
if (command.IsVersionOptionAvailable && input.IsVersionOptionSpecified)
|
if (command.IsVersionOptionAvailable && input.IsVersionOptionSpecified)
|
||||||
@@ -159,7 +159,7 @@ namespace CliFx
|
|||||||
|
|
||||||
// Help option
|
// Help option
|
||||||
if (command.IsHelpOptionAvailable && input.IsHelpOptionSpecified ||
|
if (command.IsHelpOptionAvailable && input.IsHelpOptionSpecified ||
|
||||||
command == StubDefaultCommand.Schema && !input.Parameters.Any() && !input.Options.Any())
|
command == FallbackDefaultCommand.Schema && !input.Parameters.Any() && !input.Options.Any())
|
||||||
{
|
{
|
||||||
_helpTextWriter.Write(root, command, defaultValues);
|
_helpTextWriter.Write(root, command, defaultValues);
|
||||||
return ExitCode.Success;
|
return ExitCode.Success;
|
||||||
@@ -173,7 +173,10 @@ namespace CliFx
|
|||||||
// This may throw exceptions which are useful only to the end-user
|
// This may throw exceptions which are useful only to the end-user
|
||||||
catch (CliFxException ex)
|
catch (CliFxException ex)
|
||||||
{
|
{
|
||||||
WriteError(ex.ToString());
|
_console.WithForegroundColor(ConsoleColor.Red, () =>
|
||||||
|
_console.Error.WriteLine(ex.ToString())
|
||||||
|
);
|
||||||
|
|
||||||
_helpTextWriter.Write(root, command, defaultValues);
|
_helpTextWriter.Write(root, command, defaultValues);
|
||||||
|
|
||||||
return ExitCode.FromException(ex);
|
return ExitCode.FromException(ex);
|
||||||
@@ -188,10 +191,14 @@ namespace CliFx
|
|||||||
// Swallow command exceptions and route them to the console
|
// Swallow command exceptions and route them to the console
|
||||||
catch (CommandException ex)
|
catch (CommandException ex)
|
||||||
{
|
{
|
||||||
WriteError(ex.ToString());
|
_console.WithForegroundColor(ConsoleColor.Red, () =>
|
||||||
|
_console.Error.WriteLine(ex.ToString())
|
||||||
|
);
|
||||||
|
|
||||||
if (ex.ShowHelp)
|
if (ex.ShowHelp)
|
||||||
|
{
|
||||||
_helpTextWriter.Write(root, command, defaultValues);
|
_helpTextWriter.Write(root, command, defaultValues);
|
||||||
|
}
|
||||||
|
|
||||||
return ex.ExitCode;
|
return ex.ExitCode;
|
||||||
}
|
}
|
||||||
@@ -202,7 +209,13 @@ namespace CliFx
|
|||||||
// because we still want the IDE to show them to the developer.
|
// because we still want the IDE to show them to the developer.
|
||||||
catch (Exception ex) when (!Debugger.IsAttached)
|
catch (Exception ex) when (!Debugger.IsAttached)
|
||||||
{
|
{
|
||||||
WriteError(ex.ToString());
|
_console.WithColors(ConsoleColor.White, ConsoleColor.DarkRed, () =>
|
||||||
|
_console.Error.Write("ERROR:")
|
||||||
|
);
|
||||||
|
|
||||||
|
_console.Error.Write(" ");
|
||||||
|
_console.WriteException(ex);
|
||||||
|
|
||||||
return ExitCode.FromException(ex);
|
return ExitCode.FromException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -257,11 +270,14 @@ namespace CliFx
|
|||||||
: 1;
|
: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fallback default command used when none is defined in the application
|
||||||
[Command]
|
[Command]
|
||||||
private class StubDefaultCommand : ICommand
|
private class FallbackDefaultCommand : ICommand
|
||||||
{
|
{
|
||||||
public static CommandSchema Schema { get; } = CommandSchema.TryResolve(typeof(StubDefaultCommand))!;
|
public static CommandSchema Schema { get; } = CommandSchema.TryResolve(typeof(FallbackDefaultCommand))!;
|
||||||
|
|
||||||
|
// Never actually executed
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,12 @@ namespace CliFx
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a command of specified type to the application.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder AddCommand<TCommand>() where TCommand : ICommand =>
|
||||||
|
AddCommand(typeof(TCommand));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds multiple commands to the application.
|
/// Adds multiple commands to the application.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -159,9 +165,9 @@ namespace CliFx
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public CliApplication Build()
|
public CliApplication Build()
|
||||||
{
|
{
|
||||||
_title ??= TryGetDefaultTitle() ?? "App";
|
_title ??= GetDefaultTitle();
|
||||||
_executableName ??= TryGetDefaultExecutableName() ?? "app";
|
_executableName ??= GetDefaultExecutableName();
|
||||||
_versionText ??= TryGetDefaultVersionText() ?? "v1.0";
|
_versionText ??= GetDefaultVersionText();
|
||||||
_console ??= new SystemConsole();
|
_console ??= new SystemConsole();
|
||||||
_typeActivator ??= new DefaultTypeActivator();
|
_typeActivator ??= new DefaultTypeActivator();
|
||||||
|
|
||||||
@@ -179,23 +185,29 @@ namespace CliFx
|
|||||||
// Entry assembly is null in tests
|
// Entry assembly is null in tests
|
||||||
private static Assembly? EntryAssembly => LazyEntryAssembly.Value;
|
private static Assembly? EntryAssembly => LazyEntryAssembly.Value;
|
||||||
|
|
||||||
private static string? TryGetDefaultTitle() => EntryAssembly?.GetName().Name;
|
private static string GetDefaultTitle() => EntryAssembly?.GetName().Name?? "App";
|
||||||
|
|
||||||
private static string? TryGetDefaultExecutableName()
|
private static string GetDefaultExecutableName()
|
||||||
{
|
{
|
||||||
var entryAssemblyLocation = EntryAssembly?.Location;
|
var entryAssemblyLocation = EntryAssembly?.Location;
|
||||||
|
|
||||||
// The assembly can be an executable or a dll, depending on how it was packaged
|
// 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);
|
var isDll = string.Equals(
|
||||||
|
Path.GetExtension(entryAssemblyLocation),
|
||||||
|
".dll",
|
||||||
|
StringComparison.OrdinalIgnoreCase
|
||||||
|
);
|
||||||
|
|
||||||
return isDll
|
var name = isDll
|
||||||
? "dotnet " + Path.GetFileName(entryAssemblyLocation)
|
? "dotnet " + Path.GetFileName(entryAssemblyLocation)
|
||||||
: Path.GetFileNameWithoutExtension(entryAssemblyLocation);
|
: Path.GetFileNameWithoutExtension(entryAssemblyLocation);
|
||||||
|
|
||||||
|
return name ?? "app";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string? TryGetDefaultVersionText() =>
|
private static string GetDefaultVersionText() =>
|
||||||
EntryAssembly != null
|
EntryAssembly != null
|
||||||
? $"v{EntryAssembly.GetName().Version.ToSemanticString()}"
|
? $"v{EntryAssembly.GetName().Version.ToSemanticString()}"
|
||||||
: null;
|
: "v1.0";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,26 +24,17 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
<None Include="../favicon.png" Pack="true" PackagePath="" />
|
||||||
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
|
|
||||||
</AssemblyAttribute>
|
|
||||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
|
||||||
<_Parameter1>$(AssemblyName).Analyzers</_Parameter1>
|
|
||||||
</AssemblyAttribute>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" 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>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" />
|
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<None Include="../favicon.png" Pack="true" PackagePath="" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- The following item group and target ensure that the analyzer project is copied into the output NuGet package -->
|
<!-- The following item group and target ensure that the analyzer project is copied into the output NuGet package -->
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
|
using CliFx.Internal.Extensions;
|
||||||
|
|
||||||
namespace CliFx
|
namespace CliFx
|
||||||
{
|
{
|
||||||
@@ -13,7 +14,7 @@ namespace CliFx
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return Activator.CreateInstance(type);
|
return type.CreateInstance();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,49 +17,64 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public bool IsScalar => TryGetEnumerableArgumentUnderlyingType() == 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;
|
Property = property;
|
||||||
Description = description;
|
Description = description;
|
||||||
|
ConverterType = converterType;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Type? TryGetEnumerableArgumentUnderlyingType() =>
|
private Type? TryGetEnumerableArgumentUnderlyingType() =>
|
||||||
Property != null && Property.PropertyType != typeof(string)
|
Property != null && Property.PropertyType != typeof(string)
|
||||||
? Property.PropertyType.GetEnumerableUnderlyingType()
|
? Property.PropertyType.TryGetEnumerableUnderlyingType()
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
private object? ConvertScalar(string? value, Type targetType)
|
private object? ConvertScalar(string? value, Type targetType)
|
||||||
{
|
{
|
||||||
try
|
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);
|
var primitiveConverter = PrimitiveConverters.GetValueOrDefault(targetType);
|
||||||
if (primitiveConverter != null)
|
if (primitiveConverter != null && !string.IsNullOrWhiteSpace(value))
|
||||||
return primitiveConverter(value);
|
return primitiveConverter(value);
|
||||||
|
|
||||||
// Enum
|
// Enum conversion
|
||||||
if (targetType.IsEnum)
|
if (targetType.IsEnum && !string.IsNullOrWhiteSpace(value))
|
||||||
return Enum.Parse(targetType, value, true);
|
return Enum.Parse(targetType, value, true);
|
||||||
|
|
||||||
// Nullable
|
// Nullable<T> conversion
|
||||||
var nullableUnderlyingType = targetType.GetNullableUnderlyingType();
|
var nullableUnderlyingType = targetType.TryGetNullableUnderlyingType();
|
||||||
if (nullableUnderlyingType != null)
|
if (nullableUnderlyingType != null)
|
||||||
return !string.IsNullOrWhiteSpace(value)
|
return !string.IsNullOrWhiteSpace(value)
|
||||||
? ConvertScalar(value, nullableUnderlyingType)
|
? ConvertScalar(value, nullableUnderlyingType)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
// String-constructable
|
// String-constructible conversion
|
||||||
var stringConstructor = targetType.GetConstructor(new[] {typeof(string)});
|
var stringConstructor = targetType.GetConstructor(new[] {typeof(string)});
|
||||||
if (stringConstructor != null)
|
if (stringConstructor != null)
|
||||||
return stringConstructor.Invoke(new object[] {value!});
|
return stringConstructor.Invoke(new object[] {value!});
|
||||||
|
|
||||||
// String-parseable (with format provider)
|
// String-parseable conversion (with format provider)
|
||||||
var parseMethodWithFormatProvider = targetType.GetStaticParseMethod(true);
|
var parseMethodWithFormatProvider = targetType.TryGetStaticParseMethod(true);
|
||||||
if (parseMethodWithFormatProvider != null)
|
if (parseMethodWithFormatProvider != null)
|
||||||
return parseMethodWithFormatProvider.Invoke(null, new object[] {value!, FormatProvider});
|
return parseMethodWithFormatProvider.Invoke(null, new object[] {value!, FormatProvider});
|
||||||
|
|
||||||
// String-parseable (without format provider)
|
// String-parseable conversion (without format provider)
|
||||||
var parseMethod = targetType.GetStaticParseMethod();
|
var parseMethod = targetType.TryGetStaticParseMethod();
|
||||||
if (parseMethod != null)
|
if (parseMethod != null)
|
||||||
return parseMethod.Invoke(null, new object[] {value!});
|
return parseMethod.Invoke(null, new object[] {value!});
|
||||||
}
|
}
|
||||||
@@ -71,7 +86,10 @@ namespace CliFx.Domain
|
|||||||
throw CliFxException.CannotConvertToType(this, value, targetType);
|
throw CliFxException.CannotConvertToType(this, value, targetType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private object ConvertNonScalar(IReadOnlyList<string> values, Type targetEnumerableType, Type targetElementType)
|
private object ConvertNonScalar(
|
||||||
|
IReadOnlyList<string> values,
|
||||||
|
Type targetEnumerableType,
|
||||||
|
Type targetElementType)
|
||||||
{
|
{
|
||||||
var array = values
|
var array = values
|
||||||
.Select(v => ConvertScalar(v, targetElementType))
|
.Select(v => ConvertScalar(v, targetElementType))
|
||||||
@@ -83,7 +101,7 @@ namespace CliFx.Domain
|
|||||||
if (targetEnumerableType.IsAssignableFrom(arrayType))
|
if (targetEnumerableType.IsAssignableFrom(arrayType))
|
||||||
return array;
|
return array;
|
||||||
|
|
||||||
// Constructable from an array
|
// Constructible from an array
|
||||||
var arrayConstructor = targetEnumerableType.GetConstructor(new[] {arrayType});
|
var arrayConstructor = targetEnumerableType.GetConstructor(new[] {arrayType});
|
||||||
if (arrayConstructor != null)
|
if (arrayConstructor != null)
|
||||||
return arrayConstructor.Invoke(new object[] {array});
|
return arrayConstructor.Invoke(new object[] {array});
|
||||||
@@ -126,7 +144,7 @@ namespace CliFx.Domain
|
|||||||
return Array.Empty<string>();
|
return Array.Empty<string>();
|
||||||
|
|
||||||
var underlyingType =
|
var underlyingType =
|
||||||
Property.PropertyType.GetNullableUnderlyingType() ??
|
Property.PropertyType.TryGetNullableUnderlyingType() ??
|
||||||
Property.PropertyType;
|
Property.PropertyType;
|
||||||
|
|
||||||
// Enum
|
// Enum
|
||||||
@@ -141,12 +159,9 @@ namespace CliFx.Domain
|
|||||||
{
|
{
|
||||||
private static readonly IFormatProvider FormatProvider = CultureInfo.InvariantCulture;
|
private static readonly IFormatProvider FormatProvider = CultureInfo.InvariantCulture;
|
||||||
|
|
||||||
private static readonly IReadOnlyDictionary<Type, Func<string?, object?>> PrimitiveConverters =
|
private static readonly IReadOnlyDictionary<Type, Func<string, object?>> PrimitiveConverters =
|
||||||
new Dictionary<Type, Func<string?, object?>>
|
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(char)] = v => v.Single(),
|
||||||
[typeof(sbyte)] = v => sbyte.Parse(v, FormatProvider),
|
[typeof(sbyte)] = v => sbyte.Parse(v, FormatProvider),
|
||||||
[typeof(byte)] = v => byte.Parse(v, FormatProvider),
|
[typeof(byte)] = v => byte.Parse(v, FormatProvider),
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
namespace CliFx.Domain
|
namespace CliFx.Domain
|
||||||
{
|
{
|
||||||
@@ -12,6 +13,7 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public CommandDirectiveInput(string name) => Name = name;
|
public CommandDirectiveInput(string name) => Name = name;
|
||||||
|
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public override string ToString() => $"[{Name}]";
|
public override string ToString() => $"[{Name}]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using CliFx.Internal.Extensions;
|
using CliFx.Internal.Extensions;
|
||||||
@@ -36,6 +37,7 @@ namespace CliFx.Domain
|
|||||||
Options = options;
|
Options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
var buffer = new StringBuilder();
|
var buffer = new StringBuilder();
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using CliFx.Internal.Extensions;
|
using CliFx.Internal.Extensions;
|
||||||
|
|
||||||
@@ -29,6 +30,7 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public string GetRawValues() => Values.Select(v => v.Quote()).JoinToString(" ");
|
public string GetRawValues() => Values.Select(v => v.Quote()).JoinToString(" ");
|
||||||
|
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public override string ToString() => $"{GetRawAlias()} {GetRawValues()}";
|
public override string ToString() => $"{GetRawAlias()} {GetRawValues()}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -22,8 +23,9 @@ namespace CliFx.Domain
|
|||||||
char? shortName,
|
char? shortName,
|
||||||
string? environmentVariableName,
|
string? environmentVariableName,
|
||||||
bool isRequired,
|
bool isRequired,
|
||||||
string? description)
|
string? description,
|
||||||
: base(property, description)
|
Type? converterType)
|
||||||
|
: base(property, description, converterType)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
ShortName = shortName;
|
ShortName = shortName;
|
||||||
@@ -75,6 +77,7 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ('{GetUserFacingDisplayString()}')";
|
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ('{GetUserFacingDisplayString()}')";
|
||||||
|
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public override string ToString() => GetInternalDisplayString();
|
public override string ToString() => GetInternalDisplayString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +98,8 @@ namespace CliFx.Domain
|
|||||||
attribute.ShortName,
|
attribute.ShortName,
|
||||||
attribute.EnvironmentVariableName,
|
attribute.EnvironmentVariableName,
|
||||||
attribute.IsRequired,
|
attribute.IsRequired,
|
||||||
attribute.Description
|
attribute.Description,
|
||||||
|
attribute.Converter
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,9 +107,9 @@ namespace CliFx.Domain
|
|||||||
internal partial class CommandOptionSchema
|
internal partial class CommandOptionSchema
|
||||||
{
|
{
|
||||||
public static CommandOptionSchema HelpOption { get; } =
|
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; } =
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
namespace CliFx.Domain
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
{
|
{
|
||||||
internal class CommandParameterInput
|
internal class CommandParameterInput
|
||||||
{
|
{
|
||||||
@@ -6,6 +8,7 @@
|
|||||||
|
|
||||||
public CommandParameterInput(string value) => Value = value;
|
public CommandParameterInput(string value) => Value = value;
|
||||||
|
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public override string ToString() => Value;
|
public override string ToString() => Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using System.Reflection;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
|
|
||||||
@@ -10,8 +12,13 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
|
|
||||||
public CommandParameterSchema(PropertyInfo? property, int order, string name, string? description)
|
public CommandParameterSchema(
|
||||||
: base(property, description)
|
PropertyInfo? property,
|
||||||
|
int order,
|
||||||
|
string name,
|
||||||
|
string? description,
|
||||||
|
Type? converterType)
|
||||||
|
: base(property, description, converterType)
|
||||||
{
|
{
|
||||||
Order = order;
|
Order = order;
|
||||||
Name = name;
|
Name = name;
|
||||||
@@ -31,6 +38,7 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ([{Order}] {GetUserFacingDisplayString()})";
|
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ([{Order}] {GetUserFacingDisplayString()})";
|
||||||
|
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public override string ToString() => GetInternalDisplayString();
|
public override string ToString() => GetInternalDisplayString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +56,8 @@ namespace CliFx.Domain
|
|||||||
property,
|
property,
|
||||||
attribute.Order,
|
attribute.Order,
|
||||||
name,
|
name,
|
||||||
attribute.Description
|
attribute.Description,
|
||||||
|
attribute.Converter
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -207,6 +208,7 @@ namespace CliFx.Domain
|
|||||||
return buffer.ToString();
|
return buffer.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public override string ToString() => GetInternalDisplayString();
|
public override string ToString() => GetInternalDisplayString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -105,7 +105,57 @@ namespace CliFx.Domain
|
|||||||
WriteLine();
|
WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteCommandUsage(CommandSchema command, IReadOnlyList<CommandSchema> childCommands)
|
private void WriteCommandUsageLineItem(CommandSchema command, bool showChildCommandsPlaceholder)
|
||||||
|
{
|
||||||
|
// Command name
|
||||||
|
if (!string.IsNullOrWhiteSpace(command.Name))
|
||||||
|
{
|
||||||
|
Write(ConsoleColor.Cyan, command.Name);
|
||||||
|
Write(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Child command placeholder
|
||||||
|
if (showChildCommandsPlaceholder)
|
||||||
|
{
|
||||||
|
Write(ConsoleColor.Cyan, "[command]");
|
||||||
|
Write(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parameters
|
||||||
|
foreach (var parameter in command.Parameters)
|
||||||
|
{
|
||||||
|
Write(parameter.IsScalar
|
||||||
|
? $"<{parameter.Name}>"
|
||||||
|
: $"<{parameter.Name}...>"
|
||||||
|
);
|
||||||
|
Write(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Required options
|
||||||
|
foreach (var option in command.Options.Where(o => o.IsRequired))
|
||||||
|
{
|
||||||
|
Write(ConsoleColor.White, !string.IsNullOrWhiteSpace(option.Name)
|
||||||
|
? $"--{option.Name}"
|
||||||
|
: $"-{option.ShortName}"
|
||||||
|
);
|
||||||
|
Write(' ');
|
||||||
|
|
||||||
|
Write(option.IsScalar
|
||||||
|
? "<value>"
|
||||||
|
: "<values...>"
|
||||||
|
);
|
||||||
|
Write(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options placeholder
|
||||||
|
Write(ConsoleColor.White, "[options]");
|
||||||
|
|
||||||
|
WriteLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WriteCommandUsage(
|
||||||
|
CommandSchema command,
|
||||||
|
IReadOnlyList<CommandSchema> childCommands)
|
||||||
{
|
{
|
||||||
if (!IsEmpty)
|
if (!IsEmpty)
|
||||||
WriteVerticalMargin();
|
WriteVerticalMargin();
|
||||||
@@ -115,52 +165,23 @@ namespace CliFx.Domain
|
|||||||
// Exe name
|
// Exe name
|
||||||
WriteHorizontalMargin();
|
WriteHorizontalMargin();
|
||||||
Write(_metadata.ExecutableName);
|
Write(_metadata.ExecutableName);
|
||||||
|
Write(' ');
|
||||||
|
|
||||||
// Command name
|
// Current command usage
|
||||||
if (!string.IsNullOrWhiteSpace(command.Name))
|
WriteCommandUsageLineItem(command, childCommands.Any());
|
||||||
{
|
|
||||||
Write(' ');
|
|
||||||
Write(ConsoleColor.Cyan, command.Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Child command placeholder
|
// Sub commands usage
|
||||||
if (childCommands.Any())
|
if (childCommands.Any())
|
||||||
{
|
{
|
||||||
Write(' ');
|
WriteVerticalMargin();
|
||||||
Write(ConsoleColor.Cyan, "[command]");
|
|
||||||
|
foreach (var childCommand in childCommands)
|
||||||
|
{
|
||||||
|
WriteHorizontalMargin();
|
||||||
|
Write("... ");
|
||||||
|
WriteCommandUsageLineItem(childCommand, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parameters
|
|
||||||
foreach (var parameter in command.Parameters)
|
|
||||||
{
|
|
||||||
Write(' ');
|
|
||||||
Write(parameter.IsScalar
|
|
||||||
? $"<{parameter.Name}>"
|
|
||||||
: $"<{parameter.Name}...>"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Required options
|
|
||||||
foreach (var option in command.Options.Where(o => o.IsRequired))
|
|
||||||
{
|
|
||||||
Write(' ');
|
|
||||||
Write(ConsoleColor.White, !string.IsNullOrWhiteSpace(option.Name)
|
|
||||||
? $"--{option.Name}"
|
|
||||||
: $"-{option.ShortName}"
|
|
||||||
);
|
|
||||||
|
|
||||||
Write(' ');
|
|
||||||
Write(option.IsScalar
|
|
||||||
? "<value>"
|
|
||||||
: "<values...>"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Options placeholder
|
|
||||||
Write(' ');
|
|
||||||
Write(ConsoleColor.White, "[options]");
|
|
||||||
|
|
||||||
WriteLine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteCommandParameters(CommandSchema command)
|
private void WriteCommandParameters(CommandSchema command)
|
||||||
@@ -264,7 +285,7 @@ namespace CliFx.Domain
|
|||||||
if (!option.IsRequired)
|
if (!option.IsRequired)
|
||||||
{
|
{
|
||||||
var defaultValue = argumentDefaultValues.GetValueOrDefault(option);
|
var defaultValue = argumentDefaultValues.GetValueOrDefault(option);
|
||||||
var defaultValueFormatted = FormatDefaultValue(defaultValue);
|
var defaultValueFormatted = TryFormatDefaultValue(defaultValue);
|
||||||
if (defaultValueFormatted != null)
|
if (defaultValueFormatted != null)
|
||||||
{
|
{
|
||||||
Write($"Default: {defaultValueFormatted}.");
|
Write($"Default: {defaultValueFormatted}.");
|
||||||
@@ -334,7 +355,9 @@ namespace CliFx.Domain
|
|||||||
CommandSchema command,
|
CommandSchema command,
|
||||||
IReadOnlyDictionary<CommandArgumentSchema, object?> defaultValues)
|
IReadOnlyDictionary<CommandArgumentSchema, object?> defaultValues)
|
||||||
{
|
{
|
||||||
var childCommands = root.GetChildCommands(command.Name);
|
var commandName = command.Name;
|
||||||
|
var childCommands = root.GetChildCommands(commandName);
|
||||||
|
var descendantCommands = root.GetDescendantCommands(commandName);
|
||||||
|
|
||||||
_console.ResetColor();
|
_console.ResetColor();
|
||||||
|
|
||||||
@@ -342,7 +365,7 @@ namespace CliFx.Domain
|
|||||||
WriteApplicationInfo();
|
WriteApplicationInfo();
|
||||||
|
|
||||||
WriteCommandDescription(command);
|
WriteCommandDescription(command);
|
||||||
WriteCommandUsage(command, childCommands);
|
WriteCommandUsage(command, descendantCommands);
|
||||||
WriteCommandParameters(command);
|
WriteCommandParameters(command);
|
||||||
WriteCommandOptions(command, defaultValues);
|
WriteCommandOptions(command, defaultValues);
|
||||||
WriteCommandChildren(command, childCommands);
|
WriteCommandChildren(command, childCommands);
|
||||||
@@ -354,7 +377,7 @@ namespace CliFx.Domain
|
|||||||
private static string FormatValidValues(IReadOnlyList<string> values) =>
|
private static string FormatValidValues(IReadOnlyList<string> values) =>
|
||||||
values.Select(v => v.Quote()).JoinToString(", ");
|
values.Select(v => v.Quote()).JoinToString(", ");
|
||||||
|
|
||||||
private static string? FormatDefaultValue(object? defaultValue)
|
private static string? TryFormatDefaultValue(object? defaultValue)
|
||||||
{
|
{
|
||||||
if (defaultValue == null)
|
if (defaultValue == null)
|
||||||
return null;
|
return null;
|
||||||
@@ -362,7 +385,7 @@ namespace CliFx.Domain
|
|||||||
// Enumerable
|
// Enumerable
|
||||||
if (!(defaultValue is string) && defaultValue is IEnumerable defaultValues)
|
if (!(defaultValue is string) && defaultValue is IEnumerable defaultValues)
|
||||||
{
|
{
|
||||||
var elementType = defaultValues.GetType().GetEnumerableUnderlyingType() ?? typeof(object);
|
var elementType = defaultValues.GetType().TryGetEnumerableUnderlyingType() ?? typeof(object);
|
||||||
|
|
||||||
// If the ToString() method is not overriden, the default value can't be formatted nicely
|
// If the ToString() method is not overriden, the default value can't be formatted nicely
|
||||||
if (!elementType.IsToStringOverriden())
|
if (!elementType.IsToStringOverriden())
|
||||||
|
|||||||
@@ -114,6 +114,18 @@ namespace CliFx.Domain
|
|||||||
nonLastNonScalarParameter
|
nonLastNonScalarParameter
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var invalidConverterParameters = command.Parameters
|
||||||
|
.Where(p => p.ConverterType != null && !p.ConverterType.Implements(typeof(IArgumentValueConverter)))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
if (invalidConverterParameters.Any())
|
||||||
|
{
|
||||||
|
throw CliFxException.ParametersWithInvalidConverters(
|
||||||
|
command,
|
||||||
|
invalidConverterParameters
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ValidateOptions(CommandSchema command)
|
private static void ValidateOptions(CommandSchema command)
|
||||||
@@ -184,6 +196,18 @@ namespace CliFx.Domain
|
|||||||
duplicateEnvironmentVariableNameGroup.ToArray()
|
duplicateEnvironmentVariableNameGroup.ToArray()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var invalidConverterOptions = command.Options
|
||||||
|
.Where(o => o.ConverterType != null && !o.ConverterType.Implements(typeof(IArgumentValueConverter)))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
if (invalidConverterOptions.Any())
|
||||||
|
{
|
||||||
|
throw CliFxException.OptionsWithInvalidConverters(
|
||||||
|
command,
|
||||||
|
invalidConverterOptions
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ValidateCommands(IReadOnlyList<CommandSchema> commands)
|
private static void ValidateCommands(IReadOnlyList<CommandSchema> commands)
|
||||||
|
|||||||
@@ -172,6 +172,19 @@ If it's not feasible to fit into these constraints, consider using options inste
|
|||||||
return new CliFxException(message.Trim());
|
return new CliFxException(message.Trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static CliFxException ParametersWithInvalidConverters(
|
||||||
|
CommandSchema command,
|
||||||
|
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameter(s) with invalid converters:
|
||||||
|
{invalidParameters.JoinToString(Environment.NewLine)}
|
||||||
|
|
||||||
|
Specified converter must implement {typeof(IArgumentValueConverter).FullName}.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
internal static CliFxException OptionsWithNoName(
|
internal static CliFxException OptionsWithNoName(
|
||||||
CommandSchema command,
|
CommandSchema command,
|
||||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||||
@@ -243,6 +256,19 @@ Environment variable names are not case-sensitive.";
|
|||||||
|
|
||||||
return new CliFxException(message.Trim());
|
return new CliFxException(message.Trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static CliFxException OptionsWithInvalidConverters(
|
||||||
|
CommandSchema command,
|
||||||
|
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} option(s) with invalid converters:
|
||||||
|
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||||
|
|
||||||
|
Specified converter must implement {typeof(IArgumentValueConverter).FullName}.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// End-user-facing exceptions
|
// End-user-facing exceptions
|
||||||
|
|||||||
13
CliFx/IArgumentValueConverter.cs
Normal file
13
CliFx/IArgumentValueConverter.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
namespace CliFx
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Implements custom conversion logic that maps an argument value to a domain type.
|
||||||
|
/// </summary>
|
||||||
|
public interface IArgumentValueConverter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Converts an input value to object of required type.
|
||||||
|
/// </summary>
|
||||||
|
public object ConvertFrom(string value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using CliFx.Internal;
|
||||||
|
|
||||||
namespace CliFx
|
namespace CliFx
|
||||||
{
|
{
|
||||||
@@ -85,7 +86,10 @@ namespace CliFx
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets console foreground color, executes specified action, and sets the color back to the original value.
|
/// Sets console foreground color, executes specified action, and sets the color back to the original value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void WithForegroundColor(this IConsole console, ConsoleColor foregroundColor, Action action)
|
public static void WithForegroundColor(
|
||||||
|
this IConsole console,
|
||||||
|
ConsoleColor foregroundColor,
|
||||||
|
Action action)
|
||||||
{
|
{
|
||||||
var lastColor = console.ForegroundColor;
|
var lastColor = console.ForegroundColor;
|
||||||
console.ForegroundColor = foregroundColor;
|
console.ForegroundColor = foregroundColor;
|
||||||
@@ -98,7 +102,10 @@ namespace CliFx
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets console background color, executes specified action, and sets the color back to the original value.
|
/// Sets console background color, executes specified action, and sets the color back to the original value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void WithBackgroundColor(this IConsole console, ConsoleColor backgroundColor, Action action)
|
public static void WithBackgroundColor(
|
||||||
|
this IConsole console,
|
||||||
|
ConsoleColor backgroundColor,
|
||||||
|
Action action)
|
||||||
{
|
{
|
||||||
var lastColor = console.BackgroundColor;
|
var lastColor = console.BackgroundColor;
|
||||||
console.BackgroundColor = backgroundColor;
|
console.BackgroundColor = backgroundColor;
|
||||||
@@ -111,7 +118,133 @@ namespace CliFx
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets console foreground and background colors, executes specified action, and sets the colors back to the original values.
|
/// Sets console foreground and background colors, executes specified action, and sets the colors back to the original values.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void WithColors(this IConsole console, ConsoleColor foregroundColor, ConsoleColor backgroundColor, Action action) =>
|
public static void WithColors(
|
||||||
|
this IConsole console,
|
||||||
|
ConsoleColor foregroundColor,
|
||||||
|
ConsoleColor backgroundColor,
|
||||||
|
Action action) =>
|
||||||
console.WithForegroundColor(foregroundColor, () => console.WithBackgroundColor(backgroundColor, action));
|
console.WithForegroundColor(foregroundColor, () => console.WithBackgroundColor(backgroundColor, action));
|
||||||
|
|
||||||
|
private static void WriteException(
|
||||||
|
this IConsole console,
|
||||||
|
Exception exception,
|
||||||
|
int indentLevel)
|
||||||
|
{
|
||||||
|
var exceptionType = exception.GetType();
|
||||||
|
|
||||||
|
var indentationShared = new string(' ', 4 * indentLevel);
|
||||||
|
var indentationLocal = new string(' ', 2);
|
||||||
|
|
||||||
|
// Fully qualified exception type
|
||||||
|
console.Error.Write(indentationShared);
|
||||||
|
console.WithForegroundColor(ConsoleColor.DarkGray, () =>
|
||||||
|
console.Error.Write(exceptionType.Namespace + ".")
|
||||||
|
);
|
||||||
|
console.WithForegroundColor(ConsoleColor.White, () =>
|
||||||
|
console.Error.Write(exceptionType.Name)
|
||||||
|
);
|
||||||
|
console.Error.Write(": ");
|
||||||
|
|
||||||
|
// Exception message
|
||||||
|
console.WithForegroundColor(ConsoleColor.Red, () => console.Error.WriteLine(exception.Message));
|
||||||
|
|
||||||
|
// Recurse into inner exceptions
|
||||||
|
if (exception.InnerException != null)
|
||||||
|
{
|
||||||
|
console.WriteException(exception.InnerException, indentLevel + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to parse and pretty-print the stack trace
|
||||||
|
try
|
||||||
|
{
|
||||||
|
foreach (var stackFrame in StackFrame.ParseMany(exception.StackTrace))
|
||||||
|
{
|
||||||
|
console.Error.Write(indentationShared + indentationLocal);
|
||||||
|
console.Error.Write("at ");
|
||||||
|
|
||||||
|
// "CliFx.Demo.Commands.BookAddCommand."
|
||||||
|
console.WithForegroundColor(ConsoleColor.DarkGray, () =>
|
||||||
|
console.Error.Write(stackFrame.ParentType + ".")
|
||||||
|
);
|
||||||
|
|
||||||
|
// "ExecuteAsync"
|
||||||
|
console.WithForegroundColor(ConsoleColor.Yellow, () =>
|
||||||
|
console.Error.Write(stackFrame.MethodName)
|
||||||
|
);
|
||||||
|
|
||||||
|
console.Error.Write("(");
|
||||||
|
|
||||||
|
for (var i = 0; i < stackFrame.Parameters.Count; i++)
|
||||||
|
{
|
||||||
|
var parameter = stackFrame.Parameters[i];
|
||||||
|
|
||||||
|
// Separator
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
console.Error.Write(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// "IConsole"
|
||||||
|
console.WithForegroundColor(ConsoleColor.Blue, () =>
|
||||||
|
console.Error.Write(parameter.Type)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(parameter.Name))
|
||||||
|
{
|
||||||
|
console.Error.Write(" ");
|
||||||
|
|
||||||
|
// "console"
|
||||||
|
console.WithForegroundColor(ConsoleColor.White, () =>
|
||||||
|
console.Error.Write(parameter.Name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.Error.Write(") ");
|
||||||
|
|
||||||
|
// Location
|
||||||
|
if (!string.IsNullOrWhiteSpace(stackFrame.FilePath))
|
||||||
|
{
|
||||||
|
console.Error.Write("in");
|
||||||
|
console.Error.Write("\n" + indentationShared + indentationLocal + indentationLocal);
|
||||||
|
|
||||||
|
// "E:\Projects\Softdev\CliFx\CliFx.Demo\Commands\"
|
||||||
|
var stackFrameDirectoryPath = Path.GetDirectoryName(stackFrame.FilePath);
|
||||||
|
console.WithForegroundColor(ConsoleColor.DarkGray, () =>
|
||||||
|
console.Error.Write(stackFrameDirectoryPath + Path.DirectorySeparatorChar)
|
||||||
|
);
|
||||||
|
|
||||||
|
// "BookAddCommand.cs"
|
||||||
|
var stackFrameFileName = Path.GetFileName(stackFrame.FilePath);
|
||||||
|
console.WithForegroundColor(ConsoleColor.Yellow, () =>
|
||||||
|
console.Error.Write(stackFrameFileName)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(stackFrame.LineNumber))
|
||||||
|
{
|
||||||
|
console.Error.Write(":");
|
||||||
|
|
||||||
|
// "35"
|
||||||
|
console.WithForegroundColor(ConsoleColor.Blue, () =>
|
||||||
|
console.Error.Write(stackFrame.LineNumber)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.Error.WriteLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If any point of parsing has failed - print the stack trace without any formatting
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
console.Error.WriteLine(exception.StackTrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Should this be public?
|
||||||
|
internal static void WriteException(
|
||||||
|
this IConsole console,
|
||||||
|
Exception exception) =>
|
||||||
|
console.WriteException(exception, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,11 @@ namespace CliFx.Internal.Extensions
|
|||||||
{
|
{
|
||||||
internal static class StringExtensions
|
internal static class StringExtensions
|
||||||
{
|
{
|
||||||
|
public static string? NullIfWhiteSpace(this string str) =>
|
||||||
|
!string.IsNullOrWhiteSpace(str)
|
||||||
|
? str
|
||||||
|
: null;
|
||||||
|
|
||||||
public static string Repeat(this char c, int count) => new string(c, count);
|
public static string Repeat(this char c, int count) => new string(c, count);
|
||||||
|
|
||||||
public static string AsString(this char c) => c.Repeat(1);
|
public static string AsString(this char c) => c.Repeat(1);
|
||||||
|
|||||||
@@ -8,11 +8,15 @@ namespace CliFx.Internal.Extensions
|
|||||||
{
|
{
|
||||||
internal static class TypeExtensions
|
internal static class TypeExtensions
|
||||||
{
|
{
|
||||||
|
public static object CreateInstance(this Type type) => Activator.CreateInstance(type);
|
||||||
|
|
||||||
|
public static T CreateInstance<T>(this Type type) => (T) type.CreateInstance();
|
||||||
|
|
||||||
public static bool Implements(this Type type, Type interfaceType) => type.GetInterfaces().Contains(interfaceType);
|
public static bool Implements(this Type type, Type interfaceType) => type.GetInterfaces().Contains(interfaceType);
|
||||||
|
|
||||||
public static Type? GetNullableUnderlyingType(this Type type) => Nullable.GetUnderlyingType(type);
|
public static Type? TryGetNullableUnderlyingType(this Type type) => Nullable.GetUnderlyingType(type);
|
||||||
|
|
||||||
public static Type? GetEnumerableUnderlyingType(this Type type)
|
public static Type? TryGetEnumerableUnderlyingType(this Type type)
|
||||||
{
|
{
|
||||||
if (type.IsPrimitive)
|
if (type.IsPrimitive)
|
||||||
return null;
|
return null;
|
||||||
@@ -25,17 +29,20 @@ namespace CliFx.Internal.Extensions
|
|||||||
|
|
||||||
return type
|
return type
|
||||||
.GetInterfaces()
|
.GetInterfaces()
|
||||||
.Select(GetEnumerableUnderlyingType)
|
.Select(TryGetEnumerableUnderlyingType)
|
||||||
.Where(t => t != null)
|
.Where(t => t != null)
|
||||||
.OrderByDescending(t => t != typeof(object)) // prioritize more specific types
|
.OrderByDescending(t => t != typeof(object)) // prioritize more specific types
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MethodInfo GetToStringMethod(this Type type) => type.GetMethod(nameof(ToString), Type.EmptyTypes);
|
public static MethodInfo GetToStringMethod(this Type type) =>
|
||||||
|
// ToString() with no params always exists
|
||||||
|
type.GetMethod(nameof(ToString), Type.EmptyTypes)!;
|
||||||
|
|
||||||
public static bool IsToStringOverriden(this Type type) => type.GetToStringMethod() != typeof(object).GetToStringMethod();
|
public static bool IsToStringOverriden(this Type type) =>
|
||||||
|
type.GetToStringMethod() != typeof(object).GetToStringMethod();
|
||||||
|
|
||||||
public static MethodInfo GetStaticParseMethod(this Type type, bool withFormatProvider = false)
|
public static MethodInfo? TryGetStaticParseMethod(this Type type, bool withFormatProvider = false)
|
||||||
{
|
{
|
||||||
var argumentTypes = withFormatProvider
|
var argumentTypes = withFormatProvider
|
||||||
? new[] {typeof(string), typeof(IFormatProvider)}
|
? new[] {typeof(string), typeof(IFormatProvider)}
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ namespace System
|
|||||||
|
|
||||||
public static bool EndsWith(this string str, char c) =>
|
public static bool EndsWith(this string str, char c) =>
|
||||||
str.Length > 0 && str[str.Length - 1] == c;
|
str.Length > 0 && str[str.Length - 1] == c;
|
||||||
|
|
||||||
|
public static string[] Split(this string str, char separator, StringSplitOptions splitOptions) =>
|
||||||
|
str.Split(new[] {separator}, splitOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +34,7 @@ namespace System.Collections.Generic
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dic, TKey key) =>
|
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dic, TKey key) =>
|
||||||
dic.TryGetValue(key, out var result) ? result! : default!;
|
dic.TryGetValue(key!, out var result) ? result! : default!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
121
CliFx/Internal/StackFrame.cs
Normal file
121
CliFx/Internal/StackFrame.cs
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using CliFx.Internal.Extensions;
|
||||||
|
|
||||||
|
namespace CliFx.Internal
|
||||||
|
{
|
||||||
|
internal class StackFrameParameter
|
||||||
|
{
|
||||||
|
public string Type { get; }
|
||||||
|
|
||||||
|
public string? Name { get; }
|
||||||
|
|
||||||
|
public StackFrameParameter(string type, string? name)
|
||||||
|
{
|
||||||
|
Type = type;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal partial class StackFrame
|
||||||
|
{
|
||||||
|
public string ParentType { get; }
|
||||||
|
|
||||||
|
public string MethodName { get; }
|
||||||
|
|
||||||
|
public IReadOnlyList<StackFrameParameter> Parameters { get; }
|
||||||
|
|
||||||
|
public string? FilePath { get; }
|
||||||
|
|
||||||
|
public string? LineNumber { get; }
|
||||||
|
|
||||||
|
public StackFrame(
|
||||||
|
string parentType,
|
||||||
|
string methodName,
|
||||||
|
IReadOnlyList<StackFrameParameter> parameters,
|
||||||
|
string? filePath,
|
||||||
|
string? lineNumber)
|
||||||
|
{
|
||||||
|
ParentType = parentType;
|
||||||
|
MethodName = methodName;
|
||||||
|
Parameters = parameters;
|
||||||
|
FilePath = filePath;
|
||||||
|
LineNumber = lineNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal partial class StackFrame
|
||||||
|
{
|
||||||
|
private const string Space = @"[\x20\t]";
|
||||||
|
private const string NotSpace = @"[^\x20\t]";
|
||||||
|
|
||||||
|
// Taken from https://github.com/atifaziz/StackTraceParser
|
||||||
|
private static readonly Regex Pattern = new Regex(@"
|
||||||
|
^
|
||||||
|
" + Space + @"*
|
||||||
|
\w+ " + Space + @"+
|
||||||
|
(?<frame>
|
||||||
|
(?<type> " + NotSpace + @"+ ) \.
|
||||||
|
(?<method> " + NotSpace + @"+? ) " + Space + @"*
|
||||||
|
(?<params> \( ( " + Space + @"* \)
|
||||||
|
| (?<pt> .+?) " + Space + @"+ (?<pn> .+?)
|
||||||
|
(, " + Space + @"* (?<pt> .+?) " + Space + @"+ (?<pn> .+?) )* \) ) )
|
||||||
|
( " + Space + @"+
|
||||||
|
( # Microsoft .NET stack traces
|
||||||
|
\w+ " + Space + @"+
|
||||||
|
(?<file> ( [a-z] \: # Windows rooted path starting with a drive letter
|
||||||
|
| / ) # *nix rooted path starting with a forward-slash
|
||||||
|
.+? )
|
||||||
|
\: \w+ " + Space + @"+
|
||||||
|
(?<line> [0-9]+ ) \p{P}?
|
||||||
|
| # Mono stack traces
|
||||||
|
\[0x[0-9a-f]+\] " + Space + @"+ \w+ " + Space + @"+
|
||||||
|
<(?<file> [^>]+ )>
|
||||||
|
:(?<line> [0-9]+ )
|
||||||
|
)
|
||||||
|
)?
|
||||||
|
)
|
||||||
|
\s*
|
||||||
|
$",
|
||||||
|
RegexOptions.IgnoreCase |
|
||||||
|
RegexOptions.Multiline |
|
||||||
|
RegexOptions.ExplicitCapture |
|
||||||
|
RegexOptions.CultureInvariant |
|
||||||
|
RegexOptions.IgnorePatternWhitespace,
|
||||||
|
TimeSpan.FromSeconds(5)
|
||||||
|
);
|
||||||
|
|
||||||
|
public static IEnumerable<StackFrame> ParseMany(string stackTrace)
|
||||||
|
{
|
||||||
|
var matches = Pattern.Matches(stackTrace).Cast<Match>().ToArray();
|
||||||
|
|
||||||
|
// Ensure success (all lines should be parsed)
|
||||||
|
var isSuccess =
|
||||||
|
matches.Length ==
|
||||||
|
stackTrace.Split('\n', StringSplitOptions.RemoveEmptyEntries).Length;
|
||||||
|
|
||||||
|
if (!isSuccess)
|
||||||
|
{
|
||||||
|
throw new FormatException("Could not parse stack trace.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return from m in matches
|
||||||
|
select m.Groups
|
||||||
|
into groups
|
||||||
|
let pt = groups["pt"].Captures
|
||||||
|
let pn = groups["pn"].Captures
|
||||||
|
select new StackFrame(
|
||||||
|
groups["type"].Value,
|
||||||
|
groups["method"].Value,
|
||||||
|
(
|
||||||
|
from i in Enumerable.Range(0, pt.Count)
|
||||||
|
select new StackFrameParameter(pt[i].Value, pn[i].Value.NullIfWhiteSpace())
|
||||||
|
).ToArray(),
|
||||||
|
groups["file"].Value.NullIfWhiteSpace(),
|
||||||
|
groups["line"].Value.NullIfWhiteSpace()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
43
CliFx/Utilities/MemoryStreamWriter.cs
Normal file
43
CliFx/Utilities/MemoryStreamWriter.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace CliFx.Utilities
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Implementation of <see cref="StreamWriter"/> with a <see cref="MemoryStream"/> as a backing store.
|
||||||
|
/// </summary>
|
||||||
|
public class MemoryStreamWriter : StreamWriter
|
||||||
|
{
|
||||||
|
private new MemoryStream BaseStream => (MemoryStream) base.BaseStream;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="MemoryStreamWriter"/>.
|
||||||
|
/// </summary>
|
||||||
|
public MemoryStreamWriter(Encoding encoding)
|
||||||
|
: base(new MemoryStream(), encoding)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="MemoryStreamWriter"/>.
|
||||||
|
/// </summary>
|
||||||
|
public MemoryStreamWriter()
|
||||||
|
: base(new MemoryStream())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the bytes written to the underlying stream.
|
||||||
|
/// </summary>
|
||||||
|
public byte[] GetBytes()
|
||||||
|
{
|
||||||
|
Flush();
|
||||||
|
return BaseStream.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the string written to the underlying stream.
|
||||||
|
/// </summary>
|
||||||
|
public string GetString() => Encoding.GetString(GetBytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user