Estoy usando VS 2022, .Net 6.0 e intento crear mi primera aplicación usando System.CommandLine .
Problema: cuando lo construyo, me sale un error
El nombre 'CommandHandler' no existe en el contexto actual
El código que intento compilar es la aplicación de muestra del sitio de GitHub: https://github.com/dotnet/command-line-api/blob/main/docs/Your-first-app-with-System-CommandLine .md , sin alteración (creo).
Se parece a esto:
using System; using System.CommandLine; using System.IO; static int Main(string[] args) { // Create a root command with some options var rootCommand = new RootCommand { new Option<int>( "--int-option", getDefaultValue: () => 42, description: "An option whose argument is parsed as an int"), new Option<bool>( "--bool-option", "An option whose argument is parsed as a bool"), new Option<FileInfo>( "--file-option", "An option whose argument is parsed as a FileInfo") }; rootCommand.Description = "My sample app"; // Note that the parameters of the handler method are matched according to the names of the options rootCommand.Handler = CommandHandler.Create<int, bool, FileInfo>((intOption, boolOption, fileOption) => { Console.WriteLine($"The value for --int-option is: {intOption}"); Console.WriteLine($"The value for --bool-option is: {boolOption}"); Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}"); }); // Parse the incoming args and invoke the handler return rootCommand.InvokeAsync(args).Result; } He instalado la última versión de System.Commandline : 2.0.0-beta2.21617.1
Seguramente solo estoy siendo un gran idiota gordo en algún aspecto. Pero no lo veo.
Cualquier idea sería bienvenida.
Creo que te estás perdiendo una línea de using :
using System; using System.CommandLine; using System.CommandLine.Invocation; using System.IO; No puedo jurar que sea eso, pero parece que CommandHandler está definido en un espacio de nombres al que no hace referencia un using (en su código actual), por lo que System.CommandLine.Invocation puede ser la clave.
Este problema se debe a la actualización del paquete CommandLine 2.0 Beta 2 . Agregue la referencia System.CommandLine.NamingConventionBinder a las referencias para solucionar el problema. Siga los anuncios en la cuenta de GitHub de command-line-api:
En su proyecto, agregue una referencia a System.CommandLine.NamingConventionBinder.
En su código, cambie las referencias al espacio de nombres System.CommandLine.Invocation para usar System.CommandLine.NamingConventionBinder, donde ahora se encuentran los métodos CommandHandler.Create. (Ya no hay un tipo CommandHandler en System.CommandLine, por lo que después de actualizar obtendrá errores de compilación hasta que haga referencia a System.CommandLine.NamingConventionBinder).
Si desea continuar con los viejos hábitos, intente usar versiones anteriores del paquete System.CommandLine .