Esempio n. 1
0
        //================================
        // Methods
        //================================
        public static Parameter CreateParameter(string[] args)
        {
            var parameter = new Parameter();

            var parser = new Parser();

            //-------- Setting --------
            parser.ArgumentEvent += delegate(object sender, ArgumentEventArgs e)
            {
                if (args.Length > 0)
                {
                    parameter.ProjectFileName = e.Args[0];
                }
            };

            parser.AddOptionDefinition(new OptionDefinition("-o", "--output")
            {
                Type         = OptionType.RequireValue,
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    parameter.OutputDirectory = e.Value;
                }
            });

            parser.AddOptionDefinition(new OptionDefinition("-v", "--view-detail")
            {
                Type         = OptionType.NoValue,
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    parameter.ViewDetail = true;
                }
            });

            parser.AddOptionDefinition(new OptionDefinition("--variable")
            {
                Type         = OptionType.RequireValue,
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    var index = e.Value.IndexOf('=');
                    if (index > -1)
                    {
                        var key   = e.Value.Substring(0, index);
                        var value = string.Empty;
                        if (index < e.Value.Length)
                        {
                            value = e.Value.Substring(index + 1);
                        }
                        parameter.Variables.Add(key, value);
                    }
                    parameter.ViewDetail = true;
                }
            });

            parser.AddOptionDefinition(new OptionDefinition("--log-level")
            {
                Type         = OptionType.RequireValue,
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    parameter.LogLevel = e.Value;
                }
            });

            parser.AddOptionDefinition(new OptionDefinition("--intermediates")
            {
                Type         = OptionType.RequireValue,
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    parameter.Intermediates = e.Value;
                }
            });

            parser.AddOptionDefinition(new OptionDefinition("--skip-scheme-validation")
            {
                Type         = OptionType.NoValue,
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    parameter.SkipSchemeValidation = true;
                }
            });

            parser.AddOptionDefinition(new OptionDefinition("-s", "--setting-root")
            {
                Type         = OptionType.RequireValue,
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    parameter.SettingRoot = e.Value;
                }
            });


            //-------- Parse --------
            parser.Parse(args);

            //-------- Correct --------
            if (parameter.SettingRoot == String.Empty)
            {
                parameter.SettingRoot = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            }

            if (parameter.ProjectSchema == String.Empty)
            {
                parameter.ProjectSchema = Path.Combine(parameter.SettingRoot, DEFAULT_PROJECT_SCHEMA);
            }

            if (parameter.CatalogSchema == String.Empty)
            {
                parameter.CatalogSchema = Path.Combine(parameter.SettingRoot, DEFAULT_CATALOG_SCHEMA);
            }

            return(parameter);
        }
Esempio n. 2
0
        static int Main(string[] args)
        {
            Writer writer = new Writer()
            {
                EnabledWrite = true
            };


            writer.Write("-------- Start test. -------- ");

            //Parser setting.
            var parser = new Parser();

            parser.ArgumentEvent += delegate(object sender, ArgumentEventArgs e)
            {
                for (int i = 0; i < e.Args.Length; i++)
                {
                    writer.Write("  Argument[" + (i + 1).ToString() + "] : " + e.Args[i]);
                }
            };

            parser.ErrorEvent += delegate(object sender, ErrorEventArgs e)
            {
                var s = "Parse Error : ";

                switch (e.Type)
                {
                case ErrorType.NoValue:
                    s += "Option '" + e.OptionName + "' needs value.";
                    break;
                }

                writer.Write(s);
            };

            parser.AddOptionDefinition(new OptionDefinition("-s")
            {
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    writer.Write("  " + e.Name + " (No Value).");
                }
            });

            parser.AddOptionDefinition(new OptionDefinition("-a", "--a-longname")
            {
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    writer.Write("  " + e.Name + " (No Value).");
                }
            });

            parser.AddOptionDefinition(new OptionDefinition("-v")
            {
                Type         = OptionType.RequireValue,
                EventHandler = delegate(object sender, OptionEventArgs e)
                {
                    writer.Write("  " + e.Name + " : " + e.Value);
                }
            });


            //Test.
            var testData = new string[]
            {
                "value1",
                "-s srcpath destpath",
                "-a srcpath destpath",
                "--a-longname srcpath destpath",
                "-s",
                "-v opt path",
                "-v opt1 -v opt2",
                "-v -s srcpath destpath"
            };

            for (int i = 0; i < testData.Length; i++)
            {
                writer.Write("[Case" + (i + 1).ToString() + "]");
                writer.Write("Input :");
                writer.Write("  " + testData[i]);
                writer.Write("Output :");
                parser.Parse(testData[i].Split(' '));
                writer.Write("");
            }

            writer.Write("-------- End test. --------");

            return(0);
        }