{"id":22006376,"url":"https://github.com/tom-wolfe/wolfe.fluentcli","last_synced_at":"2025-03-23T06:44:03.498Z","repository":{"id":39709930,"uuid":"412427879","full_name":"tom-wolfe/Wolfe.FluentCli","owner":"tom-wolfe","description":"A .NET Fluent API for creating command line interfaces.","archived":false,"fork":false,"pushed_at":"2022-05-27T10:13:10.000Z","size":129,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-20T00:39:49.515Z","etag":null,"topics":[],"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/tom-wolfe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-01T10:46:51.000Z","updated_at":"2022-06-27T12:34:39.000Z","dependencies_parsed_at":"2022-08-28T06:32:58.769Z","dependency_job_id":null,"html_url":"https://github.com/tom-wolfe/Wolfe.FluentCli","commit_stats":null,"previous_names":["trwolfe13/fluentcli"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tom-wolfe%2FWolfe.FluentCli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tom-wolfe%2FWolfe.FluentCli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tom-wolfe%2FWolfe.FluentCli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tom-wolfe%2FWolfe.FluentCli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tom-wolfe","download_url":"https://codeload.github.com/tom-wolfe/Wolfe.FluentCli/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245066523,"owners_count":20555404,"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":[],"created_at":"2024-11-30T01:12:00.858Z","updated_at":"2025-03-23T06:44:03.481Z","avatar_url":"https://github.com/tom-wolfe.png","language":"C#","readme":"# Wolfe.FluentCli\n\nA .NET 5 Fluent API for creating command line interfaces.\n\n## Basic Usage\n\n### Building a CLI\n\nA CLI object is a mediator that invokes a named command handler based on a string input, along with any required parameters. The first step is defining what the valid command structure is, and what arguments, if any, each command requires. The entire process starts with the static `Cli.Build` method. This method takes a lambda that allows you to construct a CLI interpreter object in any way you need.\n\nHere is a basic example:\n\n```cs\nvar cli = Cli.Build(cli =\u003e cli\n    // The handler that runs when no command is specified.\n    .WithDefault\u003cDefaultCommand\u003e()\n    .AddCommand\u003cHelloCommand\u003e(\"hello\", command =\u003e command\n        // Options are bound automatically based on property names in snake case.\n        .WithOptions\u003cHelloCommandArguments\u003e()\n        // Commands can be nested infinitely\n        .AddCommand\u003cFooCommandHandler\u003e(\"foo\")\n    ));\n```\n\n### Command Handlers\n\nCommand handlers implement the `ICommandHandler` interface or the `ICommandHandler\u003cTArgs\u003e` interface if they take any additional parameters. Here are the handlers for the above CLI definition:\n\n```cs\n// Implement ICommandHandler when you don't have any arguments.\npublic class DefaultCommand : ICommandHandler\n{\n    public Task Execute()\n    {\n        Console.WriteLine($\"Default Handler\");\n        return Task.CompletedTask;\n    }\n}\n\n// Implement ICommandHandler\u003cTArgs\u003e when you have configuration.\npublic class HelloCommand : ICommandHandler\u003cHelloCommandArguments\u003e\n{\n    public Task Execute(HelloCommandArguments arguments)\n    {\n        Console.WriteLine($\"Hello {arguments.FirstName}!\");\n        return Task.CompletedTask;\n    }\n}\n\npublic class FooCommandHandler : ICommandHandler\n{\n    public Task Execute()\n    {\n        Console.WriteLine(\"Foo\");\n        return Task.CompletedTask;\n    }\n}\n```\n\n### Arguments\n\nWhen your command handlers take additional values, you can define an arguments model. By default, properties are mapped using snake case. The property `FirstName` would be referenced using `--first-name` in the command line string.\n\nIf you want to override this behaviour, you can use the `CliArgument` attribute to override the short name, long name, and whether or not the argument is required. Where an argument is unnamed, you can use the `CliDefaultArgument` attribute.\n\n```cs\npublic class HelloCommandArguments\n{\n    public string FirstName { get; set; }\n\n    [CliArgument(\"l\", \"last-name\", true)]\n    public string LastName { get; set; }\n}\n```\n\n### Advanced Usage\n\n#### Dependency Injection\n\nBy default, command handlers will be instantiated using `Activator.CreateInstance`. To provide a custom dependency resoltion, you can use the `WithServiceProvider` method on the `ICliBuilder` object:\n\n```cs\nvar cli = Cli.Build(cli =\u003e cli\n    .WithDefault\u003cDefaultCommandHandler, TestArgs\u003e()\n    .WithServiceProvider(x =\u003e myDIContainer.ResolveType(x))\n    .AddCommand(\"foo\", hello =\u003e hello\n        .AddCommand\u003cHelloCommandHandler\u003e(\"hello\"))\n        .AddCommand\u003cBarCommandHandler\u003e(\"bar\")\n    );\n```\n\n### Executing Commands\n\n```cs\n// Executes the default command handler\nawait cli.Execute(\"\");\n\n// Executes the hello command, setting HelloCommandArguments.FirstName to 'Tom'\nawait cli.Execute(\"hello --first-name Tom\");\n\n// Executes the foo subcommand.\nawait cli.Execute(\"hello foo\");\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftom-wolfe%2Fwolfe.fluentcli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftom-wolfe%2Fwolfe.fluentcli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftom-wolfe%2Fwolfe.fluentcli/lists"}