C# System.CommandLine from WPF application just returns null for string flag

Viewed 40

I'm trying to use c# System.CommandLine library from microsoft (https://www.nuget.org/packages/System.CommandLine) to add command line arguments to my wpf application. I'm wondering why --genkey message box doesn't popup given the code below?

using System.Windows;
using System.Windows.Input;
using System.CommandLine;
using System.CommandLine.Parsing;

namespace WpfApp
{
    public partial class App : Application
    {
        private string[] args_files = null;
        private string flag_genkey  = null;

        private void ProcessArgs(string genkey, string[] files) {
            flag_genkey  = genkey;
            args_files   = files;
        }
        
        private void Application_Startup(object sender, StartupEventArgs e) {
            var opGenKey    = new Option<string>("genkey");
            var argsFiles   = new Argument<string[]>();

            var rootCommand = new RootCommand() {
                opGenKey,
                argsFiles
            };

            rootCommand.SetHandler(
                ProcessArgs, 
                opGenKey, 
                argsFiles
            );

            rootCommand.Invoke(new string[] {"--genkey", "file1.txt", "file2.txt"}); //e.Args

            if (flag_genkey != null) {
                System.Windows.MessageBox.Show(
                    messageBoxText: "GenKey",
                    caption: "Doing that right now...",
                    button: System.Windows.MessageBoxButton.OK,
                    icon: System.Windows.MessageBoxImage.Information
                );
            }
            
            foreach (string file in args_files) {
                System.Windows.MessageBox.Show(
                    messageBoxText: "File",
                    caption: file,
                    button: System.Windows.MessageBoxButton.OK,
                    icon: System.Windows.MessageBoxImage.Information
                );          
            }
        }
    } //Class
} // Namespace
1 Answers

change to:

 var opGenKey    = new Option<string>("--genkey");
Related