dotnet / dotnet/command-line-api

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

Open
#911 1 comment 9 reactions 0 assignees View on GitHub
Area-Parser and Binder bug
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

**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'
```

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.