Ejemplo n.º 1
0
        private static bool ParseArguments(IEnumerable <string> args, out bool isVerbError)
        {
            bool noVerb = false;

            using (Parser parser = new Parser(cfg =>
            {
                cfg.CaseInsensitiveEnumValues = true;
                cfg.HelpWriter = Console.Out;
            }))
            {
                bool success = true;
                parser.ParseArguments <WatcherOptions, CheckUpdatesOption>(args)
                .WithNotParsed(e =>
                {
                    noVerb  = e.Any(err => err.Tag == ErrorType.NoVerbSelectedError || err.Tag == ErrorType.BadVerbSelectedError);
                    success = false;
                })
                .WithParsed <CheckUpdatesOption>(o => checkUpdates = true)
                .WithParsed <WatcherOptions>(o =>
                {
                    options = o;

                    if (o.LeftX > o.RightX || o.TopY > o.BottomY)
                    {
                        Console.WriteLine("Invalid args: check rectangle borders");
                        success = false;
                        return;
                    }

                    if (o.UseMirror)
                    {
                        UrlManager.MirrorMode = o.UseMirror;
                    }
                    if (o.ServerUrl != null)
                    {
                        UrlManager.BaseUrl = o.ServerUrl;
                    }
                });
                isVerbError = noVerb;
                return(success);
            }
        }
Ejemplo n.º 2
0
        private static bool ParseArguments(IEnumerable <string> args)
        {
            bool ProcessAppOptions(AppOptions o)
            {
                if (!string.IsNullOrWhiteSpace(o.ProxyAddress))
                {
                    int protocolLength = o.ProxyAddress.IndexOf("://");
                    if (!o.ProxyAddress.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (protocolLength > -1)
                        {
                            o.ProxyAddress = "http" + o.ProxyAddress.Substring(protocolLength);
                        }
                        else
                        {
                            o.ProxyAddress = "http://" + o.ProxyAddress;
                        }
                    }
                    if (!Uri.IsWellFormedUriString(o.ProxyAddress, UriKind.Absolute))
                    {
                        Console.WriteLine("Invalid proxy address");
                        return(false);
                    }

                    proxySettings = new ProxySettings
                    {
                        Address  = o.ProxyAddress,
                        Username = o.ProxyUsername,
                        Password = o.ProxyPassword
                    };
                }
                if (o.UseMirror)
                {
                    UrlManager.MirrorMode = o.UseMirror;
                }
                if (o.ServerUrl != null)
                {
                    UrlManager.BaseUrl = o.ServerUrl;
                }
                return(true);
            }

            using (Parser parser = new Parser(cfg =>
            {
                cfg.CaseInsensitiveEnumValues = true;
                cfg.HelpWriter = Console.Out;
            }))
            {
                bool success = true;
                parser.ParseArguments <WatcherOptions, CheckUpdatesOption>(args)
                .WithNotParsed(e => success = false)
                .WithParsed <CheckUpdatesOption>(o => checkUpdates = true)
                .WithParsed <WatcherOptions>(o =>
                {
                    if (!ProcessAppOptions(o))
                    {
                        success = false;
                        return;
                    }
                    options = o;
                    if (o.LeftX > o.RightX || o.TopY > o.BottomY)
                    {
                        Console.WriteLine("Invalid args: check rectangle borders");
                        success = false;
                        return;
                    }
                });
                return(success);
            }
        }