@@ -21,157 +21,47 @@ A simple, light-weight and strongly typed commandline parser made in .Net standa
2121PM> Install-Package MatthiWare.CommandLineParser
2222```
2323
24- ## Configuration
24+ # Quick Start
2525
26- #### Using model class with attributes
27- ``` csharp
28- using MatthiWare .CommandLine ;
29- using MatthiWare .CommandLine .Core .Attributes ;
30-
31- static int Main (string [] args )
32- {
33- var parser = new CommandLineParser <OptionsModel >();
34-
35- var parseResult = parser .Parse (args );
36-
37- if (result .HasErrors )
38- {
39- Console .Error .WriteLine (result .Error );
40- Console .ReadKey ();
41-
42- return - 1 ;
43- }
44-
45- var options = result .Result ;
46-
47- Console .WriteLine ($" Message: {options .Message }" );
48- Console .WriteLine ($" Port: {options .Port }" );
49-
50- Console .ReadKey ();
51-
52- return 0 ;
53- }
54-
55- public class OptionsModel
56- {
57- [Required , Name (" -m" , " --message" )]
58- public string Message { get ; set ; } // Mandatory
59-
60- [Name (" -p" , " --port" ), DefaultValue (8080 )]
61- public int Port { get ; set ; } // Optional
26+ First of all you need to add the nuget package.
6227
63- [Ignored ]
64- public string Metadata { get ; set ; } // Property will not be added
65- }
28+ ``` powershell
29+ PM> Install-Package MatthiWare.CommandLineParser
6630```
6731
68- _ ** Warning: ** Attributes will be overwritten by fluent api if both are configured _
32+ Now you can setup the command line parser.
6933
70- #### Using model class and Fluent API
7134``` csharp
72- using MatthiWare .CommandLine ;
73-
74- static int Main (string [] args )
75- {
76- var parser = new CommandLineParser <OptionsModel >();
77-
78- parser .Configure (opt => opt .Message )
79- .Name (" -m" , " --message" )
80- .Required ();
81-
82- parser .Configure (opt => opt .Port )
83- .Name (" -p" , " --port" )
84- .Default (8080 );
85-
86- var parseResult = parser .Parse (args );
87-
88- if (result .HasErrors )
89- {
90- Console .Error .WriteLine (result .Error );
91- Console .ReadKey ();
92-
93- return - 1 ;
94- }
95-
96- var options = result .Result ;
97-
98- Console .WriteLine ($" Message: {options .Message }" );
99- Console .WriteLine ($" Port: {options .Port }" );
100-
101- Console .ReadKey ();
102-
103- return 0 ;
104- }
105-
106- public class OptionsModel
107- {
108- public string Message { get ; set ; } // Mandatory
109-
110- public int Port { get ; set ; } // Optional
111- }
35+ static void Main (string [] args )
36+ {
37+ // create the parser
38+ var parser = new CommandLineParser <ServerOptions >();
39+
40+ // configure the options using the fluent api
41+ parser .Configure (options => options .Port )
42+ .Name (" p" , " port" )
43+ .Description (" The port of the server" )
44+ .Required ();
45+
46+ // parse
47+ var result = parser .Parse (args );
48+
49+ // check for parsing errors
50+ if (result .HasErrors )
51+ {
52+ Console .ReadKey ();
53+
54+ return - 1 ;
55+ }
56+
57+ Console .WriteLine ($" Parsed port is {result .Result .Port }" );
58+ }
11259```
11360
114- _ ** Warning:** Attributes will be overwritten by fluent api if both are configured_
115-
116- ### Commands
117- ``` csharp
118- using MatthiWare .CommandLine ;
119-
120- static int Main (string [] args )
121- {
122- var parser = new CommandLineParser <OptionsModel >();
123-
124- parser .Configure (opt => opt .Message )
125- .Name (" -m" , " --message" )
126- .Required ();
127-
128- parser .Configure (opt => opt .Port )
129- .Name (" -p" , " --port" )
130- .Default (8080 );
131-
132- var parseResult = parser .Parse (args );
133-
134- var startCmd = parser .AddCommand <CommandOptions >()
135- .Name (" -s" , " --start" )
136- .Required ()
137- .OnExecuting (parsedCmdOption => Console .WriteLine ($" Starting server using verbose option: {parsedCmdOption .Verbose }" ));
138-
139- startCmd .Configure (cmd => cmd .Verbose ) // configures the command options can also be done using attributes
140- .Required ()
141- .Name (" -v" , " --verbose" );
61+ Run command line
14262
143- if (result .HasErrors )
144- {
145- Console .Error .WriteLine (result .Error );
146- Console .ReadKey ();
147-
148- return - 1 ;
149- }
150-
151- foreach (var cmdResult in result .CommandResults )
152- {
153- cmdResult .ExecuteCommand (); // executes the command handler that is configured above.
154- }
155-
156- var options = result .Result ;
157-
158- Console .WriteLine ($" Message: {options .Message }" );
159- Console .WriteLine ($" Port: {options .Port }" );
160-
161- Console .ReadKey ();
162-
163- return 0 ;
164- }
165-
166- public class OptionsModel
167- {
168- public string Message { get ; set ; } // Mandatory
169-
170- public int Port { get ; set ; } // Optional
171- }
172-
173- public class CommandOptions
174- {
175- public bool Verbose { get ; set ; }
176- }
63+ ``` shell
64+ dotnet myapp --port 2551
17765```
66+
67+ ### For more advanced configuration options see [ the wiki] ( https://github.com/MatthiWare/CommandLineParser.Core/wiki ) .
0 commit comments