AppCommand 1.0.2

dotnet add package AppCommand --version 1.0.2
NuGet\Install-Package AppCommand -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AppCommand" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AppCommand --version 1.0.2
#r "nuget: AppCommand, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AppCommand as a Cake Addin
#addin nuget:?package=AppCommand&version=1.0.2

// Install AppCommand as a Cake Tool
#tool nuget:?package=AppCommand&version=1.0.2

AppCommand:

Concept:

Sometimes we need to use our web application for some other purposes. Such as running our seeds, applying our pending migrations or dropping the database and etc.
We don't want to build our application from scratch or even we don't have access to source code. We also need to access our services and create instance from them.
At this situation AppCommand package can help us.
You can easily create a new CLI command and pass you parameters to it and run your application for what you want.

Commands that created with AppCommand can access to DI tree, so you can inject your services to this commands.

How to enable:

Dotnet 5 or below:

Put following lines to Main function in Program class:

CommandManager.SearchForCommands();
var host = CreateHostBuilder(args).Build();

using (var scope = host.Services.CreateScope()){
    CommandManager.SetServiceProvider(scope.ServiceProvider);
    await CommandManager.InvokeCommand(args);
}

host.Run();
Note that you change Main method to an async method.
Dotnet 6 or later:

Put following lines after var app = builder.Build(); line Program.cs in file:

CommandManager.SearchForCommands();
CommandManager.SetServiceProvider(app.Services);
CommandManager.InvokeCommand(args);

How add new command:

  1. Create a TestCommand class.
  2. Add [Command('test')] attribute to TestCommand class.
  3. Inherit TestCommand from AbstractCommand.
  4. Implement what you want in Run method.

Your class should looks like this:

[Command("test")]
public class TestCommand : AbstractCommand
{
    public ILogger<TestCommand> Logger { get; set; }

    public TestCommand(ILogger<TestCommand> logger)
    {
        /// All services you want to inject.
        Logger = logger;
    }

    public override Task Run(string[] args, CancellationToken cancellationToken = default)
    {
        // Implement what you want.
        Logger.LogInformation("Hi there!!!");
        var applicationArgs = args.Aggregate((x, y) => x += $" {y}");
        Logger.LogInformation("ApplicationArgs = {applicationArgs}", applicationArgs);
        Thread.Sleep(100);
        return Task.CompletedTask;
    }
}

How to run specific command:

You have 2 ways to run your commands:

  1. If you have access ro source code or you want to compile it:
    run this command in terminal dotnet run YOUR_COMMAND ARGS
  2. If you don't have access to source code or you don't want to compile it:
    run this command in terminal ./YOUR_APP YOUR_COMMAND ARGS

Available commands:

These commands will be updated over time, you can add a command and create an RP to this repo.
Command Description Args
help Prints all available commands
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 1,778 3/10/2022
1.0.1 491 2/6/2022
1.0.0 482 2/6/2022