commandlineparser / commandline

The best C# command line parser that brings standardized *nix getopt style, for .NET. Includes F# support
MIT License
4.53k stars 477 forks source link

dotnet fsi fails on dotnet-sdk 6.0.3 - No parameterless constructor found #828

Open edeustace opened 2 years ago

edeustace commented 2 years ago

Expected: Get cli usage message Actual: Get error message:

System.MissingMethodException: Cannot dynamically create an instance of type 'FSI_0002+options'. Reason: No parameterless constructor defined.
   at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
   at System.RuntimeType.CreateInstanceOfT()
   at System.Activator.CreateInstance[T]()
   at CommandLine.Core.InstanceBuilder.<>c__1`1.<Build>b__1_0(Func`1 f)
   at CSharpx.MaybeExtensions.MapValueOrDefault[T1,T2](Maybe`1 maybe, Func`2 func, T2 noneValue)
   at CommandLine.Core.InstanceBuilder.Build[T](Maybe`1 factory, Func`3 tokenizer, IEnumerable`1 arguments, StringComparer nameComparer, Boolean ignoreValueCase, CultureInfo parsingCulture, Boolean autoHelp, Boolean autoVersion, Boolean allowMultiInstance, IEnumerable`1 nonFatalErrors)
   at CommandLine.Parser.ParseArguments[T](IEnumerable`1 args)
   at <StartupCode$FSI_0002>.$FSI_0002.main@() in C:\Users\edeus\dev\github\smashcut\stream-poc\scripts\Opts.fsx:line 26
Stopped due to error

Note: works in dotnet 6.0.203

fsx script:

#r "nuget: CommandLineParser.FSharp, 2.9.1"
// #r "nuget: CommandLineParser.FSharp, 2.9.2-ci-210"

open CommandLine
open System

type options =
    { [<Option('r', "read", Required = true, HelpText = "Input files.")>]
      files: seq<string>
      [<Option(HelpText = "Prints all messages to standard output.")>]
      verbose: bool
      [<Option(Default = "русский", HelpText = "Content language.")>]
      language: string
      [<Value(0, MetaName = "offset", HelpText = "File offset.")>]
      offset: int64 option }

let result =
    CommandLine.Parser.Default.ParseArguments<options>(Environment.GetCommandLineArgs())

match result with
| :? Parsed<options> as parsed -> printfn $"{parsed.Value}"
| :? NotParsed<options> as notParsed -> printfn $"{notParsed.Errors}"
| _ -> printfn "?"
profan commented 11 months ago

if anyone else happens to run into this, the [<CLIMutable>] may help you, it helped me 👀

XiaoFaye commented 8 months ago

if anyone else happens to run into this, the [<CLIMutable>] may help you, it helped me 👀

How to use it? I have the same issue when publishing a C# project as standalone executable file.

profan commented 8 months ago

👀

This issue might be separate (at least if you use any options related to assembly stripping), and you might want to tell .NET to not strip stuff you only access through reflection, here's an example in my project using the DynamicDependencyAttribute: https://github.com/profan/brainiac/blob/856c167a12219988f0d458bb448e4c0d7b3ee649/Compiler.fs#L472