{"id":22837208,"url":"https://github.com/cake-contrib/cake.argumentbinder","last_synced_at":"2025-04-24T03:25:55.736Z","repository":{"id":51281809,"uuid":"205063971","full_name":"cake-contrib/Cake.ArgumentBinder","owner":"cake-contrib","description":"A way to bind Cake Arguments to Classes","archived":false,"fork":false,"pushed_at":"2022-11-21T22:30:47.000Z","size":205,"stargazers_count":4,"open_issues_count":0,"forks_count":3,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-16T00:12:46.688Z","etag":null,"topics":["addin","arguments","build","cake","cake-addin","cake-build","csharp","dotnet"],"latest_commit_sha":null,"homepage":"https://cakebuild.net/extensions/cake-argumentbinder/","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/cake-contrib.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":"2019-08-29T02:29:57.000Z","updated_at":"2024-04-24T01:02:52.000Z","dependencies_parsed_at":"2022-09-24T04:51:49.707Z","dependency_job_id":null,"html_url":"https://github.com/cake-contrib/Cake.ArgumentBinder","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cake-contrib%2FCake.ArgumentBinder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cake-contrib%2FCake.ArgumentBinder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cake-contrib%2FCake.ArgumentBinder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cake-contrib%2FCake.ArgumentBinder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cake-contrib","download_url":"https://codeload.github.com/cake-contrib/Cake.ArgumentBinder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250553879,"owners_count":21449494,"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":["addin","arguments","build","cake","cake-addin","cake-build","csharp","dotnet"],"created_at":"2024-12-12T23:15:59.842Z","updated_at":"2025-04-24T03:25:55.696Z","avatar_url":"https://github.com/cake-contrib.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"Cake.ArgumentBinder\n=========\nA way to bind arguments passed into cake to configuration classes.\n\n[![Build status](https://ci.appveyor.com/api/projects/status/p8qx0ee022gy9r9i?svg=true)](https://ci.appveyor.com/project/cakecontrib/cake-argumentbinder)\n\nAbout\n--------\nThis addin to [Cake](https://github.com/cake-build/cake), a C# build system, allows one to bind arguments passed into Cake\nto C# classes without having to do it manually.\n\nHonestly, for most users of Cake, this addin is probably overkill (even the cakefile for this very project doesn't use it).\nIf was designed for complex, flexible, build environments with multiple Tasks and multiple configurations passed in from the command-line.\n\nPackages\n--------\n[![NuGet](https://img.shields.io/nuget/v/Cake.ArgumentBinder.svg)](https://www.nuget.org/packages/Cake.ArgumentBinder/) \n\nHow it Works\n--------\nLet's say when invoking a target defined in Cake, the target requires several arguments from the user before it can execute.  In this example, it is deleting files from a directory.\nOne option is to just parse the arguments using the Arguments Function:\n\n```C#\nstring path = Argument( \"path\", string.Empty );\nstring pattern = Argument( \"pattern\", string.Empty );\nint filesToKeep = Argument\u003cint\u003e( \"num_to_keep\", 0 );\nbool dryRun = Argument\u003cbool\u003e( \"dry_run\", false );\n```\n\nThis is great and all, but now one should probably do validation to ensure the passed in arguments\nare not an empty string, and the number of files to keep isn't negative.\n\nDon't forget to add to the description what each argument does, which arguments are required, etc!  That gets REALLY old REALLY fast, especially in an environment with multiple tasks.\nWouldn't it be nice if we could take these arguments and bind them to a class we can then use without any issue?\n\nWith this addin, you now can, with the power of attributes.  Now instead of grabbing the arguments manually, just create a configuration class\nand tag the corresponding properties with the proper Attributes.\n\n```C#\npublic class DeleteHelpersConfig\n{\n    // ---------------- Properties ----------------\n\n    /// \u003csummary\u003e\n    /// The directory to delete things from.\n    /// \u003c/summary\u003e\n    [StringArgument(\n        \"path\",\n        Description = \"The path to delete from.\",\n        Required = true,\n        ArgumentSource = ArgumentSource.CommandLineThenEnvironmentVariable\n    )]\n    public string Directory { get; set; }\n\n    /// \u003csummary\u003e\n    /// The number of files or directories \n    /// to keep that match the given pattern.\n    /// Defaulted to 0.\n    /// Can not be negative.\n    /// \u003c/summary\u003e\n    [IntegerArgument(\n        \"num_to_keep\",\n        Description = \"The number of the most recent files/directories to keep that match the pattern.\",\n        DefaultValue = 0,\n        Min = 0,\n        Max = 255,\n        ArgumentSource = ArgumentSource.CommandLineThenEnvironmentVariable\n    )]\n    public int NumberOfFilesToKeep { get; set; }\n\n    /// \u003csummary\u003e\n    /// The glob of the deletion pattern to use.\n    /// \u003c/summary\u003e\n    [StringArgument(\n        \"pattern\",\n        Description = \"The glob pattern to delete files/directories from.\",\n        DefaultValue = \"*\",\n        ArgumentSource = ArgumentSource.CommandLineThenEnvironmentVariable\n    )]\n    public string DeletionPattern { get; set; }\n\n    [BooleanArgument(\n        \"dry_run\",\n        Description = \"Set to 'true' to not delete any files, this will simply print what files will be deleted.\",\n        DefaultValue = false,\n        ArgumentSource = ArgumentSource.CommandLineThenEnvironmentVariable\n    )]\n    public bool DryRun { get; set; }\n}\n```\n\nNow in your Task, instead of having to parse and validate several arguments, ArgumentBinder takes care of all of that for you.  You just need to do this in your task:\n\n```C#\nTask( \"delete_files\" )\n.Does(\n    ( context ) =\u003e\n    {\n        DeleteHelpersConfig config = CreateFromArguments\u003cDeleteHelpersConfig\u003e();\n        DeleteHelpers.DeleteFiles( context, config );\n    }\n)\n.DescriptionFromArguments\u003cDeleteHelpersConfig\u003e( \"Deletes specified files.\" );\n```\n\nNow, if you type ```cake --showdescription``` on the command line, you will see a print out of the task and\nall the arguments:\n\n```\nTask                          Description\n================================================================================\ndelete_files                  Deletes specified files.\n- Arguments:\n         --path\n                The path to delete from.\n                Type: String\n                This argument is Required.\n\n         --num_to_keep\n                The number of the most recent files/directories to keep that match the pattern.\n                Type: Integer\n                Default Value: 0\n                Minimum Value: 0\n                Maximum Value: 255\n\n         --pattern\n                The glob pattern to delete files/directories from.\n                Type: String\n                Defaulted to: *\n\n         --dry_run\n                Set to 'true' to not delete any files, this will simply print what files will be deleted.\n                Type: Boolean\n                Default Value: False\n```\n\nIf you do not specify the required arguments, you will get a helpful error message:\n\n```\ncake --target=delete_files\n\n========================================\ndelete_files\n========================================\nAn error occurred when executing task 'delete_files'.\nError: One or more errors occurred.\n        Argument 'path' is required, but was never specified.\n```\n\nIf you specify an argument that is not valid, you'll also get a helpful error message (in this example, going below the minimum for files to keep):\n\n```\ncake --target=delete_files --path=\".\" --dry_run=true --num_to_keep=-1\n\n========================================\ndelete_files\n========================================\nAn error occurred when executing task 'delete_files'.\nError: One or more errors occurred.\n        Value specified in argument 'num_to_keep' is less than the minimum value of '0'.\n```\n\nYou can also specify the source of the argument by setting the \"ArgumentSource\" property.  There are four options:\n* CommandLine (default) - The value comes from a command-line argument.\n* EnvironmentVariable - The value comes from an environment variable.\n* CommandLineThenEnvironmentVariable - The value first comes from the command-line, but falls back to an environment variable\n                                       if a command-line argument of the given name is not specified.\n* EnvironmentVariableThenCommandLine - The value first comes from an environment variable, but falls back to the command line\n                                       if an environment variable of the given name is not specified.\n\nBest Practices\n--------\n* Keep your \"Configuration\" classes separate from classes/functions that perform any action. \n  Not only does this keep a separation of concerns, but it is consistent with what Cake does.\n\nTroubleShooting\n--------\n* Ensure that in the classes that you are binding arguments to, you _may_ need to include the using statement ```using Cake.ArgumentBinder;```,\n  otherwise the compiler won't be able to find the attributes, and produce a lot of errors.\n\nDiscussion\n--------\nFor questions and to discuss ideas \u0026 feature requests, use the [GitHub discussions on the Cake GitHub repository](https://github.com/cake-build/cake/discussions), under the [Extension Q\u0026A](https://github.com/cake-build/cake/discussions/categories/extension-q-a) category.\n\n[![Join in the discussion on the Cake repository](https://img.shields.io/badge/GitHub-Discussions-green?logo=github)](https://github.com/cake-build/cake/discussions)\n\nVersioning\n--------\nCake.ArgumentBinder's versioning isn't quite semantic verioning.  Rather, the major version (x in x.y.z) is the version of cake the library was compiled against.  For example, if Cake.ArgumentBinder has a dependency on Cake version 2.0.0, the Cake.ArgumentBinder's version would be 2.y.z.  The minor version (y in x.y.z) will be incremented when a new feature gets added.  The patch (z in x.y.z) will be incremented when a bug fix happens.  We'll try to keep breaking backwards compatibility  to a minimum, and only when a new major version of Cake is released.\n\nLicense\n--------\nTo be consistent with Cake itself, Cake.ArgumentBinder is released under the MIT license.  This includes the examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcake-contrib%2Fcake.argumentbinder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcake-contrib%2Fcake.argumentbinder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcake-contrib%2Fcake.argumentbinder/lists"}