{"id":25745986,"url":"https://github.com/cyrix126/clap_shortcuts","last_synced_at":"2025-02-26T11:29:30.102Z","repository":{"id":219297484,"uuid":"748142809","full_name":"Cyrix126/clap_shortcuts","owner":"Cyrix126","description":"library to generate args and link them to field of structs and functions.","archived":false,"fork":false,"pushed_at":"2024-02-04T14:55:58.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-02-04T16:45:27.021Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Cyrix126.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}},"created_at":"2024-01-25T11:13:19.000Z","updated_at":"2024-01-26T14:55:04.000Z","dependencies_parsed_at":"2024-02-04T16:13:51.172Z","dependency_job_id":"eb088869-d59f-4c1a-ab58-9421cca89bab","html_url":"https://github.com/Cyrix126/clap_shortcuts","commit_stats":null,"previous_names":["cyrix126/clap_shortcuts"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cyrix126%2Fclap_shortcuts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cyrix126%2Fclap_shortcuts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cyrix126%2Fclap_shortcuts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cyrix126%2Fclap_shortcuts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Cyrix126","download_url":"https://codeload.github.com/Cyrix126/clap_shortcuts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240843101,"owners_count":19866706,"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":"2025-02-26T11:29:29.279Z","updated_at":"2025-02-26T11:29:30.022Z","avatar_url":"https://github.com/Cyrix126.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CLAP_DERIVE\n\nsimple crate and derive macro to generate Clap Structs and implementation for structs of the ShortCuts trait which enable to apply a function with any parameters on a field or nested field of the struct from the command line.\n\nThe magic behind the derive trait Shortcuts:\nIt will generate:\n\n- An enum with the trait EnumValue from clap with a variant for every shortcuts.\n- A subcommand enum using the enum with EnumValue.\n- An implementation of the trait Shortcut\u003cStruct\u003e, matching the Arg and executing the function passed through on the field given with parameters given.\n\n## Usage\n\nThe derive macro takes a struct attributs composent of a tuple of 4 elements.\n- name of the shortcut that will appear in args.\n- the path of the field. Can be a field of the struct or a nested field.\n- the name of the function. to propagate errors, put the ? operator on the end of the name.\n- parameters that the function will take less the first parameter. for example like this: \"msg: \u0026str, is_true: bool\"\n\n\n\n\nThe method in which the process is happening depend of the borrow type first parameter of the function. The first parameter is considered to be the type of the field given. Because the proc macro can't read the parameters of the real function, the dev user must include the borrow type on the field path.\n\n## Example\n\n### Simple Example:\n\n```rust,ignore\nuse clap::Parser;\nuse clap_shortcuts::ShortCuts;\nuse clap_shortcuts_derive::ShortCuts;\nfn print(a: \u0026str) {\n    println!(\"{a}\")\n}\n#[derive(ShortCuts)]\n#[shortcut(values(name = \"shortcut_a\", func = \"print(\u0026self.a.as_str())\"))]\nstruct Test {\n    a: String,\n}\n#[derive(Parser)]\nstruct Cli {\n    #[clap(flatten)]\n    shortcut: ShortCutArgTest,\n}\n\nfn main() {\n    let cli = Cli::parse();\n    let test = Test {\n        a: String::from(\"ok\"),\n    };\n    if let Some(s) = cli.shortcut.shortcut_ref {\n        test.shortcut_ref(\u0026s, ()).unwrap();\n    }\n}\n```\n\nclap will help the user to enter\n\n```bash,ignore\nbinary --shortcut_ref shortcut-a\n```\nThe output for this example will be:\n\n```bash,ignore\nok\n```\n\nBehind the scene, the code generated will be:\n```rust,ignore\n            impl clap_shortcuts::ShortCuts\u003c()\u003e for Test {\n      fn shortcut_mut(\u0026mut self, shortcut: \u0026impl clap::ValueEnum, params: ()) -\u003e anyhow::Result\u003c()\u003e {\n                match \u0026shortcut {\n                    _ =\u003e anyhow::bail!(\"This shortcut variant is not mutable, use another method of the trait Shortcut\")\n                };\n                Ok(())\n            }\n      fn shortcut_ref(\u0026self, shortcut: \u0026impl clap::ValueEnum, params: ()) -\u003e anyhow::Result\u003c()\u003e {\n                match \u0026shortcut {\n                    \u0026ShortCutsTest::A =\u003e print(self.a.as_str()),\n                    _ =\u003e anyhow::bail!(\"This shortcut variant is not mutable, use another method of the trait Shortcut\")\n                }\n                Ok(())\n            }\n      fn shortcut_owned(self, shortcut: \u0026impl clap::ValueEnum, params: ()) -\u003e anyhow::Result\u003c()\u003e {\n                match \u0026shortcut {\n                    _ =\u003e anyhow::bail!(\"This shortcut variant is not mutable, use another method of the trait Shortcut\")\n                }\n                Ok(())\n            }\n        }\n\n#[derive(clap::ValueEnum, Clone)]\nenum ShortCutsTest {\n  A\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyrix126%2Fclap_shortcuts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcyrix126%2Fclap_shortcuts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyrix126%2Fclap_shortcuts/lists"}