Giter VIP home page Giter VIP logo

Comments (2)

BorisWilhelms avatar BorisWilhelms commented on September 23, 2024

Hi Steve,

I am not sure what you want to achiev. If you want to have command line arguments in options you can use the command line provider for the Microsoft configuration library (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?tabs=basicconfiguration#commandline-configuration-provider).

If you like to instantiate a class (via DI) by string you must create the type via Type.GetType and then you can retrieve the instance via DI.

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public static class Startup
    {
        public static async Task Main(string[] args)
        {
            var program = BuildProgram(args);
            await program.Run();
            Console.ReadLine();
        }

        private static void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<ProgramA>();
            services.AddSingleton<ProgramB>();
        }

        private static IProgram BuildProgram(string[] args)
        {
            var serviceCollection = new ServiceCollection();
            ConfigureServices(serviceCollection);

            var serviceProvider = serviceCollection.BuildServiceProvider(true);
            var type = Type.GetType(args[0], true, true);
            return serviceProvider.GetRequiredService(type) as IProgram;
        }
    }

    public interface IProgram
    {
        Task Run();
    }

    public class ProgramA : IProgram
    {
        public async Task Run()
        {
            Console.WriteLine("Hello From Program A");
        }
    }

    public class ProgramB : IProgram
    {
        public async Task Run()
        {
            Console.WriteLine("Hello From Program B");
        }
    }
}

You can run this program with consoleapp1.exe ConsoleApp1.ProgramA or with consoleapp1.exe ConsoleApp1.ProgramB.

I hope this helps!

from advancedconsoleapptemplate.

SteveDrakey avatar SteveDrakey commented on September 23, 2024

I will take a look at that, I have used the NUGET package CommandLine for a few projects, the fact that you can decorate the command line options class with attributes to make to generate a helper message is very useful. I think I missed some key points from my example, like the fact that it will parse params and it lets you have different params for different VERBS, it considers the first param to be the VERB

consoleapp1.exe doWebJob -url <url> -user <username> -password <password>
consoleapp1.exe doFolderPath -path <folder> -recursive

I would like to run

doWebJob or doFolderPath and auto magically pass over the parms, just like MVC routing does

eg

doWebJob.Run( url, username, password)

https://github.com/commandlineparser/commandline

Thanks for getting back, if I come up with a fancy solution I will ping it over.

from advancedconsoleapptemplate.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.