Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom magic command options seems to be empty in F# #959

Closed
PawelStadnicki opened this issue Dec 30, 2020 · 4 comments
Closed

Custom magic command options seems to be empty in F# #959

PawelStadnicki opened this issue Dec 30, 2020 · 4 comments

Comments

@PawelStadnicki
Copy link

I have my own magic command created like this:

let command = new Command("#!mymagiccommand")
command.AddOption(new Option<string>("--load"))
command.AddOption(new Option<string>("--name"))
//this is not necessary, just to ensure typing
let d = System.Func<string, string, KernelInvocationContext, Task>(commandHandler)
command.Handler <-CommandHandler.Create(d)

Options seems to be registered correctly:
#!mymagicommand -h :

  #!mymagiccommand [options]

Options:
  --load <load>
  --name <name>    
  -?, -h, --help       Show help and usage information

However inside the command handler the option parameters seems to be empty

let commandHandler (load:string) (name:string) (context:KernelInvocationContext): Task = 
        context.Display (sprintf "load param is %s" load) |> ignore
        context.Display (sprintf $"name param is {name}") |> ignore

as

#!mymagiccommand --load loader --name name
or
#!mymagiccommand --load "loader" --name "name"
results with

load param is
name param is

Any clues ? Do I use it wrongly somehow or F# is not well supported in this case?

@jonsequitur
Copy link
Contributor

This looks like you're hitting a common System.CommandLine gotcha. Your handler parameters will be bound based on the parameter names, so you need to have named parameters for your command handler method, e.g. load and name.

@PawelStadnicki
Copy link
Author

How it should be different from commandHandler I already have?

let commandHandler (load:string) (name:string) (context:KernelInvocationContext): Task = ...

@PawelStadnicki
Copy link
Author

Ok, I didn't notice earlier that System.Command is used. Found this issue which drove me to this working code:

type Opt(name, load, context) =
    member val Context:KernelInvocationContext = context
    member val Name: string = name
    member val Load: string = load 

@jonsequitur
Copy link
Contributor

We have an alternative proposal for setting up handlers which doesn't use name-based matching, which you can see here: dotnet/command-line-api#1012

Feel free to weigh in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants