RSCG – TeCLI

RSCG – TeCLI
 
 

name TeCLI
nuget https://www.nuget.org/packages/TeCLI/
link https://github.com/tyevco/TeCLI
author Tyler Coles

Parse Command line arguments

 

This is how you can use TeCLI .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
	<ItemGroup>
	  <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" />
	  <PackageReference Include="TeCLI" Version="0.2.5">
	  </PackageReference>
		<PackageReference Include="TeCLI.Extensions.DependencyInjection" Version="0.2.5">
		</PackageReference>

	</ItemGroup>
</Project>


The code that you will use is


using TeCLI;
using Microsoft.Extensions.DependencyInjection;

Console.WriteLine("Hello, World!");
// execute with makesum sum 10 20

//Do not know how to work those
// --help
// --msg Andrei
// echo --msg Andrei
// sum 10 20

IServiceCollection services = new ServiceCollection();
services.AddCommandDispatcher();

var sp = services.BuildServiceProvider();

var dispatcher = sp.GetRequiredService<CommandDispatcher>();
await dispatcher.DispatchAsync(args);



using TeCLI.Attributes;

namespace ConsoleDemo;

[Command("MakeSum", Description = "Makes sum")]
public  class CmdForSum
{
    [Action("sum")]
    public void MySum([Argument(Description = "x")] int x, [Argument(Description = "y")] int y)
    {
        Console.WriteLine($"Hello, {x+y}!");
    }
}


 

The code that is generated is

using System;
using System.Linq;
using TeCLI;
using TeCLI.Attributes;
using global::ConsoleDemo;

namespace TeCLI
{
    public partial class CommandDispatcher
    {
        private async Task DispatchCmdForSumAsync(string[] args)
        {
            if (args.Length == 0)
            {
                throw new Exception();
            }
            else
            {
                string action = args[0].ToLower();
                string[] remainingArgs = args.Skip(1).ToArray();
                switch (action)
                {
                    case "sum":
                    {
                        ProcessCmdForSumMySum(remainingArgs);
                        break;
                    }
                    default:
                    {
                        Console.WriteLine($"Unknown action: {action}");
                        break;
                    }
                }
            }
        }

        private void ProcessCmdForSumMySum(string[] args)
        {
            if (args.Length == 0)
            {
                throw new Exception();
            }
            else
            {
                int p0 = default;
                {
                    if (args.Length < 1)
                    {
                        throw new ArgumentException("Required argument 'x' not provided.");
                    }
                    else
                    {
                        try
                        {
                            p0 = (int)Convert.ChangeType(args&#91;0&#93;, typeof(int));
                        }
                        catch
                        {
                            throw new ArgumentException("Invalid syntax provided for argument 'x'.");
                        }
                    }
                }
                int p1 = default;
                {
                    if (args.Length < 2)
                    {
                        throw new ArgumentException("Required argument 'y' not provided.");
                    }
                    else
                    {
                        try
                        {
                            p1 = (int)Convert.ChangeType(args&#91;1&#93;, typeof(int));
                        }
                        catch
                        {
                            throw new ArgumentException("Invalid syntax provided for argument 'y'.");
                        }
                    }
                }

                // Now invoke the method with the parsed parameters
                InvokeCommandAction<CmdForSum>(command => command.MySum(p0, p1));
            }
        }

    }
}

using System;

namespace TeCLI
{
    public partial class CommandDispatcher
    {
        public static void DisplayCommandCmdForSumHelp(string actionName = null)
        {
            Console.WriteLine("Please provide more details...");
        }
    }
}

using System;
using System.Linq;

namespace TeCLI
{
    public partial class CommandDispatcher
    {
        public async Task DispatchAsync(string[] args)
        {
            if (args.Length == 0)
            {
                DisplayApplicationHelp();
            }
            else
            {

                string command = args[0].ToLower();
                string[] remainingArgs = args.Skip(1).ToArray();

                switch (command)
                {
                    case "makesum":
                    {
                        await DispatchCmdForSumAsync(remainingArgs);
                        break;
                    }

                    default:
                    {
                        Console.WriteLine($"Unknown command: {args[0]}");
                        DisplayApplicationHelp();
                        break;
                    }
                }
            }
        }
    }
}

using System;

namespace TeCLI
{
    public partial class CommandDispatcher
    {
        public static void DisplayApplicationHelp()
        {
            Console.WriteLine("Please provide more details...");
        }
    }
}

using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;

namespace TeCLI
{
    public partial class CommandDispatcher
    {
        private IServiceProvider ServiceProvider { get; }

        public CommandDispatcher(IServiceProvider serviceProvider)
        {
            ServiceProvider = serviceProvider;
        }

        async Task InvokeCommandActionAsync<TCommand>(Func<TCommand, Task> parameterizedAction)
        {
            var command = ServiceProvider.GetRequiredService<TCommand>();
            await parameterizedAction?.Invoke(command);
        }

        void InvokeCommandAction<TCommand>(Action<TCommand> parameterizedAction)
        {
            var command = ServiceProvider.GetRequiredService<TCommand>();
            parameterizedAction?.Invoke(command);
        }
    }
}

using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;

namespace TeCLI
{
    public static class CommandDispatcherExtensions
    {
        public static IServiceCollection AddCommandDispatcher(this IServiceCollection services)
        {
            services.AddSingleton<CommandDispatcher>();
            services.AddSingleton<global::ConsoleDemo.CmdForSum>();
            return services;
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/TeCLI


Posted

in

, ,

by

Tags: