VConsole 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package VConsole --version 1.0.1                
NuGet\Install-Package VConsole -Version 1.0.1                
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="VConsole" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add VConsole --version 1.0.1                
#r "nuget: VConsole, 1.0.1"                
#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 VConsole as a Cake Addin
#addin nuget:?package=VConsole&version=1.0.1

// Install VConsole as a Cake Tool
#tool nuget:?package=VConsole&version=1.0.1                

VConsole

VConsole is a .NET library to parse command line arguments and execute commands.

The VConsole command Line Parser Library offers CLR applications a clean and concise API for manipulating command line arguments and related tasks, such as defining switches, options and verb commands. It allows you to display a help screen with a high degree of customization and a simple way to report syntax errors to the end user.

 dotnet add package VConsole

or

 NuGet\Install-Package VConsole

Quick Start Example

  • Create a class to define valid command with varb and options attrbutes to receive the parsed options.
  • Register commands using RegisterCommand or RegisterCommandsFromAssembly methods.
  • Call ParseArguments with the args string array.

Example:

using VConsole;

[Verb("clone", HelpText = "Clone a repository into a new directory.")]
public class Clone : ICommand
{
    [Option('u', "url", Required = true, HelpText = "Cloud repository URL.")]
    public string URL { get; set; } = string.Empty;
    public void Execute()
    {
        Console.WriteLine($"Cloning a repository: {URL}");
    }
}

# top level statment in dotnet core
Parser.Default
    .RegisterCommand<Clone>()
    .ParseArguments(args);

or

static void Main(string[] args)
{
    Parser.Default
    .RegisterCommand<Clone>()
    .ParseArguments(args);
}

# Build your application and run it like this:
myapp.exe clone --url=https://github.com/VikashChauhan51/vconsole.git

Dependency Resolver Example:

Here we took an example with Microsoft Dependency Injection, but you can use any one you prefer. Please add following nuget packages before to proceed:

  • VConsole
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Logging.Console
// Crate a fake dependency service for command

public interface IFooService
{
    void DoThing(string message);
}

public class FooService : IFooService
{
    private readonly ILogger<FooService> logger;
    public FooService(ILoggerFactory loggerFactory)
    {
        logger = loggerFactory.CreateLogger<FooService>();
    }

    public void DoThing(string message)
    {
        logger.LogInformation($"Doing the thing {message}");
    }
}

// Create a command with dependency

[Verb("clone", HelpText = "Clone a repository into a new directory.")]
public class Clone : ICommand
{
    [Option('u', "url", Required = true, HelpText = "Cloud repository URL.")]
    public string URL { get; set; } = string.Empty;

    private readonly IFooService fooService;
    private readonly ILogger<Clone> logger;
    public Clone(IFooService fooService, ILogger<Clone> logger)
    {
        this.fooService = fooService;
        this.logger = logger;
    }
    public void Execute()
    {
        fooService.DoThing("Pulling...");
        logger.LogInformation($"Cloning a repository: {URL}");
    }
}


// Create a service resolver

public class DependencyResolver : IDependencyResolver
{
    private readonly ServiceProvider serviceProvider;

    public DependencyResolver(ServiceProvider serviceProvider)
    {
        this.serviceProvider = serviceProvider;
    }
    public object GetService(Type serviceType) => serviceProvider.GetService(serviceType) ?? throw new ArgumentOutOfRangeException(nameof(serviceType));

}

// create DI container and parser. (Program.cs)

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using VConsole;


//setup our DI
var serviceProvider = new ServiceCollection()
    .AddLogging(x => x.AddConsole())
    .AddSingleton<IFooService, FooService>()
    .BuildServiceProvider();

//setup dependency resolver
var serviceResolver = new DependencyResolver(serviceProvider);

//create parser
var parser = new Parser(serviceResolver);

// configure commands
parser
    .RegisterCommand<Clone>()
    .ParseArguments(args);

# Build your application and run it like this:

myapp.exe clone --url=https://github.com/VikashChauhan51/vconsole.git

Culture Example:

Default parser has InvariantCulture to parse command parameters values:

using VConsole;

[Verb("clone", HelpText = "Clone a repository into a new directory.")]
public class Clone : ICommand
{
    [Option('u', "url", Required = true, HelpText = "Cloud repository URL.")]
    public string URL { get; set; } = string.Empty;
    public void Execute()
    {
        Console.WriteLine($"Cloning a repository: {URL}");
    }
}

//create parser settings
var settings = new ParserSettings
    {
        // set current culture instead of Invariant.
        ParsingCulture = Thread.CurrentThread.CurrentCulture
    };

//create parser with settings
var parser = New Parser(settings);

// configure commands
parser
    .RegisterCommand<Clone>()
    .ParseArguments(args);

# Build your application and run it like this:

myapp.exe clone --url=https://github.com/VikashChauhan51/vconsole.git

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • 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.3.0 185 6/25/2023
1.2.0 168 6/24/2023
1.1.0 149 6/23/2023
1.0.1 121 6/22/2023
1.0.0 166 6/20/2023