Exemplo n.º 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;
            }
        }
Exemplo n.º 2
0
        public static Command Parse(string[] args)
        {
            const string DEFAULT_TEMPLATE_FILENAME = "template.cs";

            if (args.Length == 1 && Path.GetExtension(args[0])?.ToLower() == ".csrun")
            {
                return(new RunCommand(args[0], DEFAULT_TEMPLATE_FILENAME));
            }

            var schema = new appcfg.AppCfgSchema("csrun.exe.config.json",
                                                 new Route("run", isDefault: true)
                                                 .Param("sourceFilename", "s,f,source,filename", ValueTypes.String, isRequired: true)
                                                 .Param("templateFilename", "t,template", ValueTypes.String, defaultValue: DEFAULT_TEMPLATE_FILENAME),
                                                 new Route("test")
                                                 .Param("sourceFilename", "s,f,source,filename", ValueTypes.String, isRequired: true)
                                                 .Param("jsonOutput", "json")
                                                 .Param("templateFilename", "t,template", ValueTypes.String, defaultValue: DEFAULT_TEMPLATE_FILENAME),
                                                 new Route("watch")
                                                 .Param("sourceFilename", "s,f,source,filename", ValueTypes.String, isRequired: true)
                                                 .Param("templateFilename", "t,template", ValueTypes.String, defaultValue: DEFAULT_TEMPLATE_FILENAME));

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

                switch (cfg._RoutePath)
                {
                case "run": return(new RunCommand(cfg.sourceFilename, cfg.templateFilename));

                case "test": return(new TestCommand(cfg.sourceFilename, cfg.jsonOutput, cfg.templateFilename));

                case "watch": return(new WatchCommand(cfg.sourceFilename, cfg.templateFilename));

                default: throw new ApplicationException($"Unknown command: '{cfg._RoutePath}'!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("*** Error found in command line: {0}", ex.Message);
                Console.WriteLine(@"
Usage:

csrun.exe <source filename> // run main section
csrun.exe -f <source filename> [ -t <template filename> ]
csrun.exe test -f <source filename> [ -json ]  [ -t <template filename> ] // run tests
csrun.exe watch -f <source filename>  [ -t <template filename> ] // run tests whenever the source file changes
");
                Environment.Exit(1);
                throw new ApplicationException("this is never reached. it just soothes the compiler");
            }
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
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;
        }
Exemplo n.º 5
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);
        }