{"id":22825355,"url":"https://github.com/crabdancing/dynarg","last_synced_at":"2025-03-31T00:22:34.120Z","repository":{"id":65369253,"uuid":"578165865","full_name":"crabdancing/dynarg","owner":"crabdancing","description":"simple dynamic argument handling in rust","archived":false,"fork":false,"pushed_at":"2023-01-24T04:35:07.000Z","size":102,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-13T13:09:10.749Z","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":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crabdancing.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":"2022-12-14T12:14:59.000Z","updated_at":"2022-12-14T12:15:13.000Z","dependencies_parsed_at":"2023-02-13T17:30:40.173Z","dependency_job_id":null,"html_url":"https://github.com/crabdancing/dynarg","commit_stats":null,"previous_names":["a7287/dynarg","alxpettit/dynarg"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crabdancing%2Fdynarg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crabdancing%2Fdynarg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crabdancing%2Fdynarg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crabdancing%2Fdynarg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crabdancing","download_url":"https://codeload.github.com/crabdancing/dynarg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246397387,"owners_count":20770544,"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-12-12T17:10:28.542Z","updated_at":"2025-03-31T00:22:34.092Z","avatar_url":"https://github.com/crabdancing.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dynarg\n\n\n[![Build status](https://github.com/alxpettit/dynarg/workflows/CI/badge.svg)](https://github.com/alxpettit/dynarg/actions?query=workflow%3ACI)\n[![crates.io](https://img.shields.io/crates/v/dynarg.svg)](https://crates.io/crates/dynarg)\n[![Documentation](https://docs.rs/dynarg/badge.svg)](https://docs.rs/dynarg)\n\nA simple dynamic argument system\n\nHave you ever wanted to have multiple functions with the same signature,\nbut with very different purposes and behavior?\n\n- Maybe you want to make an image editing app, with lots of tools.\n- Maybe you want to make a Rust GCODE implementation.\n- Maybe you just want to make a modular shell program and dynamically pick arguments out like fruits off a berry bush, \nwhile keeping track of which ones haven't been used.\n\nRegardless, you probably want an API that can:\n\n- Match arguments to static strings, to avoid runtime overhead,\nwhile maintaining readability.\n- Potentially handle dynamic strings if needed.\n- Handle arbitrary argument types.\n- Provide convenience functions for working with arguments \n-- e.g., wrapper functions for common types.\n\nIf any of this applies to you, this is a library to consider.\nNote that for _very_ high-performance applications, \nit might be better to roll your own custom use case with a Vec (ideally recycled!) of enum, to avoid dynamic dispatch.\n\nThis API is at this point considered stable and reasonably mature. It is `forbid_unsafe`, and written in pure Rust.\n### Basic example\n```rust\nuse dynarg::Args;\n\nfn main() {\n    // Creating Args object\n    let mut args = Args::new();\n    // Inserting a string type\n    args.insert_string(\"greeting\", String::from(\"hello world\"));\n    // Inserting an i32 type\n    args.insert_i32(\"meaning_of_life\", 42);\n    // There's a lot more types where that came from, BTW :)\n    // (In fact, you can use any type that implements `Any`,\n    // which... I think should be any?)\n    \n    // Retrieving string type\n    let out = args.get_string(\"greeting\").unwrap();\n    println!(\"{}\", out);\n    // Retrieving i32\n    let meaning_of_life = args.get_i32(\"meaning_of_life\").unwrap();\n    println!(\"The meaning of life is: {}\", meaning_of_life);\n}\n```\n\n### Tracking which argument is used\n\n\n```rust\nuse dynarg::Args;\n\nfn main() {\n    // Creating Args object\n    let mut args = Args::new();\n    // Inserting a string type\n    args.insert_string(\"greeting\", String::from(\"hello world\"));\n    // Inserting an i32 type\n    args.insert_i32(\"meaning_of_life\", 42);\n    // There's a lot more types where that came from, BTW :)\n    // (In fact, you can use any type that implements `Any`, which... I think should be any?)\n        \n    // Retrieving string type (while marking used flag!)\n    let out = args.poke_string(\"greeting\").unwrap();\n    println!(\"{}\", out);\n    // Retrieving i32 (also while marking used flag!)\n    let meaning_of_life = args.poke_i32(\"meaning_of_life\").unwrap();\n    println!(\"The meaning of life is: {}\", meaning_of_life);\n    // NOTE: the difference between poke and poke_* functions, and get and get_,\n    // is that poke marks the status as used.\n    // Note that this only exists if `used` feature is enabled for library.\n    // Explicitly marking status is used is useful for sanity checking -- e.g.\n    \n    // Checking used status of args is useful for catching\n    // what would otherwise be silent or hard-to-catch errors\n    if args.all_used() {\n        println!(\"All used! :3\");\n    } else {\n        for used_arg_name in args.iter_not_used_name() {\n            println!(\"Arg: \\\"{}\\\" not used\", used_arg_name);\n        }\n    }\n}\n```\n\n### Less basic example\n\n```rust\nuse dynarg::*;\n\n/// Where normally you'd need to have a fixed set of arguments,\n/// each of which would be roughly fixed types\n/// -- you can dynamically push arguments on the fly instead.\n/// This is useful when you need a consistent function signature\n/// for different types of functions,\n/// each needing different arguments\nfn draw(args: \u0026mut Args) {\n    if let Ok(greeting) = args.poke_string(\"greeting\") {\n        println!(\"{} world\", greeting);\n    }\n    if let Ok(arg) = args.poke::\u003cFruit\u003e(\"fruit_to_draw\") {\n        println!(\"I will draw {}!\", arg.0);\n        if let Ok(size) = args.poke::\u003cf32\u003e(\"size\") {\n            println!(\"with a size of {}\", size);\n        }\n    } else {\n        panic!(\"Nothing to draw D:\");\n    }\n}\n\n/// A custom struct as an example\nstruct Fruit\u003c'a\u003e(\u0026'a str);\n\nfn main() {\n   let mut args = Args::default();\n\n    let apple = Fruit(\"apple\");\n    // This is how you add arguments\n    args.insert(\"fruit_to_draw\", Box::new(apple));\n    args.insert(\"size\", Box::new(5.2f32));\n    let greeting = String::from(\"henlo\");\n    args.insert_string(\"greeting\", greeting);\n    draw(\u0026mut args);\n    if !args.all_used() {\n        println!(\"Warning! I didn't use all my arguments D:\");\n    }\n    // Clear all the used flags on args\n    args.reset_used_status();\n}\n```\n\n[Github available here.](https://github.com/alxpettit/dynarg)\n\nPRs welcome :)\n\n## Todo\n\n- [x] Custom type handling\n- [x] Replace `Option`s with `Result`s such that it's possible to identify whether the argument name didn't exist, or the type was wrong\n- [x] Add `snafu`\n- [x] Add convenience functions (e.g. `get_string()`, `get_int()`)\n- [x] Properly document gotchas\n- [x] Add variant without `used()` functionality.\n- [x] Add more examples\n- [ ] Benchmarks","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrabdancing%2Fdynarg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrabdancing%2Fdynarg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrabdancing%2Fdynarg/lists"}