예제 #1
0
        public bool DefineParameters(string[] args, Setup setupInformation)
        {
            string userCommand = string.Empty;
            try
            {
                bool result = CommandLineParser.ParseForConsoleApplication(delegate (CommandLineParser parser)
                {
                    //Settings
                    foreach (KeyValuePair<string, Setting> option in setupInformation.Settings)
                    {
                        string temp = "";
                        parser.DefineOptionalQualifier(option.Key, ref temp, option.Value.Description, option.Value.DefaultValue, option.Value.Values);
                        SettingParameters[option.Key] = temp;
                    }

                    //Commands
                    foreach (KeyValuePair<string, Command> comm in setupInformation.Commands)
                    {
                        parser.DefineParameterSet(comm.Key, ref userCommand, comm.Key, string.Format("Help for {0}", comm.Key));
                        Dictionary<string, string> param = new Dictionary<string, string>();
                        foreach(KeyValuePair<string, AliasPerCommand> aliasInfo in comm.Value.Alias)
                        {
                            string temp = "";
                            parser.DefineOptionalQualifier(aliasInfo.Key, ref temp, aliasInfo.Value.Description, null, null);
                            if(!string.IsNullOrEmpty(temp) && !temp.Equals("true"))
                            {
                                List<string> keys = new List<string>(aliasInfo.Value.Settings.Keys);
                                if(keys.Count < 2)
                                {
                                    foreach (string key in keys)
                                    {
                                        setupInformation.Commands[comm.Key].Alias[aliasInfo.Key].Settings[key] = temp;
                                    }
                                }
                            }

                            param[aliasInfo.Key] = temp;
                        }
                        CommandParameters[comm.Key] = new Dictionary<string, string>(param);
                    }
                }, args, setupInformation);
                SettingParameters["ExtraParameters"] = CommandLineParser.ExtraParameters;
                CommandSelectedByUser = userCommand;
                setupInformation.SettingParameters = SettingParameters;
                return result;
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error: {0} {1}", e.Message, e.StackTrace);
                return false;
            }
            
        }
예제 #2
0
        public static int Main(string[] args)
        {
            string[] parseArgs;
            Executor executor;

            if (args.Length > 0 && args[0].EndsWith(".json"))
            {
                executor  = new Executor(args[0]);
                parseArgs = new string[args.Length - 1];
                Array.Copy(args, 1, parseArgs, 0, args.Length - 1);
            }
            else
            {
                executor  = new Executor();
                parseArgs = args;
            }

            Setup jsonSetup = executor.OpenFile();

            if (jsonSetup == null)
            {
                Console.Error.WriteLine("Error: Could not load Json configuration file.");
                return(1);
            }
            string os = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "windows" : "unix";

            if (jsonSetup.PrepareValues(os, executor.SettingParameters, executor.configFilePath) == 0)
            {
                if (executor.DefineParameters(parseArgs, jsonSetup))
                {
                    if (string.IsNullOrEmpty(executor.CommandSelectedByUser))
                    {
                        Console.Error.WriteLine("Error: No command was passed. Use -? for help.");
                        return(1);
                    }

                    List <string> paramSelected = new List <string>();
                    foreach (KeyValuePair <string, string> param in executor.CommandParameters[executor.CommandSelectedByUser])
                    {
                        if (!string.IsNullOrEmpty(param.Value))
                        {
                            paramSelected.Add(param.Key);
                        }
                    }

                    // If aliases exist, and the user's parameters have no match, we'll end up in this state.
                    // If a default alias is provided, we can add that; otherwise we should error out as the
                    // behavior at this point may be unexpected.
                    if (paramSelected.Count == 0 && executor.CommandParameters[executor.CommandSelectedByUser].Count > 0)
                    {
                        string defaultAlias = jsonSetup.Commands[executor.CommandSelectedByUser].DefaultValues.DefaultAlias;
                        if (!string.IsNullOrEmpty(defaultAlias))
                        {
                            Console.WriteLine($"No parameter selected, using default alias '{defaultAlias}'");
                            paramSelected.Add(defaultAlias);
                        }
                        // May want to error out here in the future;  Need to find out if there's ever a reason
                        // to allow aliases, but specify none.
                    }

                    return(jsonSetup.ExecuteCommand(executor.CommandSelectedByUser, paramSelected));
                }
            }
            //There was an error when parsing the user input, Define Parameters is in charge of printing an error message.
            return(1);
        }