dotnet / dotnet/command-line-api
Creating RootCommand fails when used as library to a Blazor WASM project
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
Hello,
I am trying to create a GUI for a System.CommandLine console app. The GUI is a Blazor WASM webpage.
If I convert the console app to a library - the program always crashes when "RootCommand" is created.
I have tried this with various simple System.CommandLine console apps turned library dll's for various simple Blazor components and always get the same problem.
I have had the same issue in both .NET 5 and .NET 6.
```
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Value cannot be null or whitespace. (Parameter 'value')
System.ArgumentException: Value cannot be null or whitespace. (Parameter 'value')
at System.CommandLine.IdentifierSymbol.set_Name(String value)
at System.CommandLine.IdentifierSymbol..ctor(String name, String description)
at System.CommandLine.Command..ctor(String name, String description)
at System.CommandLine.RootCommand..ctor(String description)
at consoleApp.Program.Main(String[] args) in C:\my-coding-projects\net6-console-blazor\console-app\Program.cs:line 12
at gui_for_console.Pages.Index.CallConsoleApp() in C:\my-coding-projects\net6-console-blazor\gui-for-console\Pages\Index.razor:line 9
at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[Object](MulticastDelegate delegate, Object arg)
at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync(Object arg)
at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg)
at Microsoft.AspNetCore.Components.EventCallback.InvokeAsync(Object arg)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs)
window.Module.s.printErr @ blazor.webassembly.js:1
Te._internal.dotNetCriticalError @ blazor.webassembly.js:1
Rt @ blazor.webassembly.js:1
_mono_wasm_invoke_js_blazor @ dotnet.6.0.0.z6iy0buwwq.js:1
$func2588 @ 00945abe:0x8065b
$func2307 @ 00945abe:0x689a7
$func7015 @ 00945abe:0x17bac4
$func6830 @ 00945abe:0x172bc5
$func3524 @ 00945abe:0xb47cc
$func474 @ 00945abe:0x11875
$func6442 @ 00945abe:0x15f1e2
$func2588 @ 00945abe:0x80614
$func2307 @ 00945abe:0x689a7
$func7015 @ 00945abe:0x17bac4
$func6830 @ 00945abe:0x172bc5
$func3524 @ 00945abe:0xb47cc
$func675 @ 00945abe:0x16602
$fb @ 00945abe:0x16f60e
Module._mono_wasm_invoke_method @ dotnet.6.0.0.z6iy0buwwq.js:1
managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_BeginInvokeDotNet @ managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_BeginInvokeDotNet:19
beginInvokeDotNetFromJS @ blazor.webassembly.js:1
b @ blazor.webassembly.js:1
invokeMethodAsync @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
invokeWhenHeapUnlocked @ blazor.webassembly.js:1
S @ blazor.webassembly.js:1
C @ blazor.webassembly.js:1
dispatchGlobalEventToAllElements @ blazor.webassembly.js:1
onGlobalEvent @ blazor.webassembly.js:1
```
I have also tried using the System.CommandLine console app as a library for another console app -- which works fine (console app inception?!?). No problem at all with creating RootCommand.
Perhaps, I am asking this in the wrong place, as it might be more of a Blazor WASM issue than System.CommandLine issue.
Any guidance would be most appreciated.
Simple example:
Index.Razor
```csharp
@page "/"
Click me
@code {
public void CallConsoleApp()
{
string[] args = { "--name", "Luke"};
consoleApp.Program.Main(args);
}
}
```
Console apps Program.cs
```csharp
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Reflection;
namespace consoleApp
{
public static class Program
{
public static void Main(string[] args)
{
var rootCommand = new RootCommand().WithHandler(nameof(handleName));
rootCommand.AddOption(new Option(new string[] { "--name"}, "Your name"));
rootCommand.Invoke(args);
}
static void handleName(string name)
{
Console.WriteLine($"Hello {name}");
}
static Command WithHandler(this Command command, string name)
{
var flags = BindingFlags.NonPublic | BindingFlags.Static;
var method = typeof(Program).GetMethod(name, flags);
var handler = CommandHandler.Create(method!);
command.Handler = handler;
return command;
}
}
}
```
Thank you again for all your hard work on this wonderful library. And thank you in advance for any possible assistance.
--Luke
Contributor guide
Assessment
This issue has not been assessed yet.