Example #1
0
        public void UnknownParameter_Throw()
        {
            CommandLineParser parser = new CommandLineParser();
            parser.Options.Add(new CommandLineOption(
                new[] { "p" }, "none", x => { throw new NotImplementedException(); }));

            Assert.Throws<FormatException>(() => parser.Parse(new[] { "/x=v" }));
        }
Example #2
0
        public void ActionThrows_Throw()
        {
            CommandLineParser parser = new CommandLineParser();
            parser.Options.Add(new CommandLineOption(
                new[] { "p" }, "none", x => { throw new NotImplementedException(); }));

            Assert.Throws<NotImplementedException>(() => parser.Parse(new[] { "/p=v" }));
        }
Example #3
0
        public void ParseEmptyParameters_Success()
        {
            CommandLineParser parser = new CommandLineParser();
            parser.Options.Add(new CommandLineOption(
                new[] { "p" }, "none", x => { throw new NotImplementedException(); }));

            IList<string> extra;
            extra = parser.Parse(new string[] { });
            Assert.Equal(0, extra.Count);
            extra = parser.Parse(new[] { string.Empty });
            Assert.Equal(1, extra.Count);
        }
Example #4
0
        public void MultiplyOcurringParameter_ActionInvokedMultipleTimes()
        {
            CommandLineParser parser = new CommandLineParser();
            int callbackCount = 0;
            string callbackValue = string.Empty;
            parser.Options.Add(new CommandLineOption(
                new[] { "known" }, "a known parameter",
                x => { callbackCount++; callbackValue += x; }));

            IList<string> extra = parser.Parse(new[] { "/known=a", "-known=b", "/known=\"c\"", "extra", "/known=d" });

            Assert.Equal(3, callbackCount);
            Assert.Equal("abc", callbackValue);
            Assert.Equal(2, extra.Count);
            Assert.Equal("extra", extra[0]);
            Assert.Equal("/known=d", extra[1]);
        }
Example #5
0
        public void ExpectedLongUsage_Success()
        {
            string server = null, url = null, port = null, output = null,
                settings = null, verbose = null, help = null, boot = null;
            CommandLineParser parser = new CommandLineParser();
            parser.Options.Add(new CommandLineOption(new[] { "s", "server" }, string.Empty, x => server = x));
            parser.Options.Add(new CommandLineOption(new[] { "u", "url" }, string.Empty, x => url = x));
            parser.Options.Add(new CommandLineOption(new[] { "p", "port" }, string.Empty, x => port = x));
            parser.Options.Add(new CommandLineOption(new[] { "o", "output" }, string.Empty, x => output = x));
            parser.Options.Add(new CommandLineOption(new[] { "settings" }, string.Empty, x => settings = x));
            parser.Options.Add(new CommandLineOption(new[] { "v", "verbose" }, string.Empty, x => verbose = x));
            parser.Options.Add(new CommandLineOption(new[] { "?", "help" }, string.Empty, x => help = x));
            parser.Options.Add(new CommandLineOption(new[] { "b", "boot" }, string.Empty, x => boot = x));

            IList<string> extra = parser.Parse(new[] 
            {
                "-server=Microsoft.Owin.Host.HttpListener",
                "/url=http://localhost:5000/path/",
                "/port=5000",
                "/output=\"c:\\file.log\"",
                "/settings=Microsoft.Owin.Hosting.config",
                "/verbose",
                "/help",
                "/boot=Microsoft.Owin.Boot.AspNet",
                "My.Application"
            });

            Assert.Equal("Microsoft.Owin.Host.HttpListener", server);
            Assert.Equal("http://localhost:5000/path/", url);
            Assert.Equal("5000", port);
            Assert.Equal("c:\\file.log", output);
            Assert.Equal("Microsoft.Owin.Hosting.config", settings);
            Assert.Equal(string.Empty, verbose);
            Assert.Equal(string.Empty, help);
            Assert.Equal("Microsoft.Owin.Boot.AspNet", boot);
            Assert.Equal(1, extra.Count);
            Assert.Equal("My.Application", extra[0]);
        }
Example #6
0
        public void BadData_ThrowFormatException(string input)
        {
            CommandLineParser parser = new CommandLineParser();
            parser.Options.Add(new CommandLineOption(
                new[] { "param" }, "none", x => { throw new NotImplementedException(); }));

            Assert.Throws<FormatException>(() => parser.Parse(new[] { input }));
        }
Example #7
0
        public void KnownParameter_Success(string input, string param, string value)
        {
            CommandLineParser parser = new CommandLineParser();
            int callbackCount = 0;
            string callbackValue = null;
            parser.Options.Add(new CommandLineOption(
                new[] { param }, "a known parameter", 
                x => { callbackCount++; callbackValue = x; }));
            parser.Options.Add(new CommandLineOption(
                new[] { "unknown" }, "none", x => { throw new NotImplementedException(); }));

            IList<string> extra = parser.Parse(new[] { input });

            Assert.Equal(1, callbackCount);
            Assert.Equal(value, callbackValue);
            Assert.Equal(0, extra.Count);
        }
Example #8
0
        private static void ShowHelp(CommandLineParser parser)
        {
            Console.WriteLine("Usage: OwinHost [options] [<application>]");
            Console.WriteLine("Runs <application> on an http server");
            Console.WriteLine("Example: OwinHost /p=5000 HelloWorld.Startup");
            Console.WriteLine();
            Console.WriteLine("Options:");

            foreach (CommandLineOption option in parser.Options)
            {
                Console.WriteLine(string.Format("   /{0} - {1}", option.Parameters.Aggregate((s1, s2) => s1 + ", /" + s2), option.Description));
            }

            Console.WriteLine();
            Console.WriteLine("Environment Variables:");
            Console.WriteLine("PORT                         Changes the default TCP port to listen on when");
            Console.WriteLine("                               both /port and /url options are not provided.");
            Console.WriteLine("OWIN_SERVER                  Changes the default server TYPE to use when");
            Console.WriteLine("                               the /server option is not provided.");
        }
Example #9
0
        private static StartOptions ParseArguments(IEnumerable<string> args)
        {
            var options = new StartOptions();
            bool showHelp = false;

            CommandLineParser parser = new CommandLineParser();
            parser.Options.Add(new CommandLineOption(
                    new[] { "s", "serverfactory" },
                    @"Load the specified server factory type or assembly. The default is to auto-detect.",
                    x => options.ServerFactory = x));
            parser.Options.Add(new CommandLineOption(
                    new[] { "u", "url" },
                    @"Format is '<scheme>://<host>[:<port>]<path>/'.",
                    x => options.Urls.Add(x)));
            parser.Options.Add(new CommandLineOption(
                    new[] { "p", "port" },
                    @"Which TCP port to listen on. Default is 5000.",
                    x => options.Port = int.Parse(x, CultureInfo.InvariantCulture)));
            parser.Options.Add(new CommandLineOption(
                    new[] { "d", "directory" },
                    @"Specifies the target directory of the application.",
                    x => options.Settings["directory"] = x));
            parser.Options.Add(new CommandLineOption(
                    new[] { "t", "traceoutput" },
                    @"Writes any trace data to the given FILE. Default is stderr.",
                    x => options.Settings["traceoutput"] = x));
            parser.Options.Add(new CommandLineOption(
                    new[] { "settings" },
                    @"The settings file that contains service and setting overrides. Otherwise they are read from the AppSettings section of the app's config file.",
                    x => LoadSettings(options, x)));
            parser.Options.Add(new CommandLineOption(
                    new[] { "v", "traceverbosity" },
                    @"Enable verbose tracing.",
                    x =>
                    {
                        if (string.IsNullOrWhiteSpace(x))
                        {
                            x = "1";
                        }
                        options.Settings["traceverbosity"] = x;
                    }));
            parser.Options.Add(new CommandLineOption(
                    new[] { "b", "boot" },
                    @"Loads an assembly to provide custom startup control.",
                    x => options.Settings["boot"] = x));
            parser.Options.Add(new CommandLineOption(
                    new[] { "?", "help" },
                    @"Show this message and exit.",
                    x => showHelp = true));

            IList<string> extra;
            try
            {
                extra = parser.Parse(args);
            }
            catch (FormatException e)
            {
                Console.Write("OwinHost: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try 'OwinHost /?' for more information.");
                return null;
            }
            if (showHelp)
            {
                ShowHelp(parser);
                return null;
            }
            options.AppStartup = string.Join(" ", extra.ToArray());
            return options;
        }