Cysharp / ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
MIT License
1.54k stars 88 forks source link

Passing a quoted string with a short parameter breaks Microsoft CommandLine engine #83

Closed igadmg closed 1 year ago

igadmg commented 2 years ago

Here is my case. I am writing an app which need to accept parameter switches for another app it is using. So for example I am trying to call my app

./myapp command -msg=hello random parameters here

or even like this

./myapp command "-msg=hello random parameters here"

And that break everything with exception System.FormatException: 'The short switch '-msg=hello random parameters here' is not defined in the switch mappings.'

Looks like HostBuilder takes that parameters personal and try to parse them for some reason. Looks like not an issue of ConsoleAppFramework but still maybe there is a way to fix that?

One solution I found is to wrap that as a named parameter. For example

./myapp command --parameters "-msg=hello random parameters here"

But that look ugly. Will require escaping of internal quotes etc.

So the question is is it possible to prevent HostBuilder from parsing parameters, and why is it doing it? I just want to get that strings in my app and don't really care if my data is valid for HostBuilder or not. Or that is a limitation of .net platform and we are doomed to live with that?

igadmg commented 2 years ago

I found suitable workaround for that - don't pass args to Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder So instead of ConsoleApp.CreateBuilder need to use ConsoleApp.CreateFromHostBuilder(Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(), args)