{"id":15962412,"url":"https://github.com/darkxahtep/darkxahtep.commandline","last_synced_at":"2026-03-09T20:05:41.572Z","repository":{"id":96617090,"uuid":"108479346","full_name":"DarkXaHTeP/DarkXaHTeP.CommandLine","owner":"DarkXaHTeP","description":"Allows creating CommandLine applications using Microsoft.Extensions.CommandLineUtils together with DI, Configuration and Logging in a convenient way similar to AspNetCore Hosting","archived":false,"fork":false,"pushed_at":"2017-12-09T00:46:25.000Z","size":54,"stargazers_count":4,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-07T06:06:24.056Z","etag":null,"topics":["aspnetcore","cmd","commandline","configuration","console","console-application","dependency-injection","hosting","logging","microsoft-commandlineutils","parser"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DarkXaHTeP.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-27T00:18:13.000Z","updated_at":"2021-10-06T19:26:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"90b4a8fa-cf89-4e40-8de9-ee67ef84a304","html_url":"https://github.com/DarkXaHTeP/DarkXaHTeP.CommandLine","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DarkXaHTeP%2FDarkXaHTeP.CommandLine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DarkXaHTeP%2FDarkXaHTeP.CommandLine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DarkXaHTeP%2FDarkXaHTeP.CommandLine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DarkXaHTeP%2FDarkXaHTeP.CommandLine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DarkXaHTeP","download_url":"https://codeload.github.com/DarkXaHTeP/DarkXaHTeP.CommandLine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252823918,"owners_count":21809713,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aspnetcore","cmd","commandline","configuration","console","console-application","dependency-injection","hosting","logging","microsoft-commandlineutils","parser"],"created_at":"2024-10-07T16:03:38.191Z","updated_at":"2026-03-09T20:05:41.534Z","avatar_url":"https://github.com/DarkXaHTeP.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DarkXaHTeP.CommandLine\n\nAllows creating CommandLine applications using Microsoft.Extensions.CommandLineUtils together with DI, Configuration and Logging in a convenient way similar to AspNetCore Hosting.\n\n#### NuGet\n\n[![NuGet Version](https://img.shields.io/nuget/v/DarkXaHTeP.CommandLine.svg)](https://www.nuget.org/packages/DarkXaHTeP.CommandLine/)\n[![NuGet Downloads](https://img.shields.io/nuget/dt/DarkXaHTeP.CommandLine.svg)](https://www.nuget.org/packages/DarkXaHTeP.CommandLine/)\n\n#### Build\n\n[![Travis build](https://img.shields.io/travis/DarkXaHTeP/DarkXaHTeP.CommandLine/master.svg)](https://travis-ci.org/DarkXaHTeP/DarkXaHTeP.CommandLine)\n[![Coverage report](https://img.shields.io/coveralls/github/DarkXaHTeP/DarkXaHTeP.CommandLine.svg)](https://coveralls.io/github/DarkXaHTeP/DarkXaHTeP.CommandLine)\n\n## Getting Started\n\nTo use the library you need to go through 2 simple steps:\n\nDefine Startup class similar to Asp.Net Core:\n\n```c#\npublic class Startup\n{   \n    public void Configure(IApplicationBuilder app)\n    {\n        app.OnExecute(async () =\u003e\n        {\n            Console.WriteLine(\"Hello World\")\n            return 0;\n        });\n    }\n}\n```\n\nCreate CommandLine Host using CommandLineHostBuilder and run it:\n\n```c#\nstatic class Program\n{\n    static int Main(string[] args)\n    {\n        ICommandLineHost host = new CommandLineHostBuilder()\n            .UseStartup\u003cStartup\u003e()\n            .Build();\n            \n        return host.Run(args);\n    }\n}\n```\n\nThe application will print \"Hello World\" to console output and exit with 0 code.\n\n## Release notes\n\nFor release notes please see [CHANGELOG.md](https://github.com/DarkXaHTeP/DarkXaHTeP.CommandLine/blob/master/CHANGELOG.md)\n\n## Startup class\n\nSame as Asp.Net Core Startup, CommandLine Startup can have two methods:\n\n`void Configure(IApplicationBuilder app)` is required. This is where application\narguments and commands configuration happens using IApplicationBuilder.\n\n`void ConfigureServices(IServiceCollection services)` is optional. Allows configuring Dependency Injection\nby providing access to application's IServiceCollection. This method is invoked prior to `Configure`\n\n## IApplicationBuilder interface\n\nThis interface provides a set of methods for configuring application. These methods are similar to provided by\nMicrosoft.Extensions.CommandLineUtils (actually this library uses CommandLineUtils internally,\nhowever this may change in future due to the fact that CommandLineUtils is no longer supported).\n\n```c#\nIServiceProvider ApplicationServices { get; }\n```\ngives access to services registered in application\n\n```c#\nIApplicationBuilder Parent { get; }\n```\ncontains link to parent IApplicationBuilder. Is null for the root builder\n\n```c#\nCommandArgument Argument(string name, string description, bool multipleValues = false);\n```\nallows to define command argument\n\n```c#\nIApplicationBuilder Command (string name, Action\u003cIApplicationBuilder\u003e configure, bool throwOnUnexpectedArg = true);\n```\nallows to define command\n\n```c#\nvoid OnExecute (Func\u003cSystem.Threading.Tasks.Task\u003cint\u003e\u003e invoke);\nvoid OnExecute (Func\u003cint\u003e invoke);\n```\nallows to define root or command execution callback\n\n```c#\nCommandOption Option (string template, string description, CommandOptionType optionType);\n```\nallows to define option\n\n```c#\nvoid ShowHelp (string commandName = null);\n```\nshows help for the particular command or a whole app. Could be used in execute callbacks\n\n#### Example:\n```c#\nvar greeter = app.ApplicationServices.GetRequiredService\u003cIGreeter\u003e();\n \napp.OnExecute(() =\u003e {\n    app.ShowHelp();\n    return 0;\n});\n \napp.Command(\"greet\", command =\u003e {\n    var casualOption = command.Option(\"-c|--casual\", \"This greeting is informal/casual\", CommandOptionType.NoValue);\n    var nameArgument = command.Argument(\"\u003cname1 name2\u003e\", \"Names of persons to greet separated with space\", true);\n    \n    command.OnExecute(async () =\u003e {\n        string names = nameArgument.Values;\n        bool isCasual = casualOption.HasValue();\n        \n        string greeting = await greeter.GetGreetingAsync(isCasual);\n        greeter.GreetAll(greeting, names);\n        \n        return 0;\n    });\n});\n```\n\n## CommandLineHostBuilder class\n\nThis class is similar to `WebHostBuilder` from Asp.Net Core and allows to setup application configuration,\nlogging and arguments parsing strategy before execution.\n\n```c#\nICommandLineHostBuilder ConfigureAppConfiguration(Action\u003cIConfigurationBuilder\u003e configureDelegate);\nICommandLineHostBuilder ConfigureAppConfiguration(Action\u003cCommandLineHostBuilderContext, IConfigurationBuilder\u003e configureDelegate);\n```\nallows to configure application configuration\n\n```c#\nICommandLineHostBuilder ConfigureLogging(Action\u003cILoggingBuilder\u003e configureLogging);\nICommandLineHostBuilder ConfigureLogging(Action\u003cCommandLineHostBuilderContext, ILoggingBuilder\u003e configureLogging);\n```\nallows to configure logging\n\n```c#\nICommandLineHostBuilder UseContentRoot(string contentRoot);\n```\nallows to redefine content root that will be used for resolving relative paths (e.g. path to json configuration file if used)\n\n```c#\nICommandLineHostBuilder UseStartup\u003cTStartup\u003e() where TStartup : class;\n```\nsets Startup class to use. This method is required to call because no automatic Startup discovery is implemented\n\n```c#\nICommandLineHostBuilder AllowUnexpectedArgs();\n```\nchanges argument parsing to be less strict and not throw on unexpected arguments\n\n```c#\nICommandLineHost Build();\n```\nbuilds command line host\n\n```c#\nICommandLineHostBuilder ConfigureServices(Action\u003cIServiceCollection\u003e configureServices);\nICommandLineHostBuilder ConfigureServices(Action\u003cCommandLineHostBuilderContext, IServiceCollection\u003e configureServices);\n```\nallows to define additional services. Could be used in extension methods to extend behaviour\n\n#### Example:\n```c#\nstatic class Program\n{\n    static int Main(string[] args)\n    {\n        ICommandLineHost host = new CommandLineHostBuilder()\n            .UseContentRoot(Directory.GetCurrentDirectory())\n            .ConfigureLogging(logging =\u003e\n            {\n                logging\n                    .AddConsole()\n                    .AddDebug();\n            })\n            .ConfigureAppConfiguration(config =\u003e\n            {\n                config\n                    .AddJsonFile(\"appsettings.json\")\n                    .AddConsul(\"application\")\n                    .AddEnvironmentVariables();\n            })\n            .UseStartup\u003cStartup\u003e()\n            .AllowUnexpectedArgs()\n            .Build();\n\n        return host.Run(args);\n    }\n}\n```\n\n## Dependency Injection\n\nIt is possible to inject dependencies either through constructor of `Startup` class or `Configure` method,\nhowever only built-in dependencies (`IConfiguration`, `ICommandLineEnvironment`, `ILogger\u003c\u003e` etc.) are available through constructor\nbecause `ConfigureServices` method is not yet called.\n\n#### Example:\n```c#\npublic class Startup\n{   \n    public Startup(\n        IConfiguration config,\n        ICommandLineEnvironment environment,\n        ILogger\u003cStartup\u003e logger\n    )\n    {}\n    \n    public void Configure(IApplicationBuilder app, IMyService service)\n    {\n    }\n    \n    public void ConfigureServices(IServiceCollection services)\n    {\n        services.AddSingleton\u003cIMyService, MyService\u003e();\n    }\n}\n```\n\n## Acknowledgements\n\nThis library is highly inspired by [AspNet.Core Hosting](https://github.com/aspnet/Hosting/tree/rel/2.0.0) from Microsoft\n\n## Contribution\n\nFeel free to ask questions and post bugs/ideas in the issues, as well as send pull requests.\n\n[![License](https://img.shields.io/github/license/darkxahtep/DarkXaHTeP.CommandLine.svg)](https://github.com/DarkXaHTeP/DarkXaHTeP.CommandLine/blob/master/LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarkxahtep%2Fdarkxahtep.commandline","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarkxahtep%2Fdarkxahtep.commandline","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarkxahtep%2Fdarkxahtep.commandline/lists"}