예제 #1
0
            /// <summary>
            /// Runs the program, analogous to System.Windows.Forms.Application.Run.
            /// </summary>
            public void Run()
            {
                //Check that we've got an action corresponding to one the user requested.
                if (!Handlers.ContainsKey(Arguments.Action))
                {
                    throw new ArgumentException(S._("Unknown action {0}", Arguments.Action));
                }

                //Re-parse the command line arguments as arguments for the given action.
                ConsoleActionData data      = Handlers[Arguments.Action];
                ConsoleArguments  arguments = data.Arguments;

                ComLib.BoolMessageItem <Args> parseResult = Args.Parse(CommandLine,
                                                                       CommandLinePrefixes, CommandLineSeparators, arguments);
                if (!parseResult.Success)
                {
                    throw new ArgumentException(parseResult.Message);
                }

                //Remove the action from the positional arguments before sending it to the handler
                System.Diagnostics.Debug.Assert(Arguments.Action == parseResult.Item.Positional[0]);
                parseResult.Item.Positional.RemoveAt(0);
                arguments.PositionalArguments = parseResult.Item.Positional;

                //Then invoke the handler for this action.
                data.Handler(arguments);
            }
예제 #2
0
        static int Main(string[] rawCommandLine)
        {
            //Immediately parse command line arguments. Start by substituting all
            //response files ("@filename") arguments with the arguments found in the
            //file
            List <string> commandLine = new List <string>(rawCommandLine.Length);

            foreach (string argument in rawCommandLine)
            {
                if (argument[0] == '@' && File.Exists(argument.Substring(1)))
                {
                    //The current parameter is a response file, parse the file
                    //for arguments and substitute it.
                    using (TextReader reader = new StreamReader(argument.Substring(1)))
                    {
                        commandLine.AddRange(Shell.ParseCommandLine(reader.ReadToEnd()));
                    }
                }
                else
                {
                    commandLine.Add(argument);
                }
            }

            string[] finalCommandLine             = commandLine.ToArray();
            ComLib.BoolMessageItem argumentParser = Args.Parse(finalCommandLine,
                                                               CommandLinePrefixes, CommandLineSeparators);
            Args parsedArguments = (Args)argumentParser.Item;

            //Set application defaults for consistent behaviour across GUI and
            //Console instances
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //Load the Eraser.Manager library
            using (ManagerLibrary library = new ManagerLibrary(Settings.Get()))
            {
                //Set our UI language
                EraserSettings settings = EraserSettings.Get();
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(settings.Language);

                //We default to a GUI if:
                // - The parser did not succeed.
                // - The parser resulted in an empty arguments list
                // - The parser's argument at index 0 is not equal to the first argument
                //   (this is when the user is passing GUI options -- command line options
                //   always start with the action, e.g. Eraser help, or Eraser addtask
                if (!argumentParser.Success || parsedArguments.IsEmpty ||
                    parsedArguments.Positional.Count == 0 ||
                    parsedArguments.Positional[0] != parsedArguments.Raw[0])
                {
                    GUIMain(finalCommandLine);
                }
                else
                {
                    return(CommandMain(finalCommandLine));
                }
            }

            //Return zero to signify success
            return(0);
        }