@@ -581,54 +581,12 @@ private static void RegularParametersParsing(string[] args)
581581
582582 if ( outputFormatString != null )
583583 {
584- int outPutFormatInt ;
585- try
586- {
587- outPutFormatInt = int . Parse ( outputFormatString ) ;
588- }
589- catch ( FormatException )
590- {
591- throw new OptionException (
592- "unknown output format value (0 for MGF, 1 for mzML, 2 for indexed mzML, 3 for Parquet)" ,
593- "-f, --format" ) ;
594- }
595-
596- if ( Enum . IsDefined ( typeof ( OutputFormat ) , outPutFormatInt ) &&
597- ( ( OutputFormat ) outPutFormatInt ) != OutputFormat . NONE )
598- {
599- parseInput . OutputFormat = ( OutputFormat ) outPutFormatInt ;
600- }
601- else
602- {
603- throw new OptionException (
604- "unknown output format value (0 for MGF, 1 for mzML, 2 for indexed mzML, 3 for Parquet)" ,
605- "-f, --format" ) ;
606- }
584+ parseInput . OutputFormat = ( OutputFormat ) ParseToEnum ( typeof ( OutputFormat ) , outputFormatString , "-f, --format" ) ;
607585 }
608586
609587 if ( metadataFormatString != null )
610588 {
611- int metadataInt ;
612- try
613- {
614- metadataInt = int . Parse ( metadataFormatString ) ;
615- }
616- catch ( FormatException )
617- {
618- throw new OptionException ( "unknown metadata format value (0 for JSON, 1 for TXT)" ,
619- "-m, --metadata" ) ;
620- }
621-
622- if ( Enum . IsDefined ( typeof ( MetadataFormat ) , metadataInt ) &&
623- ( ( MetadataFormat ) metadataInt ) != MetadataFormat . NONE )
624- {
625- parseInput . MetadataFormat = ( MetadataFormat ) metadataInt ;
626- }
627- else
628- {
629- throw new OptionException ( "unknown metadata format value (0 for JSON, 1 for TXT)" ,
630- "-m, --metadata" ) ;
631- }
589+ parseInput . MetadataFormat = ( MetadataFormat ) ParseToEnum ( typeof ( MetadataFormat ) , metadataFormatString , "-m, --metadata" ) ;
632590 }
633591
634592 if ( parseInput . MetadataOutputFile != null && Directory . Exists ( parseInput . MetadataOutputFile ) )
@@ -653,29 +611,7 @@ private static void RegularParametersParsing(string[] args)
653611
654612 if ( logFormatString != null )
655613 {
656- int logFormatInt ;
657- try
658- {
659- logFormatInt = int . Parse ( logFormatString ) ;
660- }
661- catch ( FormatException )
662- {
663- throw new OptionException ( "unknown log format value (0 for silent, 1 for verbose)" ,
664- "-l, --logging" ) ;
665- }
666-
667- if ( Enum . IsDefined ( typeof ( LogFormat ) , logFormatInt ) )
668- {
669- if ( ( LogFormat ) logFormatInt != LogFormat . NONE )
670- {
671- parseInput . LogFormat = ( LogFormat ) logFormatInt ;
672- }
673- }
674- else
675- {
676- throw new OptionException ( "unknown log format value (0 for silent, 1 for verbose)" ,
677- "-l, --logging" ) ;
678- }
614+ parseInput . LogFormat = ( LogFormat ) ParseToEnum ( typeof ( LogFormat ) , logFormatString , "-l, --logging" ) ;
679615 }
680616
681617 if ( parseInput . StdOut )
@@ -789,6 +725,49 @@ private static void ShowHelp(string message, OptionException optionException, Op
789725 Environment . Exit ( - 1 ) ;
790726 }
791727
728+ private static string GetValidEnumLevels ( Type enumType )
729+ {
730+ List < string > output = new List < string > ( ) ;
731+ foreach ( int v in Enum . GetValues ( enumType ) )
732+ {
733+ output . Add ( String . Format ( "{0} ({1})" , Enum . GetName ( enumType , v ) , v ) ) ;
734+ }
735+
736+ return String . Join ( "\n " , output ) ;
737+ }
738+
739+ private static int ParseToEnum ( Type enumType , string formatString , string keyName )
740+ {
741+ if ( int . TryParse ( formatString , out var formatInt ) ) //can be parsed as int
742+ {
743+ if ( Enum . IsDefined ( enumType , formatInt ) )
744+ {
745+ return formatInt ;
746+ }
747+ else
748+ {
749+ throw new OptionException (
750+ String . Format ( "unknown format value, the following values recognized (case insensitive)\n {0}" , GetValidEnumLevels ( enumType ) ) ,
751+ keyName ) ;
752+ }
753+
754+ }
755+ else //try parse as a string
756+ {
757+ try
758+ {
759+ return ( int ) Enum . Parse ( enumType , formatString , true ) ;
760+ }
761+
762+ catch ( Exception )
763+ {
764+ throw new OptionException (
765+ String . Format ( "unknown format value, the following values recognized (case insensitive)\n {0}" , GetValidEnumLevels ( enumType ) ) ,
766+ keyName ) ;
767+ }
768+ }
769+ }
770+
792771 private static HashSet < int > ParseMsLevel ( string inputString )
793772 {
794773 HashSet < int > result = new HashSet < int > ( ) ;
0 commit comments