dotnet / dotnet/command-line-api

Examples doesnt work on 2.0.3

Open
#2,778 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

Hello,

I've recently ported my app from some 2-beta version to 2.0.3 and it just doesn't work at all.
Ignoring my frustration please check the example code, that is in docs:
```
public class Class1
{
public static int Main(string[] args)
{
var cs = new CmdCrawlerSettings(args);

Argument anyArgument = new Argument("--arg")
{
Description = "whatever",
};

Option fileOption = new("--file")
{
Description = "The file to read and display on the console"
};

RootCommand rootCommand = new("Sample app for System.CommandLine");
rootCommand.Arguments.Add(anyArgument);
rootCommand.Options.Add(fileOption);

var parseResult = rootCommand.Parse(args);
if (parseResult.Errors.Count == 0)
{
Console.WriteLine(parseResult);
Console.WriteLine(parseResult.GetValue(anyArgument));
Console.WriteLine(parseResult.GetValue(fileOption));
return 0;
}

foreach (var parseError in parseResult.Errors)
{
Console.Error.WriteLine(parseError.Message);
}
return 1;
}
}
```
nothing fancy, lets run a test:
```
D:/Repo/Local/C#/TestCmd/testcmd/testcmd/bin/Debug/net9.0/testcmd.exe arg --file arg1
Unrecognized command or argument '--file'.
Unrecognized command or argument 'arg1'.
Unhandled exception. System.ArgumentException: Invalid command line arguments.
at testcmd.CmdCrawlerSettings.Parse(String[] args) in D:\Repo\Local\C#\TestCmd\testcmd\testcmd\Class1.cs:line 132
at testcmd.CmdCrawlerSettings..ctor(String[] args) in D:\Repo\Local\C#\TestCmd\testcmd\testcmd\Class1.cs:line 123
at testcmd.Class1.Main(String[] args) in D:\Repo\Local\C#\TestCmd\testcmd\testcmd\Class1.cs:line 10

```

so test fail.

so, lets try "help":
```
D:/Repo/Local/C#/TestCmd/testcmd/testcmd/bin/Debug/net9.0/testcmd.exe --help
[ testcmd [ --help ] <> ]
Unhandled exception. System.InvalidOperationException: Required argument missing for command: 'testcmd'.
at System.CommandLine.Binding.ArgumentConverter.GetValueOrDefault[T](ArgumentConversionResult result)
at System.CommandLine.Parsing.ArgumentResult.GetValueOrDefault[T]()
at System.CommandLine.Parsing.SymbolResult.GetValue[T](Argument`1 argument)
at System.CommandLine.ParseResult.GetValue[T](Argument`1 argument)
at testcmd.Class1.Main(String[] args) in D:\Repo\Local\C#\TestCmd\testcmd\testcmd\Class1.cs:line 30

```
another fail.

csproj for ref:
```


net9.0
latest
enable
enable
Exe



```

I am assuming I am doing something wrong. Could anybody be so kind to point me what?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.