public HotKeysBuilder WithArgs(IEnumerable <string> args)
        {
            Option hkToggleOpt   = new Option("ht", "hotkey-toggle", "Hotkey to toggle between play and pause", false, -1, 2);
            Option hkNextOpt     = new Option("hn", "hotkey-next", "Hotkey to set next Media", false, -1, 2);
            Option hkPreviousOpt = new Option("hp", "hotkey-previous", "Hotkey to set previous Media", false, -1, 2);
            Option hkPlayOpt     = Option.GetLongOnly("hotkey-play", "Hotkey to set the play state to play", false, -1, 2);
            Option hkPauseOpt    = Option.GetLongOnly("hotkey-pause", "Hotkey to set the play state to pause", false, -1, 2);
            Option hkRestartOpt  = Option.GetLongOnly("hotkey-restart", "Hotkey to restart the current media", false, -1, 2);

            Options           options = new Options(hkToggleOpt, hkNextOpt, hkPreviousOpt);
            OptionParseResult result  = options.Parse(args);

            OptionParsed p;
            HotKey       hk;

            if (result.TryGetFirstValidOptionParseds(hkToggleOpt, out p) && TryGetHotKey(p, out hk))
            {
                toggle = hk;
            }
            if (result.TryGetFirstValidOptionParseds(hkNextOpt, out p) && TryGetHotKey(p, out hk))
            {
                next = hk;
            }
            if (result.TryGetFirstValidOptionParseds(hkPreviousOpt, out p) && TryGetHotKey(p, out hk))
            {
                previous = hk;
            }
            if (result.TryGetFirstValidOptionParseds(hkPlayOpt, out p) && TryGetHotKey(p, out hk))
            {
                play = hk;
            }
            if (result.TryGetFirstValidOptionParseds(hkPauseOpt, out p) && TryGetHotKey(p, out hk))
            {
                pause = hk;
            }
            if (result.TryGetFirstValidOptionParseds(hkRestartOpt, out p) && TryGetHotKey(p, out hk))
            {
                restart = hk;
            }

            return(this);
        }
Example #2
0
 /// <summary>
 /// Initializes option Snapshot with the data from OptionParse object.
 /// </summary>
 public Snapshot(OptionParseResult optionParse, DateTime?forcedTimestamp = null)
 {
     Timestamp   = forcedTimestamp ?? DateTime.Now;
     StockStatus = optionParse.StockStatus;
     Price       = optionParse.Price;
 }
Example #3
0
        /// <summary>
        /// Parse option having crude option, string or HtmlMode. Is a variable-argument method.
        /// </summary>
        public OptionParseResult Parse(string str, HtmlNode node)
        {
            bool strFormalMode  = str != null;
            bool nodeFormalMode = node != null;

            if (strFormalMode == nodeFormalMode)
            {
                throw new InvalidOperationException("This is a two-mode method, please provide only single desired parameter.");
            }

            if (PriceAmount == null)
            {
                throw new NullReferenceException($"Price extraction is not defined.");
            }

            #region Option Info Parsing

            var newOptionParse = new OptionParseResult();

            // Details
            try
            {
                if (Code != null)
                {
                    newOptionParse.Code = Code.ExtractSingle(str, node);
                }
            }
            catch (NullReferenceException nrex) { Logger.Log(nrex.Message + " @ Option Code Parsing"); }

            try
            {
                if (PropertyA != null)
                {
                    newOptionParse.PropertyA = PropertyA.ExtractSingle(str, node);
                }
            }
            catch (NullReferenceException nrex) { Logger.Log(nrex.Message + " @ Option PropertyA Parsing"); }

            try
            {
                if (PropertyB != null)
                {
                    newOptionParse.PropertyB = PropertyB.ExtractSingle(str, node);
                }
            }
            catch (NullReferenceException nrex) { Logger.Log(nrex.Message + " @ Option PropertyB Parsing"); }

            try
            {
                if (PropertyC != null)
                {
                    newOptionParse.PropertyC = PropertyC.ExtractSingle(str, node);
                }
            }
            catch (NullReferenceException nrex) { Logger.Log(nrex.Message + " @ Option PropertyC Parsing"); }

            #endregion Option Info Parsing

            #region Snapshot Info Parsing
            var snapshot = new Snapshot(newOptionParse)
            {
                Timestamp = DateTime.Now
            };

            try
            {
                if (Stock != null)
                {
                    newOptionParse.StockStatus = Stock.ExtractSingle(str, node);
                }
            }
            catch (NullReferenceException nrex) { Logger.Log(nrex.Message + " @ Option Snapshot Stock Parsing"); }

            //Price
            try
            {
                var extracted = PriceAmount.ExtractSingle(str, node);
                decimal.TryParse(extracted, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal parsedDouble);

                newOptionParse.Price = new Money(parsedDouble, PriceCurrency);
            }
            catch (NullReferenceException nrex) { Logger.Log(nrex.Message + " @ Option Snapshot Price Parsing"); }
            #endregion Snapshot Info Parsing

            return(newOptionParse);
        }
        public ServiceBuilder WithArgs(IEnumerable <string> args)
        {
            Option clientOpt = new Option("c", "client",
                                          "Starts the app as client with the following server adresse and port", false, 2, 1);
            Option serverOpt  = new Option("s", "server", "Starts the app as server with the following port", false, 1, 1);
            Option sourcesOpt = new Option("m", "media-sources", "Files and diretories to play", false, -1, 0);
            Option ifNonOpt   = new Option("i", "if-non",
                                           "If given the Media sources are only used if there are non", false, 0, 0);
            Option reloadOpt        = new Option("r", "reload", "Forces to reload", false, 0, 0);
            Option allShuffleOpt    = Option.GetLongOnly("all-shuffle", "Shuffles all songs.", false, 0, 0);
            Option searchShuffleOpt = Option.GetLongOnly("search-shuffle", "Shuffles all songs.", false, 0, 0);
            Option onlySearchOpt    = Option.GetLongOnly("only-search", "Shuffles all songs.", false, 0, 0);
            Option searchKeyOpt     = Option.GetLongOnly("search-key", "Shuffles all songs.", false, 1, 0);
            Option playOpt          = new Option("p", "play", "Starts playback on startup", false, 0, 0);
            Option serviceVolOpt    = new Option("v", "volume", "The volume of service (value between 0 and 1)", false, 1, 1);
            Option streamingOpt     = Option.GetLongOnly("stream", "If given the audio is streamed to the client", false, 0, 0);
            Option clientVolOpt     = Option.GetLongOnly("client-volume",
                                                         "The volume of client for streaming (value between 0 and 1)", false, 1, 1);

            Options           options = new Options(sourcesOpt, ifNonOpt, reloadOpt, clientOpt, serverOpt, playOpt);
            OptionParseResult result  = options.Parse(args);

            int          port;
            float        volume;
            OptionParsed parsed;

            if (result.TryGetFirstValidOptionParseds(serverOpt, out parsed) &&
                int.TryParse(parsed.Values[0], out port))
            {
                WithServer(port);
            }

            if (result.TryGetFirstValidOptionParseds(clientOpt, out parsed))
            {
                string serverAddress = parsed.Values[0];

                if (parsed.Values.Count > 1 && int.TryParse(parsed.Values[1], out port))
                {
                    WithClient(serverAddress, port);
                }
                else
                {
                    WithClient(serverAddress);
                }
            }

            if (result.HasValidOptionParseds(sourcesOpt))
            {
                WithMediaSources(result.GetValidOptionParseds(sourcesOpt).SelectMany(p => p.Values).ToArray());
            }

            if (result.TryGetFirstValidOptionParseds(allShuffleOpt, out parsed))
            {
                WithIsAllShuffle();
            }
            if (result.TryGetFirstValidOptionParseds(searchShuffleOpt, out parsed))
            {
                WithIsSearchShuffle();
            }
            if (result.TryGetFirstValidOptionParseds(onlySearchOpt, out parsed))
            {
                WithIsOnlySearch();
            }
            if (result.TryGetFirstValidOptionParseds(searchKeyOpt, out parsed))
            {
                WithSearchKey(parsed.Values.FirstOrDefault());
            }
            if (result.HasValidOptionParseds(ifNonOpt))
            {
                WithSetMediaIfNon();
            }
            if (result.HasValidOptionParseds(reloadOpt))
            {
                WithReload();
            }
            if (result.HasValidOptionParseds(playOpt))
            {
                WithPlay();
            }

            if (result.TryGetFirstValidOptionParseds(serviceVolOpt, out parsed) &&
                float.TryParse(parsed.Values[0], out volume))
            {
                WithVolume(volume);
            }

            if (result.HasValidOptionParseds(streamingOpt))
            {
                WithIsStreaming();
            }

            if (result.TryGetFirstValidOptionParseds(clientVolOpt, out parsed) &&
                float.TryParse(parsed.Values[0], out volume))
            {
                WithClientVolume(volume);
            }

            return(this);
        }