commandlineparser / commandline

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

Trying to use custom type for options: "is defined with a bad format." #910

Closed thomasd3 closed 5 months ago

thomasd3 commented 5 months ago

I've the following type (F#):

type SettingsRepository (optionsString: string option) =
    let mutable internalMap = Map.empty
    do
        try
            internalMap <-
                match optionsString with
                | Some init when not (String.IsNullOrEmpty(init)) ->
                    init.Split ';'
                    |> Array.map (fun s -> s.Split '=')
                    |> Array.map (fun a -> (a[0].Trim(), a[1].Split(',') |> Array.map _.Trim() |> String.concat ","))
                    |> Array.toList
                | _ -> []
                |> Map
        with ex ->
            failwith $"couldn't initialize SettingsRepository: {ex.Humanize()}"

and a config like this:

    type SimOptions = {
        [<Value(0, MetaName = "name",  Required = true,  HelpText = "name")    >] BotName:      string
        [<Value(1, MetaName = "ticker",   Required = true,  HelpText = "ticker name") >] Ticker:       string
        [<Value(2, MetaName = "fromTime", Required = true,  HelpText = "from time")   >] FromTime:     DateTime
        [<Value(3, MetaName = "toTime",   Required = true,  HelpText = "to time")     >] ToTime:       DateTime
        [<Option('x', "write-csv",        Required = false, HelpText = "csv filename")>] WriteCSVName: string option
        [<Option('s', "settings",         Required = false, HelpText = "settings")    >] Settings:     SettingsRepository
    }

I get this error when I'm trying to use it:

ERROR(S):
  Option 's, settings' is defined with a bad format.

But from the main page doc:

You can also map to every type with a constructor that accepts a string (like System.Uri) for reference and value types.

So, what am I doing wrong?

thomasd3 commented 5 months ago

nevermind, I had used string option...