@@ -20,34 +20,36 @@ public static int Main(string[] args)
2020 : ReadBytes ( opts . FileName , fromTop , ( int ) opts . Bytes ) ;
2121 } ;
2222 Func < IOptions , string > header = opts =>
23- {
24- if ( opts . Quiet )
2523 {
26- return string . Empty ;
27- }
28- var fromTop = opts . GetType ( ) == typeof ( HeadOptions ) ;
29- var builder = new StringBuilder ( "Reading " ) ;
30- builder = opts . Lines . HasValue
31- ? builder . Append ( opts . Lines ) . Append ( " lines" )
32- : builder . Append ( opts . Bytes ) . Append ( " bytes" ) ;
33- builder = fromTop ? builder . Append ( " from top" ) : builder . Append ( " from bottom:" ) ;
34- return builder . ToString ( ) ;
35- } ;
24+ if ( opts . Quiet )
25+ {
26+ return string . Empty ;
27+ }
28+ var fromTop = opts . GetType ( ) == typeof ( HeadOptions ) ;
29+ var builder = new StringBuilder ( "Reading " ) ;
30+ builder = opts . Lines . HasValue
31+ ? builder . Append ( opts . Lines ) . Append ( " lines" )
32+ : builder . Append ( opts . Bytes ) . Append ( " bytes" ) ;
33+ builder = fromTop ? builder . Append ( " from top:" ) : builder . Append ( " from bottom:" ) ;
34+ return builder . ToString ( ) ;
35+ } ;
36+ Action < string > printIfNotEmpty = text =>
37+ {
38+ if ( text . Length == 0 ) { return ; }
39+ Console . WriteLine ( text ) ;
40+ } ;
3641
3742 var result = Parser . Default . ParseArguments < HeadOptions , TailOptions > ( args ) ;
3843 var texts = result
3944 . Return (
4045 ( HeadOptions opts ) => Tuple . Create ( header ( opts ) , reader ( opts ) ) ,
4146 ( TailOptions opts ) => Tuple . Create ( header ( opts ) , reader ( opts ) ) ,
42- _ => Tuple . Create ( string . Empty , string . Empty ) ) ;
47+ _ => MakeError ( ) ) ;
4348
44- if ( texts . Item1 . Length > 0 )
45- {
46- Console . WriteLine ( texts . Item1 ) ;
47- }
48- Console . WriteLine ( texts . Item2 ) ;
49+ printIfNotEmpty ( texts . Item1 ) ;
50+ printIfNotEmpty ( texts . Item2 ) ;
4951
50- return 0 ;
52+ return texts . Equals ( MakeError ( ) ) ? 1 : 0 ;
5153 }
5254
5355 private static string ReadLines ( string fileName , bool fromTop , int count )
@@ -69,5 +71,10 @@ private static string ReadBytes(string fileName, bool fromTop, int count)
6971 }
7072 return Encoding . UTF8 . GetString ( bytes , bytes . Length - count , count ) ;
7173 }
74+
75+ private static Tuple < string , string > MakeError ( )
76+ {
77+ return Tuple . Create ( "\0 " , "\0 " ) ;
78+ }
7279 }
7380}
0 commit comments