{"id":23045440,"url":"https://github.com/mykeels/commandlineparser","last_synced_at":"2025-08-14T23:31:57.630Z","repository":{"id":93601892,"uuid":"96244960","full_name":"mykeels/CommandLineParser","owner":"mykeels","description":"A command-line-parser c# library for .NET","archived":false,"fork":false,"pushed_at":"2018-06-03T14:00:01.000Z","size":64,"stargazers_count":16,"open_issues_count":1,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T03:12:22.832Z","etag":null,"topics":["args","cli","command-line","dotnet","parser"],"latest_commit_sha":null,"homepage":null,"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/mykeels.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-04T18:43:53.000Z","updated_at":"2024-07-11T17:42:30.000Z","dependencies_parsed_at":"2023-03-10T14:15:42.900Z","dependency_job_id":null,"html_url":"https://github.com/mykeels/CommandLineParser","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mykeels/CommandLineParser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykeels%2FCommandLineParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykeels%2FCommandLineParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykeels%2FCommandLineParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykeels%2FCommandLineParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mykeels","download_url":"https://codeload.github.com/mykeels/CommandLineParser/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykeels%2FCommandLineParser/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270500001,"owners_count":24595150,"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","status":"online","status_checked_at":"2025-08-14T02:00:10.309Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["args","cli","command-line","dotnet","parser"],"created_at":"2024-12-15T21:20:39.082Z","updated_at":"2025-08-14T23:31:57.617Z","avatar_url":"https://github.com/mykeels.png","language":"C#","readme":"## Command Line Parser\n\nA simple .NET CLI arguments parser library using C# attributes.\n\nTired of reading the `string[] args` passed to your main method yourself? This simple library lets you parse the arguments into any standard .NET object.\n\n### Installation\n\n```\ndotnet add package CommandLine.Parser\n```\n\n### Examples\n\nHere are a few examples:\n\n#### Default Option-Name Mode\n\nTake this simple program for example:\n\n```bat\ncopy.exe --source \"./my-stuff.txt\" --destination \"./new-folder\"\n```\n\nWe want the `--source` and `--destination` values.\n\n```csharp\npublic class FileCopyModel {\n    public string source { get; set; }\n    public string destination { get; set; }\n}\n\npublic class Program {\n    public static void Main(string[] args) {\n        CommandLineParser parser = new CommandLineParser(args);\n        FileCopyModel copyInfo = parser.Parse\u003cFileCopyModel\u003e();\n        /* copyinfo contains:\n            {\n                \"source\": \"./my-stuff.txt\",\n                \"destination\": \"./new-folder\",\n            }\n        */\n        Console.Read();\n    }\n}\n```\n\n#### Custom Option-Name Mode\n\nIf for the same program above, we wanted to use short-hand option-name representation instead. E.g.\n\n```bat\ncopy.exe -s \"./my-stuff.txt\" -d \"./new-folder\"\n```\n\nOur `FileCopyModel` class would have to change a bit\n\n```csharp\nusing CommandLineParser.Attributes;\npublic class FileCopyModel {\n    [Flag(\"s\")]\n    public string source { get; set; }\n    [Flag(\"d\")]\n    public string destination { get; set; }\n}\n```\n\nOur `Program` class would be exactly the same. The `FlagAttribute` class can be found [here](CommandLineParser/Attributes/FlagAttribute.cs)\n\n#### Required Flags Mode\n\nIt might be neccessary for some flags/options to be made compulsory so the program would not proceed unless their values are provided. Set the `required` boolean parameter in the `Flag` attribute constructor to `true` on the property you want to be compulsory\n\n```csharp\n[Flag(\"s\", required: true)]\npublic string source { get; set; }\n```\n\nThe full definition for `FlagsAttribute` is:\n\n```csharp\n[Flag(shortName: \"s\", name: \"source\", required: true)]\n```\n\n#### Help Text Mode\n\nIn command-line interfaces, help information is neccessary for navigating through command APIs. Even here, `CommandLineParser` has got your back.\n\n```csharp\n[Help(\"Handles file-copy actions\")]\npublic class FileCopyModel {\n    [Flag(\"s\", required: true)]\n    public string source { get; set; }\n    [Flag(\"d\")]\n    public string destination { get; set; }\n}\n\npublic class Program {\n    public static void Main(string[] args) {\n        CommandLineParser parser = new CommandLineParser(args);\n        string helpText = parser.GetHelpInfo\u003cFileCopyModel\u003e();\n        if (!string.IsNullOrEmpty(helpText)) {\n            Console.WriteLine(helpText);\n        }\n        Console.Read();\n    }\n}\n```\n\nNow run in command-line:\n\n```bat\ncopy.exe --help\n```\n\nResults:\n```bat\n========== Help Information ==========\nHandles file-copy actions\n\ncopy.exe -s [-d]\n========== End Help Information ==========\n```\n\n#### Help Text for Option Mode\n\nYour users can query help information for a particular option. E.g.\n\n```bat\ncopy.exe --source --help\n```\n\nResult:\n\n```bat\n--source (The original uri location of the file)\n```\n\n```csharp\n[Help(\"Handles file-copy actions\")]\npublic class FileCopyModel {\n    [Flag(\"s\", required: true)]\n    [Help(\"The original uri location of the file\")]\n    public string source { get; set; }\n    [Flag(\"d\")]\n    [Help(\"The uri location the file is to be replicated in\")]\n    public string destination { get; set; }\n}\n```\n\n### Supported Types\n\n- String\n- Integer\n- Long\n- Boolean\n- DateTime\n- Enum\n- .NET Complex Objects\n\n### Support for Complex .NET Objects using Transforms\n\nWhen one of the `flags` or `options` has a string value or values that you intend to convert into a complex type like say [AddressModel](CommandLineParser.Console/Models/AddressModel.cs) for instance, you can use the `[TransformAttribute]` attribute on the corresponding property.\n\nThe `TransformAttribute` takes a `Type` and `string` parameter. The `string` parameter should be the name of an existing method that takes a string parameter, converts it to and returns an object of the desired complex type.\n\nThe `Type` parameter should be the `Type` of the parent class that contains the method discussed above.\n\nE.g.\n\n```bat\ncontact.exe --add \"{\"name\":\"Mykeels\",\"phone\":8012345678,\"email\":\"contact@example.com\"}\"\n```\n\n```csharp\npublic class ContactSetupModel {\n    \n    [Flag(name: \"add\")]\n    [Transform(typeof(ContactSetupModel), nameof(ConvertToContact))]\n    public ContactInsertModel Contact { get; set; }\n\n    public ContactInsertModel ConvertToContact(string contactInfo) {\n        return Newtonsoft.Json.JsonConvert.DeserializeObject\u003cContactInsertModel\u003e(contactInfo);\n    }\n\n    public class ContactInsertModel {\n        public string name { get; set; }\n        public string phone { get; set; }\n        public string email { get; set; }\n    }\n}\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmykeels%2Fcommandlineparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmykeels%2Fcommandlineparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmykeels%2Fcommandlineparser/lists"}