Example #1
0
        public static IArgOptions Parse(string[] args, out bool parsedOK)
        {
            var options = new ArgOptions();
            var parser  = new Fclp.FluentCommandLineParser <ArgOptions>();

            parser.Setup(arg => arg.Host).As('H', "host").SetDefault("127.0.0.1").WithDescription("host").Callback(arg => { options.Host = arg; Logger.DebugFormat("host = " + arg); });
            parser.Setup(arg => arg.Port).As('p', "port").Required().SetDefault(9111).WithDescription("port").Callback(arg => { options.Port = arg; Logger.DebugFormat("port = " + arg); });
            parser.Setup(arg => arg.SendInterval).As('s', "sendInterval").SetDefault(100).WithDescription("interval milliseconds").Callback(arg => { options.SendInterval = arg; Logger.DebugFormat("send interval = " + arg + " ms"); });
            parser.Setup(arg => arg.RunningSeconds).As('r', "runningSeconds").SetDefault(3600).WithDescription("running seconds").Callback(arg => { options.RunningSeconds = arg; Logger.DebugFormat("running seconds = " + arg + " s"); });
            parser.Setup(arg => arg.MessagesPerConnection).As('n', "messagesPerConnection").SetDefault(0).WithDescription("send message count per connection. 0 = no limit").Callback(arg => { options.MessagesPerConnection = arg; Logger.DebugFormat("messages per connection = " + arg); });
            parser.Setup(arg => arg.KeysPerConnection).As('n', "keysPerConnection").SetDefault(0).WithDescription("send message count per connection. 0 = no limit").Callback(arg => { options.KeysPerConnection = arg; Logger.DebugFormat("keys per connection = " + arg); });
            parser.Setup(arg => arg.QuitIfExceededAny).As('q', "quitIfExceeded").SetDefault(true).WithDescription("quit if exceed time or message-count").Callback(arg => { options.QuitIfExceededAny = arg; Logger.DebugFormat("quit if exceeded any condition = " + arg); });
            parser.Setup(arg => arg.MaxConnectTimes).As('q', "maxConnectTimes").SetDefault(0).WithDescription("quit if exceed time or message-count").Callback(arg => { options.MaxConnectTimes = arg; Logger.DebugFormat("quit if exceeded any condition = " + arg); });
            parser.Setup(arg => arg.PauseSecondsAtDrop).As('q', "pause seconds at each connection lost").SetDefault(0).WithDescription("pause seconds at each connection lost").Callback(arg => { options.PauseSecondsAtDrop = arg; Logger.DebugFormat("pause seconds at each connection lost = " + arg); });
            parser.SetupHelp("h", "help").Callback(text => Console.WriteLine(text));
            var result = parser.Parse(args);

            //Log(string.Format("ParserFluentArgs : p = {0}", p.ToString()));
            if (result.HasErrors || args.Length < 1)
            {
                parser.HelpOption.ShowHelp(parser.Options);
                parsedOK = false;
            }

            parsedOK = true;
            return(options);
        }
        public void Setup_Help_And_Ensure_It_Is_Called()
        {
            var parser = new Fclp.FluentCommandLineParser();

            parser.IsCaseSensitive = false;

            var formatter = new Mock <ICommandLineOptionFormatter>();

            var          args = new[] { "/help", "i", "s" };
            const string expectedCallbackResult = "blah";
            bool         wasCalled = false;

            parser.SetupHelp("?", "HELP", "h")
            .Callback(() => wasCalled = true);

            parser.Setup <int>('i');
            parser.Setup <string>('s');

            formatter.Setup(x => x.Format(parser.Options)).Returns(expectedCallbackResult);

            var result = parser.Parse(args);

            Assert.IsTrue(wasCalled);
            Assert.IsFalse(result.HasErrors);
            Assert.IsTrue(result.HelpCalled);
        }
        public void Setup_Help_And_Ensure_It_Is_Called_With_Custom_Formatter()
        {
            var parser = new Fclp.FluentCommandLineParser();

            parser.IsCaseSensitive = false;

            var formatter = new Mock <ICommandLineOptionFormatter>();

            var          args = new[] { "/help", "i", "s" };
            const string expectedCallbackResult = "blah";
            string       callbackResult         = null;

            parser.SetupHelp("?", "HELP", "h")
            .Callback(s => callbackResult = s)
            .WithCustomFormatter(formatter.Object);

            parser.Setup <int>('i');
            parser.Setup <string>('s');

            formatter.Setup(x => x.Format(parser.Options)).Returns(expectedCallbackResult);

            var result = parser.Parse(args);

            Assert.AreSame(expectedCallbackResult, callbackResult);
            Assert.IsFalse(result.HasErrors);
            Assert.IsTrue(result.HelpCalled);
        }
Example #4
0
        private static void GetParsedOtions(string[] args)
        {
            if (args.Length > 0)
            {
                // create a generic parser for the ApplicationArguments type
                var parser = new Fclp.FluentCommandLineParser <MyDiaryOptions>();

                // specify which property the value will be assigned too.
                parser.Setup <FileTypeEnum>(arg => arg.FileType)
                .As('f', "fileType");       // define the short and long option name
                //.Required()// using the standard fluent Api to declare this Option as required.
                // .SetDefault("Production");

                parser.Setup(arg => arg.Option)
                .As('r', "Run")
                .Required();

                var result = parser.Parse(args);

                parser.SetupHelp("?", "help")
                .Callback(text => Console.WriteLine(text));

                // triggers the SetupHelp Callback which writes the text to the console
                parser.HelpOption.ShowHelp(parser.Options);

                var myDiaryOptions = parser.Object;

                switch (myDiaryOptions.FileType)
                {
                case FileTypeEnum.Production:
                    var a = 1;

                    break;

                case FileTypeEnum.Submission:
                    var b = 2;
                    break;
                }


                if (result.HasErrors == false)
                {
                    // use the instantiated ApplicationArguments object from the Object property on the parser.
                    // application.Run(p.Object);
                }
            }
        }
        public static void Main(string[] args)
        {
            var commandLineParser = new Fclp.FluentCommandLineParser <Options>();

            commandLineParser
            .Setup(options => options.Prefix)
            .As("prefix")
            .SetDefault("http://+:8080/")
            .WithDescription("HTTP prefix to listen on");

            commandLineParser
            .SetupHelp("h", "help")
            .WithHeader($"{System.AppDomain.CurrentDomain.FriendlyName} [--prefix <prefix>]")
            .Callback(text => System.Console.WriteLine(text));

            if (commandLineParser.Parse(args).HelpCalled)
            {
                return;
            }

            RunServer(commandLineParser.Object);
        }