12

I am using VS 2022, .Net 6.0, and trying to build my first app using System.CommandLine.

Problem: when I build it, I get an error

The name 'CommandHandler' does not exist in the current context

The code I'm trying to build is the sample app from the GitHub site: https://github.com/dotnet/command-line-api/blob/main/docs/Your-first-app-with-System-CommandLine.md , without alteration (I think).

It looks like this:

using System;
using System.CommandLine;
using System.IO;

static int Main(string[] args)
{
    // Create a root command with some options
    var rootCommand = new RootCommand
        { 
        new Option<int>(
            "--int-option",
            getDefaultValue: () => 42,
            description: "An option whose argument is parsed as an int"),
        new Option<bool>(
            "--bool-option",
            "An option whose argument is parsed as a bool"),
        new Option<FileInfo>(
            "--file-option",
            "An option whose argument is parsed as a FileInfo")
    };

    rootCommand.Description = "My sample app";

    // Note that the parameters of the handler method are matched according to the names of the options
    rootCommand.Handler = CommandHandler.Create<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
    {
        Console.WriteLine($"The value for --int-option is: {intOption}");
        Console.WriteLine($"The value for --bool-option is: {boolOption}");
        Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}");
    });

    // Parse the incoming args and invoke the handler
    return rootCommand.InvokeAsync(args).Result;
}

I have installed the latest version of System.Commandline: 2.0.0-beta2.21617.1

SURELY I am just being a big fat idiot in some respect. But I don't see it.

Any insight would be welcomed.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hawkins Dale
  • 123
  • 1
  • 6
  • I believe, looking at the sample on Github, you're just simply missing a `using System.CommandLine.Invocation;` at the top of your code file ... – marc_s Dec 18 '21 at 06:54

2 Answers2

22

This issue is caused by updating the CommandLine 2.0 Beta 2 package. Add the reference System.CommandLine.NamingConventionBinder to the references to fix the problem. Follow the announcements on command-line-api's GitHub account:

In your project, add a reference to System.CommandLine.NamingConventionBinder.

In your code, change references to the System.CommandLine.Invocation namespace to use System.CommandLine.NamingConventionBinder, where the CommandHandler.Create methods are now found. (There’s no longer a CommandHandler type in System.CommandLine, so after you update you’ll get compilation errors until you reference System.CommandLine.NamingConventionBinder.)

If you want to continue with the old habits, try using older versions of the System.CommandLine package.


References
Sercan
  • 4,739
  • 3
  • 17
  • 36
  • 2
    YES! The problem was that the CommandHandler has been moved, as you point out, to System.CommandLine.NamingConventionBinder . To make things even more confusing, there's now a separate nuGet package for System.CommandLine.NamingConventionBinder, which must be installed on its own. Thank, you, I am in business. This is what we get for using beta tools... but this tool is SO USEFUL – Hawkins Dale Dec 18 '21 at 19:04
  • 1
    Has it moved again? I have "#r "nuget: System.CommandLine, 2.0.0-beta4.22272.1" installed in a .NET Interactive notebook and `open System.CommandLine.NamingConventionBinder` says there's no namespace named "NamingConventionBinder". – bmitc Aug 21 '22 at 07:35
  • 1
    Whelp, of course I answer it as soon as I ask. It is in a separate NuGet package: https://www.nuget.org/packages/System.CommandLine.NamingConventionBinder – bmitc Aug 21 '22 at 07:37
2

Think you are missing a using line:

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;

I can't swear that's it, but it looks like CommandHandler is defined in a namespace not referenced by a using (in your current code), so System.CommandLine.Invocation may be the key!

Sercan
  • 4,739
  • 3
  • 17
  • 36
Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • Thank you , I tried that, too, but foolishly failed it put it into the sample code. Turns out that the furniture had been rearranged, which is to be expected, I suppose, in pre-release code. – Hawkins Dale Dec 18 '21 at 19:07