1
1
using System ;
2
+ using System . Globalization ;
3
+ using Core . Extensions . CollectionRelated ;
2
4
using Core . Extensions . TextRelated ;
5
+ using Core . Parser . Arguments ;
3
6
using Core . Text . Formatter . Impl ;
4
7
5
8
namespace DatetimeExe
@@ -8,37 +11,35 @@ internal class Program
8
11
{
9
12
private static void Main ( string [ ] args )
10
13
{
11
- var options = new CliOptions ( args ) ;
12
- var formatter = new DefaultDateTimeFormatter ( ) ;
13
-
14
- if ( options . ShowHelp )
15
- {
16
- options . WriteHelp ( Console . Out ) ;
14
+ var parser = new OptionParser < Options > ( ) ;
15
+ if ( ! parser . TryParse ( args , out var options ) )
17
16
return ;
18
- }
19
17
20
- if ( options . ShowVersion )
18
+ try
21
19
{
22
- options . WriteVersion ( Console . Out ) ;
23
- return ;
24
- }
25
20
26
- if ( options . ShowExamples )
27
- {
28
- new ExampleWriter ( formatter ) . WriteExamples ( ) ;
29
- return ;
30
- }
21
+ var provider = new CultureInfo ( options . CultureTwoLetterCode ) ;
22
+
23
+ var formatter = new DefaultDateTimeFormatter ( formatProvider : provider ) ;
24
+
25
+ if ( options . ShowExamples )
26
+ {
27
+ new ExampleWriter ( formatter ) . WriteExamples ( ) ;
28
+ return ;
29
+ }
30
+
31
+ if ( options . Extra . Count > 1 ) throw new InvalidOperationException ( $ "unexpected count of arguments: { options . Extra . ToSeparatedString ( ) } ") ;
32
+
33
+ if ( options . Extra . Count != 0 )
34
+ formatter . Format = options . Extra [ 0 ] ;
31
35
32
- try
33
- {
34
- formatter . Format = options . Format ;
35
36
formatter . UniversalTime = options . UseUtc ;
36
37
formatter . WriteLine ( Console . Out ) ;
37
38
}
38
39
catch ( Exception e )
39
40
{
40
41
Console . Error . WriteLine ( e . Message ) ;
41
- options . WriteHelp ( Console . Out ) ;
42
+ parser . WriteUsage ( ) ;
42
43
}
43
44
}
44
45
}
0 commit comments