Cmdline for WPF/Winforms apps

Nov 27, 2010 at 7:03 PM

Hi.  I found this code very useful.  Thanks!  However, I needed to use it in a WPF application so I added the following class:

public class GuiCmdLine : CmdLine
    {
        public GuiCmdLine(Action<string, int> showResult)
        {
            base.RegisterParameter(new CmdLineString("help", false, "Prints the help screen."));
            _showResult = showResult;
        }

        Action<string, int> _showResult;

        public new string[] Parse(string[] args)
        {
            string[] ret = null;
            string error = "";
            try
            {
                ret = base.Parse(args);
            }
            catch (CmdLineException ex)
            {
                error = ex.Message;
            }

            if (this["help"].Exists)
            {
                //foreach(string s in base.HelpScreen().Split('\n'))
                //    Console.WriteLine(s);
                _showResult(base.HelpScreen(), 0);
            }

            if (error != "")
            {
                _showResult(error, 1);
            }
            return ret;
        }
    }
This is called from App.xaml.cs OnStartup()
GuiCmdLine gcl = new GuiCmdLine((message, exitcode) => { 
  MessageBox.Show(message, exitcode == 0 ? "Command Line Parameters" : "Command Line Error", MessageBoxButton.OK); 
}); 
// ... add parameters and parse after this
Cheers