Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PackageAddCommandDefinition() : PackageAddCommandDefinitionBase(Nam

public abstract class PackageAddCommandDefinitionBase : Command
{
public static Option<string> CreateVersionOption() => new Option<string>("--version", "-v")
public static Option<string> CreateVersionOption() => new ExtendedOption<string>("--version", "-v")
{
Description = CommandDefinitionStrings.CmdVersionDescription,
HelpName = CommandDefinitionStrings.CmdVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal abstract class ReferenceAddCommandDefinitionBase : Command
}
};

public readonly Option<string> FrameworkOption = new("--framework", "-f")
public readonly Option<string> FrameworkOption = new ExtendedOption<string>("--framework", "-f")
{
Description = CommandDefinitionStrings.ReferenceAddCmdFrameworkDescription,
HelpName = CommandDefinitionStrings.CommonCmdFramework,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.CommandLine;
using System.CommandLine.StaticCompletions;

namespace Microsoft.DotNet.Cli.Commands.Reference.Remove;

Expand All @@ -17,10 +18,11 @@ internal sealed class ReferenceRemoveCommandDefinition() : ReferenceRemoveComman

internal abstract class ReferenceRemoveCommandDefinitionBase : Command
{
public static Argument<IEnumerable<string>> CreateProjectPathArgument() => new(CommandDefinitionStrings.ReferenceRemoveProjectPathArgumentName)
public static Argument<IEnumerable<string>> CreateProjectPathArgument() => new ExtendedArgument<IEnumerable<string>>(CommandDefinitionStrings.ReferenceRemoveProjectPathArgumentName)
{
Description = CommandDefinitionStrings.ReferenceRemoveProjectPathArgumentDescription,
Arity = ArgumentArity.OneOrMore,
IsDynamic = true,
};

public static Option<string> CreateFrameworkOption() => new("--framework", "-f")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static string RestoreRuntimeArgFunc(IEnumerable<string> rids)
return $"-property:RuntimeIdentifiers={string.Join("%3B", convertedRids)}";
}

private static Option<IEnumerable<string>> CreateRuntimeOption() => new Option<IEnumerable<string>>("--runtime", "-r")
private static Option<IEnumerable<string>> CreateRuntimeOption() => new ExtendedOption<IEnumerable<string>>("--runtime", "-r")
{
Description = CommandDefinitionStrings.CmdRuntimeOptionDescription,
HelpName = CommandDefinitionStrings.CmdRuntimeOption,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static class CommonArguments
public const string PackageIdArgumentName = "packageId";

public static Argument<PackageIdentityWithRange?> CreateOptionalPackageIdentityArgument(string examplePackage = "Newtonsoft.Json", string exampleVersion = "13.0.3") =>
new(PackageIdArgumentName)
new ExtendedArgument<PackageIdentityWithRange?>(PackageIdArgumentName)
{
Description = string.Format(CommandDefinitionStrings.PackageIdentityArgumentDescription, examplePackage, exampleVersion),
CustomParser = argumentResult => ParsePackageIdentityWithVersionSeparator(argumentResult.Tokens[0]?.Value),
Expand All @@ -23,7 +23,7 @@ internal static class CommonArguments
};

public static Argument<PackageIdentityWithRange> CreateRequiredPackageIdentityArgument(string examplePackage = "Newtonsoft.Json", string exampleVersion = "13.0.3") =>
new(PackageIdArgumentName)
new ExtendedArgument<PackageIdentityWithRange>(PackageIdArgumentName)
{
Description = string.Format(CommandDefinitionStrings.PackageIdentityArgumentDescription, examplePackage, exampleVersion),
CustomParser = argumentResult => ParsePackageIdentityWithVersionSeparator(argumentResult.Tokens[0]?.Value)!.Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.DotNet.Cli;

internal static class CommonOptions
{
public static Option<bool> CreateYesOption() => new("--yes", "-y")
public static Option<bool> CreateYesOption() => new ExtendedOption<bool>("--yes", "-y")
{
Description = CommandDefinitionStrings.YesOptionDescription,
Arity = ArgumentArity.Zero,
Expand Down Expand Up @@ -169,7 +169,7 @@ public static Option<VerbosityOptions> CreateHiddenVerbosityOption() =>
public const string FrameworkOptionName = "--framework";

public static Option<string> CreateFrameworkOption(string description) =>
new Option<string>(FrameworkOptionName, "-f")
new ExtendedOption<string>(FrameworkOptionName, "-f")
{
Description = description,
HelpName = CommandDefinitionStrings.FrameworkArgumentName,
Expand All @@ -196,7 +196,7 @@ public static Option<bool> CreateUseCurrentRuntimeOption(string description) =>
public const string ConfigurationOptionName = "--configuration";

public static Option<string?> CreateConfigurationOption(string description) =>
new Option<string?>(ConfigurationOptionName, "-c")
new ExtendedOption<string?>(ConfigurationOptionName, "-c")
{
Description = description,
HelpName = CommandDefinitionStrings.ConfigurationArgumentName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static IEnumerable<string> RuntimeArgFunc(string rid)
}

public static Option<string> CreateRuntimeOption(string description) =>
new Option<string>(RuntimeOptionName, "-r")
new ExtendedOption<string>(RuntimeOptionName, "-r")
{
HelpName = CommandDefinitionStrings.RuntimeIdentifierArgumentName,
Description = description,
Expand Down
6 changes: 0 additions & 6 deletions src/Cli/dotnet/Commands/Reference/ReferenceCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ public static void ConfigureCommand(ReferenceCommandDefinition command)

command.AddCommand.SetAction(parseResult => new ReferenceAddCommand(parseResult).Execute());
command.AddCommand.FrameworkOption.AddCompletions(CliCompletion.TargetFrameworksFromProjectFile);

command.ListCommand.SetAction(parseResult => new ReferenceListCommand(parseResult).Execute());

var projectPathArgument = command.RemoveCommand.ProjectPathArgument;
projectPathArgument.CompletionSources.Add(CliCompletion.ProjectReferencesFromProjectFile);
projectPathArgument.IsDynamic = true;

command.RemoveCommand.SetAction(parseResult => new ReferenceRemoveCommand(parseResult).Execute());
}
}

This file was deleted.

29 changes: 29 additions & 0 deletions src/System.CommandLine.StaticCompletions/ExtendedSymbols.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace System.CommandLine.StaticCompletions;

public interface IExtendedSymbol
{
/// <summary>
/// Indicates whether this option requires a dynamic call into the dotnet process to compute completions.
Comment thread
tmat marked this conversation as resolved.
/// </summary>
bool IsDynamic { get; set; }
}

public sealed class ExtendedOption<T>(string name, params string[] aliases)
: Option<T>(name, aliases), IExtendedSymbol
{
public bool IsDynamic { get; set; }
}

public sealed class ExtendedArgument<T>(string name)
: Argument<T>(name), IExtendedSymbol
{
public bool IsDynamic { get; set; }
}

public static class SymbolExtensions
{
extension(Symbol symbol)
{
public bool IsDynamic => symbol is IExtendedSymbol { IsDynamic: true };
Comment thread
tmat marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private static IEnumerable<string> GenerateArgumentCompletions(Argument argument
yield break;
}

if (argument.IsDynamic)
if (argument is IExtendedSymbol { IsDynamic: true })
{
// if the argument is a not-static-friendly argument, we need to call into the app for completions
// TODO: not yet supported for powershell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public async Task GenericCompletions()
[Fact]
public async Task DynamicCompletionsGeneration()
{
var staticOption = new Option<int>("--static")
var staticOption = new ExtendedOption<int>("--static")
{
IsDynamic = true
};
staticOption.AcceptOnlyFromAmong("1", "2", "3");
var dynamicArg = new Argument<int>("--dynamic")
var dynamicArg = new ExtendedArgument<int>("--dynamic")
{
IsDynamic = true
};
Expand Down