Пример #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;
            }
        }
Пример #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");
            }
        }
Пример #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;
        }
Пример #4
0
 public AppCfgCompiler(AppCfgSchema schema)
 {
     _schema = schema;
 }
Пример #5
0
        public static dynamic Compile(this AppCfgSchema schema, string[] args)
        {
            var compiler = new AppCfgCompiler(schema);

            return(compiler.Compile(args));
        }