File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11using System ;
2+ using MatthiWare . CommandLine ;
23
34namespace SampleApp
45{
56 class Program
67 {
7- static void Main ( string [ ] args )
8+ static int Main ( string [ ] args )
89 {
9- foreach ( var arg in args )
10- Console . WriteLine ( arg ) ;
10+ var parser = new CommandLineParser < Options > ( ) ;
11+
12+ // setup
13+ parser . Configure ( opt => opt . MyInt )
14+ . ShortName ( "-i" )
15+ . LongName ( "--int" )
16+ . Required ( ) ;
17+
18+ parser . Configure ( opt => opt . MyString )
19+ . ShortName ( "-s" )
20+ . LongName ( "--string" )
21+ . Required ( ) ;
22+
23+ parser . Configure ( opt => opt . MyBool )
24+ . ShortName ( "-b" )
25+ . LongName ( "--bool" )
26+ . Required ( ) ;
27+
28+ parser . Configure ( opt => opt . MyDouble )
29+ . ShortName ( "-d" )
30+ . LongName ( "--double" )
31+ . Required ( ) ;
32+
33+ var result = parser . Parse ( args ) ;
34+
35+ if ( result . HasErrors )
36+ {
37+ Console . Error . WriteLine ( result . Error ) ;
38+ Console . ReadKey ( ) ;
39+
40+ return - 1 ;
41+ }
42+
43+ var options = result . Result ;
44+
45+ Console . WriteLine ( $ "MyInt: { options . MyInt } ") ;
46+ Console . WriteLine ( $ "MyString: { options . MyString } ") ;
47+ Console . WriteLine ( $ "MyBool: { options . MyBool } ") ;
48+ Console . WriteLine ( $ "MyDouble: { options . MyDouble } ") ;
1149
1250 Console . ReadKey ( ) ;
51+
52+ return 0 ;
53+ }
54+
55+ private class Options
56+ {
57+ public int MyInt { get ; set ; }
58+ public string MyString { get ; set ; }
59+ public bool MyBool { get ; set ; }
60+ public double MyDouble { get ; set ; }
1361 }
1462 }
1563}
Original file line number Diff line number Diff line change 55 <TargetFramework >netcoreapp2.1</TargetFramework >
66 </PropertyGroup >
77
8+ <ItemGroup >
9+ <ProjectReference Include =" ..\CommandLineParser\CommandLineParser.csproj" />
10+ </ItemGroup >
11+
812</Project >
You can’t perform that action at this time.
0 commit comments