Esempio n. 1
0
        /// <summary>
        /// Parses command line. Instantiates command if not already instantiated.
        /// Cannot change command type without creating new instance of CommandContext
        /// </summary>
        /// <param name="cmdLine">command line to process</param>
        /// <param name="diffAtStart">true if configuration comparison starts from start of command line processing, false if after main
        /// command is executed</param>
        public IEnumerable <Option> ParseCommandLine2(string cmdLine, bool diffAtStart = false, bool logCalls = false)
        {
            var console = LogService.console;

            console.Info($"> {cmdLine}");
            string[] args = ChocolateyOptionSet.SplitArgs(cmdLine);
            ChocolateyConfiguration config = new ChocolateyConfiguration();

            parser = new ChocolateyOptionSet();

            if (diffAtStart)
            {
                original = config.deep_copy();
            }
            if (parser.Parse(args, new ChocolateyStartupCommand(), config, () =>
            {
                console.Info("- logInit called.");
            }
                             ))
            {
                console.Info("chocoArgsParse: return on startup");
                return(parser);
            }

            parser = new ChocolateyOptionSet();

            if (parser.Parse(args, new ChocolateyMainCommand(), config))
            {
                console.Info("chocoArgsParse: return on main");
                return(parser);
            }

            genericArgumentsCount = parser.Count;

            if (args.Length == 0)
            {
                "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "Please run 'choco -?' or 'choco <command> -?' for help menu.");
            }

            InstantiateCommand(config.CommandName, logCalls);
            if (command == null)
            {
                return(parser.Skip(genericArgumentsCount));
            }

            LogService.Instance.adjustLogLevels(config.Debug, config.Verbose, config.Trace);

            if (!diffAtStart)
            {
                original = config.deep_copy();
            }

            string phase = "parse";

            try
            {
                if (!parser.Parse(args.Skip(1), command, config))
                {
                    phase = "validation";
                    command.handle_validation(config);

                    console.Info("config: " + config.CompareWith(original));

                    // GenericRunner.run logic, see after line:
                    // var command = find_command(config, container, isConsole, parseArgs);
                    if (config.Noop)
                    {
                        if (config.RegularOutput)
                        {
                            this.Log().Info("_ {0}:{1} - Noop Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
                        }

                        command.noop(config);
                    }
                    else
                    {
                        this.Log().Debug("_ {0}:{1} - Normal Run Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
                        command.run(config);
                    }
                }
            }
            catch (ApplicationException ex)
            {
                console.Info($"{phase} failure: " + ex.Message);
            }

            return(parser.Skip(genericArgumentsCount));
        }