command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

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

Open ghost opened this issue 5 years ago • 1 comments

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<bool>(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<bool>(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<bool>(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'

ghost avatar May 25 '20 11:05 ghost

This works in 2.0.0-beta4.22272.1.

jonorossi avatar Jul 13 '23 01:07 jonorossi