dotnet / dotnet/command-line-api

Bug: Global options defined in sub-commands are not seen, and cannot be used in sub-sub-commnds.

Offen
#911 1 Kommentar 9 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Area-Parser and Binder bug
Vorherrschende Sprache
C#
Sterne
3.7k
Forks
428
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

**Environment**
.NET Core Version: `3.1.103`
Target Framework: `netcoreapp3.1`
Package Version: `2.0.0-beta1.20253.1`
OS: Fedora 32

**Description of the problem**
When defining a global option in the root command, the option will correctly appear in all subsequently nested sub-commands.
However when defining a global option in a command other than the root command, the option will not appear in subsequently nested sub-commands.

**Steps to reproduce**
The following code demonstrates the issue:
```
namespace ConsoleApp {
using System;
using System.CommandLine;
using System.CommandLine.Invocation;

class Program {
static int Main(string[] args) {
var root = new RootCommand("My console program");
root.AddGlobalOption(new Option(new string[] {"--root-option", "-r"}, "This is a the root command global option"));

var cmd = new Command("subcmd", "This is the sub-command");
cmd.AddGlobalOption(new Option(new string[] {"--sub-option", "-s"}, "This is a the sub-command global option"));

var sub = new Command("subsubcmd", "This is a the sub-sub-command");
sub.AddOption(new Option(new string[] {"--sub-sub-option", "-u"}, "This is a the sub-sub-command option"));

cmd.AddCommand(sub);
root.AddCommand(cmd);

return root.Invoke(args);
}
}
}
```
**Expected behaviour**
invoking the the program with `ConsoleApp subcmd subsubcmd --help` should return:
```
subsubcmd:
This is a the sub-sub-command

Usage:
ConsoleApp subcmd subsubcmd [options]

Options:
-u, --sub-sub-option This is a the sub-sub-command option
-s, --sub-option This is a the sub-command global option
-r, --root-option This is a the root command global option
-?, -h, --help Show help and usage information
```
**Actual behaviour**
The actual behaviour of the program with `ConsoleApp subcmd subsubcmd --help` returns the following
```
subsubcmd:
This is a the sub-sub-command

Usage:
ConsoleApp subcmd subsubcmd [options]

Options:
-u, --sub-sub-option This is a the sub-sub-command option
-r, --root-option This is a the root command global option
-?, -h, --help Show help and usage information
```

The issue is not just merely visual, the option cannot be set when called from the sub-sub-command.
Invoking the program with `ConsoleApp subcmd subsubcmd -s' will return:
```
'-s' was not matched. Did you mean '-u', or '-r', or '-h'?
Unrecognized command or argument '-s'
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit der verschachtelten Konstruktion von RootCommand und Command mithilfe von AddGlobalOption und führe anschließend die Reproduktionen von ConsoleApp subcmd subsubcmd --help und -s aus. Verfolge, wie die Optionen von subcmd durch das verschachtelte subsubcmd gesammelt werden. Als abgeschlossen gilt dies, wenn die Hilfeausgabe --sub-option enthält und -s von subsubcmd akzeptiert wird, neben den vorhandenen Root- und Sub-Sub-Command-Optionen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp
Bereich
cli
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.