You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[System.CommandLine](https://github.com/dotnet/command-line-api) is a really handy .Net library for parsing command line arguments passed to an application. Unfortunately some useful features available in its beta stage were removed from the library's release candidates. CommandLineX brings back some of them: **hosting extensions** and **arguments model binding** including DI.
5
+
6
+
## Getting Started
7
+
### Creating Console App
8
+
1. Open terminal in the directory where your project usually are located.
9
+
1. Execute
10
+
```sh
11
+
dotnet new console -n MyConsoleApp --no-restore
12
+
```
13
+
1. Navigate to the project directory.
14
+
1. Open `MyConsoleApp.csproj` in XML editor of your choice.
15
+
1. Change project SDK by replacing `<Project Sdk="Microsoft.NET.Sdk">` with `<Project Sdk="Microsoft.NET.Sdk.Worker">` (needed for hosting integration).
16
+
17
+
### Installing The Library
18
+
In the directory of your project execute
19
+
```sh
20
+
dotnet add package diVISION.CommandLineX
21
+
```
22
+
23
+
### Integrating The Library In Your Application
24
+
1. Create `MyFirstAction.cs` (command action model) in your project directory:
Execute one of the following in your project directory
91
+
```sh
92
+
dotnet run -- myFirst 42
93
+
```
94
+
```sh
95
+
dotnet run -- myFirst 42 43
96
+
```
97
+
```sh
98
+
dotnet run -- myFirst 42 43 -d someOtherDirectory
99
+
```
100
+
You can also request help on commands like
101
+
```sh
102
+
dotnet run -- -?
103
+
```
104
+
```sh
105
+
dotnet run -- myFirst -?
106
+
```
107
+
108
+
## Building
109
+
For building the application .NET SDK 8.x is required (recommended: Visual Studio or Visual Studio Code).
110
+
111
+
After cloning the repository you can either open the solution `CommandLineX.sln` in your IDE and hit "Build" or open the terminal in the solution directory and execute
112
+
```sh
113
+
dotnet build
114
+
```
115
+
116
+
## Contributing
117
+
All contributions to development and error fixing are welcome. Please always use `develop` branch for forks and pull requests, `main` is reserved for stable releases and critical vulnarability fixes only. Please note: all code changes should meet minimal code coverage requirements to be merged into `main` or `develop`.
@@ -47,6 +67,19 @@ public static IHostBuilder UseHostedCommandInvocation(this IHostBuilder builder,
47
67
returnbuilder;
48
68
}
49
69
70
+
/// <summary>
71
+
/// Adds specified <paramref name="command"/> to <paramref name="parent"/> command (ususally <c>RootCommand</c>) and binds <typeparamref name="TAction"/> model to the former.
72
+
/// Requires comand line services to be set up by <c cref="UseCommandLine(IHostBuilder, RootCommand, Action{CommandLineOptions}?)">UseCommandLine</c> prior to this call.
/// or <typeparamref name="TAction"/>.<c cref="ICommandAction.InvokeAsync(CommandActionContext, CancellationToken)">InvokeAsync</c> is used to execute the action</param>
@@ -90,6 +123,14 @@ public static IHostBuilder UseCommandWithAction<TAction>(this IHostBuilder build
90
123
returnbuilder;
91
124
}
92
125
126
+
/// <summary>
127
+
/// Starts the <paramref name="host"/>, parses command line <paramref name="args"/> and executes matching <c cref="ICommandAction.Invoke(CommandActionContext)">ICommandAction.Invoke</c>
128
+
/// previously bound by <c cref="UseCommandWithAction{TAction}(IHostBuilder, Command, Command, bool)">UseCommandWithAction</c>. If no match exists an error message is displayed and an error code is returned.
@@ -98,6 +139,15 @@ public static int RunCommandLine(this IHost host, string[] args)
98
139
returninvoker.Invoke(args);
99
140
}
100
141
142
+
/// <summary>
143
+
/// Starts the <paramref name="host"/>, parses command line <paramref name="args"/> and executes matching <c cref="ICommandAction.InvokeAsync(CommandActionContext, CancellationToken)">ICommandAction.InvokeAsync</c>
144
+
/// previously bound by <c cref="UseCommandWithAction{TAction}(IHostBuilder, Command, Command, bool)">UseCommandWithAction</c>. If no match exists an error message is displayed and an error code is returned.
/// <param name="host">host instance being extended</param>
148
+
/// <param name="args">command line arguments to parse</param>
149
+
/// <param name="cancellationToken">cancellation token passed to <c cref="ICommandAction.InvokeAsync(CommandActionContext, CancellationToken)">ICommandAction.InvokeAsync</c></param>
150
+
/// <returns>either result of <c cref="ICommandAction.InvokeAsync(CommandActionContext, CancellationToken)">ICommandAction.InvokeAsync</c> or an error code</returns>
/// Starts the <paramref name="host"/>, during the startup the <c cref="CommandLineHostedService">CommandLineHostedService</c>
161
+
/// (registered by <c cref="UseHostedCommandInvocation(IHostBuilder, string[])">UseHostedCommandInvocation</c>)
162
+
/// parses the command line and executes matching <c cref="ICommandAction.Invoke(CommandActionContext)">ICommandAction.Invoke</c>
163
+
/// previously bound by <c cref="UseCommandWithAction{TAction}(IHostBuilder, Command, Command, bool)">UseCommandWithAction</c>. If no match exists an error message is displayed and an error code is returned.
@@ -119,6 +178,16 @@ public static int RunCommandLineHosted(this IHost host)
119
178
returnEnvironment.ExitCode;
120
179
}
121
180
181
+
/// <summary>
182
+
/// Starts the <paramref name="host"/>, during the startup the <c cref="CommandLineHostedService">CommandLineHostedService</c>
183
+
/// (registered by <c cref="UseHostedCommandInvocation(IHostBuilder, string[])">UseHostedCommandInvocation</c>)
184
+
/// parses the command line and executes matching <c cref="ICommandAction.InvokeAsync(CommandActionContext, CancellationToken)">ICommandAction.InvokeAsync</c>
185
+
/// previously bound by <c cref="UseCommandWithAction{TAction}(IHostBuilder, Command, Command, bool)">UseCommandWithAction</c>. If no match exists an error message is displayed and an error code is returned.
/// <param name="host">host instance being extended</param>
189
+
/// <param name="cancellationToken">cancellation token passed to the <paramref name="host"/></param>
190
+
/// <returns>either result of <c cref="ICommandAction.InvokeAsync(CommandActionContext, CancellationToken)">ICommandAction.InvokeAsync</c> or an error code</returns>
0 commit comments