Beispiel #1
0
        public static void Main(string[] args)
        {
            var cfgSchema = new appcfg.AppCfgSchema("democonfig.json",
                                                    new Route("add", "addition", isDefault: true)
                                                    .Param("a", "x", valueType: ValueTypes.Number, isRequired: true)
                                                    .Param("b", "y", valueType: ValueTypes.Number, isRequired: true),
                                                    new Route("sum", "summation")
                                                    .Param("*", valueType: ValueTypes.Number),
                                                    new Route("div", "division")
                                                    .Param("a", "x,divident", valueType: ValueTypes.Number, isRequired: true)
                                                    .Param("b", "y,divisor", valueType: ValueTypes.Number, isRequired: true)
                                                    .Param("round", "r", valueType: ValueTypes.Bool)
                                                    .Param("suppressDivBy0", valueType: ValueTypes.Bool, defaultValue: false, environmentVarName: "DEMO_SUPPRESSDIVBY0")
                                                    .Param("silent")
                                                    );

            var cfgcomp = new AppCfgCompiler(cfgSchema);

            var cfg = cfgcomp.Compile(args);

            switch (cfg._RoutePath)
            {
            case "add":
                var addResult = cfg.a + cfg.b;
                Console.WriteLine($"{cfg.a} + {cfg.b} = {addResult}");
                break;

            case "sum":
                var sumResult = 0;
                foreach (var x in cfg._CatchAll)
                {
                    sumResult += x;
                }
                Console.WriteLine($"Sum of all params: {sumResult}");
                break;

            case "div":
                double divResult = 0;
                if (cfg.suppressDivBy0 && cfg.b == 0)
                {
                    divResult = double.MaxValue;
                }
                else
                {
                    divResult = cfg.a / cfg.b;
                }

                if (cfg.round)
                {
                    divResult = Math.Round(divResult);
                }

                if (!cfg.silent)
                {
                    Console.WriteLine($"{cfg.a} / {cfg.b} = {divResult}");
                }
                break;
            }
        }
        public static void Load(string[] args)
        {
            var schema = new appcfg.AppCfgSchema(
                "ff.backend.config.json",
                new Route("run", "start", isDefault: true)
                .Param("address", "a", ValueTypes.String, "FEATUREFORECAST_BACKEND_ADDRESS", defaultValue: "http://localhost:80")
                .Param("dbpath", "db", ValueTypes.String, "FEATUREFORECAST_BACKEND_DATABASEPATH", defaultValue: ".")
                );

            var comp = new AppCfgCompiler(schema);
            var cfg  = comp.Compile(args);

            Address = new Uri(cfg.address);
            DbPath  = cfg.dbpath;
        }
Beispiel #3
0
        public static void Load(string[] args)
        {
            var schema = new AppCfgSchema(
                "dyp.service.config.json",
                new Route("run", "start", isDefault: true)
                .Param("address", "a", ValueTypes.String, "DYPAPP_SERVICE_ADDRESS", defaultValue: "http://localhost")
                .Param("dbpath", "db", ValueTypes.String, "DYPAPP_SERVICE_DATABASEPATH", defaultValue: "..//repository")
                );

            //.Param("address", "a", ValueTypes.String, "DYPAPP_SERVICE_ADDRESS", defaultValue: "http://192.168.178.26:8080")

            var comp = new AppCfgCompiler(schema);
            var cfg  = comp.Compile(args);

            Address = new Uri(cfg.address);
            DbPath  = cfg.dbpath;
        }
Beispiel #4
0
        public void Compile_from_all_sources()
        {
            Environment.SetEnvironmentVariable("fromEnv", "42");
            Environment.SetEnvironmentVariable("cfgfileOverridesEnv", "env2");
            Environment.SetEnvironmentVariable("cmdlineOverridesEnv", "env3");

            var args = new[] { "route2",
                               "-fromCmdline:cmdline1",
                               "/cmdlineOverridesCfgfile=cmdline2",
                               "-cmdlineOverridesEnv", "cmdline3",
                               "-onCmdline",
                               "cl*1", "cl*2" };
            var schema = new AppCfgSchema("config2.json",
                                          new Route("route1", "")
                                          .Param("x")
                                          .Param("y"),
                                          new Route("route2")
                                          .Param("onCmdline")
                                          .Param("missingFromCmdline")
                                          .Param("missingWithDefault", valueType: ValueTypes.Number, defaultValue: 42)
                                          .Param("fromEnv", valueType: ValueTypes.Number)
                                          .Param("fromCfgfile", valueType: ValueTypes.DateTime)
                                          .Param("fromCmdline", valueType: ValueTypes.String)
                                          .Param("cfgfileOverridesEnv", valueType: ValueTypes.String)
                                          .Param("cmdlineOverridesCfgfile", valueType: ValueTypes.String)
                                          .Param("cmdlineOverridesEnv", valueType: ValueTypes.String)
                                          .Param("*", valueType: ValueTypes.String)
                                          );
            var sut = new AppCfgCompiler(schema);

            var cfg = sut.Compile(args);

            Assert.AreEqual("route2", cfg._RoutePath);
            Assert.AreEqual(true, cfg.onCmdline);
            Assert.AreEqual(false, cfg.missingFromCmdline);
            Assert.AreEqual(42, cfg.missingWithDefault);
            Assert.AreEqual(42, cfg.fromEnv);
            Assert.AreEqual(new DateTime(2017, 5, 12, 10, 53, 17), cfg.fromCfgfile);
            Assert.AreEqual("cmdline1", cfg.fromCmdline);
            Assert.AreEqual("cfgfile2", cfg.cfgfileOverridesEnv);
            Assert.AreEqual("cmdline2", cfg.cmdlineOverridesCfgfile);
            Assert.AreEqual("cmdline3", cfg.cmdlineOverridesEnv);
            Assert.AreEqual(new[] { "cl*1", "cl*2" }, cfg._CatchAll);
        }