{"id":21313959,"url":"https://github.com/octopusdeploy/commandline","last_synced_at":"2025-10-11T12:04:40.706Z","repository":{"id":37799320,"uuid":"362992044","full_name":"OctopusDeploy/CommandLine","owner":"OctopusDeploy","description":"| Public |  The command line parsing library used by many of Octopus apps.","archived":false,"fork":false,"pushed_at":"2025-07-11T15:02:41.000Z","size":197,"stargazers_count":2,"open_issues_count":4,"forks_count":1,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-07-11T17:23:52.500Z","etag":null,"topics":["public"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OctopusDeploy.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-30T01:24:49.000Z","updated_at":"2025-04-16T06:15:40.000Z","dependencies_parsed_at":"2023-12-21T18:25:06.569Z","dependency_job_id":"4f16bf3f-f642-45e8-93c9-336fa48b13bd","html_url":"https://github.com/OctopusDeploy/CommandLine","commit_stats":{"total_commits":78,"total_committers":6,"mean_commits":13.0,"dds":0.3589743589743589,"last_synced_commit":"f17a3e0358ef9ecd2e412bf5e7ab65cc3d027654"},"previous_names":[],"tags_count":361,"template":false,"template_full_name":null,"purl":"pkg:github/OctopusDeploy/CommandLine","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OctopusDeploy%2FCommandLine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OctopusDeploy%2FCommandLine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OctopusDeploy%2FCommandLine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OctopusDeploy%2FCommandLine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OctopusDeploy","download_url":"https://codeload.github.com/OctopusDeploy/CommandLine/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OctopusDeploy%2FCommandLine/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264923080,"owners_count":23683716,"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":["public"],"created_at":"2024-11-21T18:09:28.379Z","updated_at":"2025-10-11T12:04:35.669Z","avatar_url":"https://github.com/OctopusDeploy.png","language":"C#","readme":"## Octopus.CommandLine\n\nThis repository contains Octopus.CommandLine, the command line parsing library used by many of Octopus apps.\n\nPlease see [Contributing](CONTRIBUTING.md).\n\n## Usage\n\n### Setup references\n\nAdd a reference to Octopus.CommandLine, Newtonsoft.Json and Autofac:\n```bash\ndotnet add package Octopus.CommandLine\ndotnet add package Autofac\n```\nNote:\n* Autofac can be swapped for your container of choice\n\n### Build your container\n```c#\nprivate static IContainer BuildContainer()\n{\n    var builder = new ContainerBuilder();\n\n    //configure logging - you're already likely doing this\n    Log.Logger = new LoggerConfiguration()\n        .WriteTo.ColoredConsole(outputTemplate: \"{Message}{NewLine}{Exception}\")\n        .CreateLogger();\n    builder.RegisterInstance(Log.Logger).As\u003cILogger\u003e().SingleInstance();\n\n    //Sometimes you'll want to provide your own implementation of these, but for most usages\n    //the default ones work fine\n    builder.RegisterType\u003cDefaultCommandOutputJsonSerializer\u003e()\n        .As\u003cICommandOutputJsonSerializer\u003e();\n    builder.RegisterType\u003cCommandOutputProvider\u003e()\n        .WithParameter(\"applicationName\", \"My sample application\")\n        .WithParameter(\"applicationVersion\", typeof(Startup).GetTypeInfo().Assembly.GetCustomAttribute\u003cAssemblyInformationalVersionAttribute\u003e().InformationalVersion) \n        .As\u003cICommandOutputProvider\u003e()\n        .SingleInstance();\n        \n    //Register all the built-in shell completion installers\n    builder.RegisterAssemblyTypes(typeof(ICommand).Assembly)\n        .Where(t =\u003e t.IsAssignableTo\u003cIShellCompletionInstaller\u003e())\n        .AsImplementedInterfaces()\n        .AsSelf();\n        \n    //register the command locator and other in-built commands (such as help, and the auto complete commands)\n    builder.RegisterType\u003cCommandLocator\u003e().As\u003cICommandLocator\u003e();\n    builder.RegisterAssemblyTypes(typeof(ICommand).Assembly).As\u003cICommand\u003e().AsSelf();\n\n    //Register any implementations in your app\n    var thisAssembly = typeof(Program).GetTypeInfo().Assembly;\n    builder.RegisterAssemblyTypes(thisAssembly).As\u003cICommand\u003e().AsSelf();\n\n    return builder.Build();\n}\n```\n\n### Find and execute the command in your `Main` method\n\n```c#\nstatic async Task\u003cint\u003e Main(string[] args)\n{\n    var container = BuildContainer();\n    var commandLocator = container.Resolve\u003cICommandLocator\u003e();\n    try\n    {\n        var command = commandLocator.GetCommand(args);\n        await command.Execute(args.Skip(1).ToArray());\n        return 0;\n    }\n    catch (CommandException ex)\n    {\n        //this is a \"known error\" - ie, one we dont want a stack trace for\n        Log.Error(ex.Message);\n        return 1;\n    }\n    catch (Exception ex)\n    {\n        //this is an unknown error - log with stack trace\n        Log.Error(ex, ex.Message);\n        return 2;\n    }\n}\n```\n\n### Create your first command\n\n```c#\n[Command(\"mycommand\", Description = \"Does the thing\")]\npublic class MyCommand : CommandBase\n{\n    private readonly ICommandOutputProvider _commandOutputProvider;\n    private bool dryRun;\n\n    public MyCommand(ICommandOutputProvider commandOutputProvider) : base(commandOutputProvider)\n    {\n        _commandOutputProvider = commandOutputProvider;\n        var options = Options.For(\"My Command\");\n        options.Add\u003cbool\u003e(\"dryRun\",\n            \"Dry run will output the proposed changes to console, instead of writing to disk.\",\n            v =\u003e dryRun = true);\n    }\n\n    public override Task Execute(string[] commandLineArguments)\n    {\n        var remainingArguments = Options.Parse(commandLineArguments);\n        if (remainingArguments.Count \u003e 0)\n            throw new CommandException(\"Unrecognized command arguments: \" + string.Join(\", \", remainingArguments));\n\n        _commandOutputProvider.Information(\"This is my command\");\n        _commandOutputProvider.Information(dryRun\n            ? \"This is a dry-run; skipping doing the thing\"\n            : \"Doing the thing\");\n        return Task.CompletedTask;\n    }\n```\n\n## Test it out:\n```powershell\nPS\u003e ./myapp.exe help\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foctopusdeploy%2Fcommandline","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foctopusdeploy%2Fcommandline","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foctopusdeploy%2Fcommandline/lists"}