Beispiel #1
0
        public override IParsingResult <ExclusiveFileOptionResult> Parse(SuccessfulResultCollection parseInput)
        {
            var result1 = parseInput.Get(_option1);
            var result2 = parseInput.Get(_option2);

            if (result1 != null && result2 != null)
            {
                return(ParsingResult <ExclusiveFileOptionResult> .FailedResult($"Please specify either option {_option1.Info.Name} or option {_option2.Info.Name}, but not both"));
            }
            if (_isRequired && result1 == null && result2 == null)
            {
                return(ParsingResult <ExclusiveFileOptionResult> .FailedResult($"Either option {_option1.Info.Name} or option {_option2.Info.Name} must be specified"));
            }
            IFileLocation result = null;
            FileOption    option = null;

            if (result1 != null)
            {
                result = result1;
                option = _option1;
            }
            if (result2 != null)
            {
                result = result2;
                option = _option2;
            }
            return(ParsingResult <ExclusiveFileOptionResult> .SuccessfulResult(new ExclusiveFileOptionResult(result, option)));
        }
Beispiel #2
0
        public override IParsingResult <List <TOut> > Parse(SuccessfulResultCollection input)
        {
            var multipleValues = input.Get(Info);
            var outputs        = new List <TOut>();

            foreach (var values in multipleValues)
            {
                var option1Result = GetOptionResult(0, values, _option1);
                var option2Result = GetOptionResult(1, values, _option2);
                var option3Result = GetOptionResult(2, values, _option3);
                if (!option1Result.Success)
                {
                    return(ParsingResult <List <TOut> > .FailedResult(option1Result.ErrorMessage));
                }
                if (!option2Result.Success)
                {
                    return(ParsingResult <List <TOut> > .FailedResult(option2Result.ErrorMessage));
                }
                if (!option3Result.Success)
                {
                    return(ParsingResult <List <TOut> > .FailedResult(option3Result.ErrorMessage));
                }
                var result = _parse(option1Result.Result, option2Result.Result, option3Result.Result);
                if (!result.Success)
                {
                    return(ParsingResult <List <TOut> > .FailedResult(result.ErrorMessage));
                }
                outputs.Add(result.Result);
            }
            if (_required && outputs.Empty())
            {
                return(ParsingResult <List <TOut> > .FailedResult($"{Info.Name} is a required option"));
            }
            return(ParsingResult <List <TOut> > .SuccessfulResult(outputs));
        }
Beispiel #3
0
        public IParsingResult <IModeLauncher> Parse(
            MainParser main, ILogger logger, ISettings settings, ICheckpointRunner checkpointRunner,
            IWorkDoer workDoer, string[] args, TextWriter standardWriter, TextWriter errorWriter)
        {
            var results = main.GetParseResults(args);

            var parsingResult = checkpointRunner.RunCheckpoint("Validate input", () =>
            {
                if (!results.Validate(out IParsingResult <IModeLauncher> failedResult))
                {
                    return(ParsingResult <T> .FailedResult(failedResult.ErrorMessage));
                }
                var successfulResults = new SuccessfulResultCollection(results);
                var commonOptions     = successfulResults.Get(MainParser.CommonOptionsParser);
                return(GetSerializedResult(successfulResults, commonOptions));
            });

            if (!parsingResult.Success)
            {
                ShowError(main, logger.Error, parsingResult.ErrorMessage);
                return(ParsingResult <IModeLauncher> .FailedResult(parsingResult));
            }
            var runner = GetRunner(parsingResult.Result);

            return(ParsingResult <IModeLauncher> .SuccessfulResult(new ModeLauncher(logger, settings, checkpointRunner, workDoer, runner, args, main.GetVersion(), Name)));
        }
        public override ParsingResult <SomaticEnrichmentOptions> Parse(SuccessfulResultCollection parseInput)
        {
            var bam            = parseInput.Get(Bam);
            var manifest       = parseInput.Get(Manifest);
            var controlBams    = parseInput.Get(ControlBams);
            var controlBinned  = parseInput.Get(ControlBinned);
            var controlBinSize = parseInput.Get(ControlBinSize);
            var controlPloidy  = parseInput.Get(ControlPloidyBed);

            var controlBinnedBools = new List <bool> {
                controlBinned != null, controlBinSize != null, controlPloidy != null
            };

            if (controlBams.Any() && controlBinnedBools.Any(controlBinnedBool => controlBinnedBool))
            {
                return(ParsingResult <SomaticEnrichmentOptions> .FailedResult($"Error: option {ControlBams.Info.Name} cannot be combined with any of {ControlBinned.Info.Name}, {ControlBinSize.Info.Name}, {ControlPloidyBed.Info.Name} "));
            }

            if (controlBinned != null && controlBinSize == null)
            {
                return(ParsingResult <SomaticEnrichmentOptions> .FailedResult($"Error: {ControlBinSize.Info.Name} is required when using the {ControlBinned.Info.Name} option"));
            }

            return(ParsingResult <SomaticEnrichmentOptions> .SuccesfulResult(
                       new SomaticEnrichmentOptions(
                           bam,
                           manifest,
                           controlBams,
                           controlBinned,
                           controlBinSize.HasValue ? (int)controlBinSize.Value : 0,
                           controlPloidy)));
        }
Beispiel #5
0
        public void CanSerializeFailedParsingResult()
        {
            var parsingResult = ParsingResult <SmallPedigreeInput> .FailedResult("error");

            var serializedResult = _tester.RoundTrip(parsingResult);

            Assert.Equal("error", serializedResult.ErrorMessage);
        }
Beispiel #6
0
        public override IParsingResult <TOut> Parse(SuccessfulResultCollection parseInput)
        {
            var multipleValues = parseInput.Get(PositionalOption);

            if (multipleValues.Count > 1)
            {
                return(ParsingResult <TOut> .FailedResult($"{PositionalOption.Info.Name} can be specified only once"));
            }
            return(ParsingResult <TOut> .SuccessfulResult(multipleValues.SingleOrDefault()));
        }
Beispiel #7
0
        private IParsingResult <T> GetOptionResult <T>(int valueIndex, List <string> values, ValueOption <T> option1)
        {
            string value = valueIndex < values.Count ? values[valueIndex] : null;

            if (option1.Info is RequiredValueOptionInfo && value == null)
            {
                return(ParsingResult <T> .FailedResult($"{option1.Info.Name} is a required positional argument for option {Info.Name}"));
            }
            return(option1.Parse(value));
        }
        public override ParsingResult <string> Parse(SuccessfulResultCollection parseInput)
        {
            string value = parseInput.Get(this);

            if (value == null)
            {
                return(ParsingResult <string> .FailedResult($"Error: {Name} is a required option"));
            }
            return(ParsingResult <string> .SuccessfulResult(value));
        }
Beispiel #9
0
        private static IParsingResult GetParseResult(OptionInfo <string> optionInfo, OptionData optionData)
        {
            ParsingResult <string> result = ParsingResult <string> .SuccesfulResult(optionData.Data.FirstOrDefault());

            if (optionData.Data.Count > 1)
            {
                result = ParsingResult <string> .FailedResult($"Error: {optionInfo.Name} can only be specified once");
            }
            return(optionInfo.Parse(new SuccessfulResultCollection(optionInfo, result)));
        }
Beispiel #10
0
        public override IParsingResult <IDirectoryLocation> Parse(string value)
        {
            IDirectoryLocation location = value == null ? null : new DirectoryLocation(value);

            if (location == null || location.Exists)
            {
                return(ParsingResult <IDirectoryLocation> .SuccessfulResult(location));
            }
            return(ParsingResult <IDirectoryLocation> .FailedResult($"Error: {location} does not exist"));
        }
Beispiel #11
0
        public override ParsingResult <List <string> > Parse(SuccessfulResultCollection parseInput)
        {
            List <string> value = parseInput.Get(this);

            if (!value.Any())
            {
                return(ParsingResult <List <string> > .FailedResult($"Error: {Name} is a required option"));
            }
            return(ParsingResult <List <string> > .SuccessfulResult(value));
        }
Beispiel #12
0
        public override IParsingResult <IDirectoryLocation> Parse(string value)
        {
            IDirectoryLocation location = value == null ? null : new DirectoryLocation(value);

            if (location != null && !ActionExtensions.Try(() => location.Create(), out Exception e))
            {
                return(ParsingResult <IDirectoryLocation> .FailedResult($"Error: failed to create directory {location}. Exception: {e}"));
            }
            return(ParsingResult <IDirectoryLocation> .SuccessfulResult(location));
        }
Beispiel #13
0
        public ResultCollection <TResult> Parse(IEnumerable <string> args)
        {
            Dictionary <IOption, OptionData> optionResults;
            OptionSet options = GetOptionSet(out optionResults);

            try
            {
                IEnumerable <string> remainingArgs = options.Parse(args);
                return(ParseGeneric(optionResults, remainingArgs));
            }
            catch (OptionException e)
            {
                return(new ResultCollection <TResult>(ParsingResult <TResult> .FailedResult(e.Message)));
            }
        }
Beispiel #14
0
 public virtual IParsingResult <T> Parse(string value)
 {
     if (value == null)
     {
         return(ParsingResult <T> .SuccessfulResult(_defaultValue));
     }
     try
     {
         T parsedValue = ConvertInternal(value);
         return(ParsingResult <T> .SuccessfulResult(parsedValue));
     }
     catch (Exception)
     {
         return(ParsingResult <T> .FailedResult($"Error parsing {Info.Name} option: failed to convert {value} to {typeof(T)}"));
     }
 }
        private bool HasMoreThanOne(List <SmallPedigreeSampleOptions> bams, SampleType sampleType, out IParsingResult <SmallPedigreeOptions> failedResult)
        {
            failedResult = null;
            var sameType = bams.Where(bam => bam.SampleType == sampleType);

            if (sameType.Count() <= 1)
            {
                return(false);
            }

            var bamsSameType = string.Join(",", sameType.Select(bam => $"'{bam.Bam}'"));

            failedResult = ParsingResult <SmallPedigreeOptions> .FailedResult($"Pedigree can have at most one sample of type '{sampleType}'. Samples with these bams have the same type: {bamsSameType}");

            return(true);
        }
Beispiel #16
0
        public override ParsingResult <List <T> > Parse(SuccessfulResultCollection input)
        {
            var values  = input.Get(Info);
            var results = new List <T>();

            foreach (var value in values)
            {
                var result = _valueOption.Parse(value);
                if (!result.Success)
                {
                    return(ParsingResult <List <T> > .FailedResult(result.ErrorMessage));
                }
                results.Add(result.Result);
            }
            return(ParsingResult <List <T> > .SuccesfulResult(results));
        }
Beispiel #17
0
 public virtual ParsingResult <T> Parse(string value)
 {
     if (value == null)
     {
         return(ParsingResult <T> .SuccessfulResult(_defaultValue));
     }
     try
     {
         T parsedValue = (T)Convert.ChangeType(value, GetUnderlyingType(typeof(T)));
         return(ParsingResult <T> .SuccessfulResult(parsedValue));
     }
     catch (Exception)
     {
         return(ParsingResult <T> .FailedResult($"Error parsing {Info.Name} option: failed to convert {value} to {typeof(T)}"));
     }
 }
 private static IParsingResult <SmallPedigreeSampleOptions> Parse(IFileLocation bam, SampleType sampleType, string sampleName)
 {
     if (sampleName == null)
     {
         Action a = () =>
         {
             BamReader.WrapException(bam, reader =>
             {
                 sampleName = reader.GetReadGroupSample();
             });
         };
         if (!a.Try(out Exception e))
         {
             return(ParsingResult <SmallPedigreeSampleOptions> .FailedResult(e.Message));
         }
     }
     return(ParsingResult <SmallPedigreeSampleOptions> .SuccessfulResult(new SmallPedigreeSampleOptions(sampleName, sampleType, bam)));
 }
Beispiel #19
0
        private ParsingResult <IModeLauncher> GetMissingModeResult(IEnumerable <string> args, TextWriter standardWriter, TextWriter errorWriter)
        {
            string error             = "Error: no mode specified";
            var    baseOptionsResult = BaseOptionsParser.Parse(args);

            if (baseOptionsResult.Success)
            {
                if (HandleBaseOptions(baseOptionsResult.Result, standardWriter))
                {
                    return(ParsingResult <IModeLauncher> .SuccessfulResult(new NullModeLauncher()));
                }
            }
            else
            {
                error = baseOptionsResult.ErrorMessage;
            }

            errorWriter.WriteLine(error);
            errorWriter.WriteLine();
            ShowHelp(errorWriter);
            return(ParsingResult <IModeLauncher> .FailedResult(error));
        }
Beispiel #20
0
        public override ParsingResult <Dictionary <string, string> > Parse(SuccessfulResultCollection parseInput)
        {
            List <string> inputs = parseInput.Get(_option);
            var           result = new Dictionary <string, string>();

            foreach (var input in inputs)
            {
                string[] split = input.Split(_separator).Select(untrimmed => untrimmed.Trim()).ToArray();
                if (split.Length != 2)
                {
                    return(ParsingResult <Dictionary <string, string> > .FailedResult($"Error: expected format is {{KEY}}{_separator}{{value}}. Input was {input}"));
                }
                string key   = split[0];
                string value = split[1];
                if (result.ContainsKey(key))
                {
                    return(ParsingResult <Dictionary <string, string> > .FailedResult($"Error parsing {_option.Info.Name}: found duplicate key {key}"));
                }
                result.Add(key, value);
            }
            return(ParsingResult <Dictionary <string, string> > .SuccesfulResult(result));
        }