{"id":26652781,"url":"https://github.com/opcoder0/flags","last_synced_at":"2025-03-25T03:58:05.521Z","repository":{"id":61144687,"uuid":"543625307","full_name":"opcoder0/flags","owner":"opcoder0","description":"Command line processing library for Rust","archived":false,"fork":false,"pushed_at":"2022-11-24T02:46:05.000Z","size":99,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-21T00:58:04.838Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/opcoder0.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-30T13:53:55.000Z","updated_at":"2024-04-21T00:58:04.839Z","dependencies_parsed_at":"2023-01-22T08:16:16.020Z","dependency_job_id":null,"html_url":"https://github.com/opcoder0/flags","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opcoder0%2Fflags","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opcoder0%2Fflags/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opcoder0%2Fflags/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opcoder0%2Fflags/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/opcoder0","download_url":"https://codeload.github.com/opcoder0/flags/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245394757,"owners_count":20608125,"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-03-25T03:58:04.945Z","updated_at":"2025-03-25T03:58:05.497Z","avatar_url":"https://github.com/opcoder0.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flags\n\nSmall and easy to use command line argument parser library for Rust.\n\n## API\n\nThe API consists of 4 main types -\n\n- `Flag` represents a commandline argument and its value.\n- `FlagSet` represents a set of flags (typically used by the application).\n- `FlagError` represents the error returned by `parse` (if any).\n- `FlagErrorKind` represents the error code in  `FlagError`.\n\nThe API allows user to define flag with any types using the same APIs.\n\n## Examples\n\nThe examples shows how basic data and user defined types can be passed in to flags.\n\n*NOTE:* If you clone this repository you can run the examples by running -\n\n```\ncargo run --example flags_basic\n```\n\nor \n\n```\ncargo run --example flags_userdefined\n```\n\n### Example with basic types\n\n```\nextern crate flags;\n\nuse flags::{Flag, FlagSet};\nuse std::env;\nuse std::process;\n\nfn main() {\n    let bflag = Flag::new(\n        Some(\"-b\"),\n        Some(\"--backup-path\"),\n        \"path to the directory that can hold the backup files\",\n        true,\n        Flag::kind::\u003cString\u003e(),\n        Some(Box::new(\"/root/backup/10102022\".to_string())),\n    );\n    let retry_flag = Flag::new(\n        Some(\"-r\"),\n        Some(\"--retry\"),\n        \"number of retry operations\",\n        false,\n        Flag::kind::\u003ci32\u003e(),\n        Some(Box::new(3i32)),\n    );\n    let mut flagset = FlagSet::new();\n    flagset.add(\u0026bflag);\n    flagset.add(\u0026retry_flag);\n    let result = flagset.parse(\u0026mut env::args());\n    match result {\n        Err(e) =\u003e {\n            println!(\"{}\", e);\t// prints the error\n            println!(\"{}\", flagset); // prints the usage\n            process::exit(1);\n        }\n        Ok(()) =\u003e {}\n    }\n    let backup_path = bflag.borrow().get_value::\u003cString\u003e().ok().unwrap(); // extracts value for the mandatory flag\n    let num_retries = retry_flag.borrow().get_value::\u003ci32\u003e().ok().unwrap(); // extracts value or default value for optional flag\n    println!(\"backup path: {}\", backup_path);\n    println!(\"number of retries: {}\", num_retries);\n}\n```\n\n### Example with user defined types\n\nMore complex types can be passed in as flags too as long as they implement `FromStr` and `Clone` traits.\n\n```\nextern crate flags;\n\nuse flags::{Flag, FlagSet};\nuse std::env;\nuse std::num::ParseIntError;\nuse std::process;\nuse std::str::FromStr;\n\n#[derive(Debug, PartialEq)]\nstruct Point {\n    x: i32,\n    y: i32,\n}\n\nimpl Clone for Point {\n    fn clone(\u0026self) -\u003e Self {\n        let x = self.x;\n        let y = self.y;\n        Self { x, y }\n    }\n}\n\nimpl ToString for Point {\n    fn to_string(\u0026self) -\u003e String {\n        format!(\"({},{})\", self.x, self.y)\n    }\n}\n\nimpl FromStr for Point {\n    type Err = ParseIntError;\n\n    fn from_str(s: \u0026str) -\u003e Result\u003cSelf, Self::Err\u003e {\n        let (x, y) = s\n            .strip_prefix('(')\n            .and_then(|s| s.strip_suffix(')'))\n            .and_then(|s| s.split_once(','))\n            .unwrap();\n\n        let x_fromstr = x.parse::\u003ci32\u003e()?;\n        let y_fromstr = y.parse::\u003ci32\u003e()?;\n\n        Ok(Point {\n            x: x_fromstr,\n            y: y_fromstr,\n        })\n    }\n}\n\nfn main() {\n    let point_flag = Flag::new(\n        Some(\"-p\"),\n        Some(\"--point\"),\n        \"point on the 2d plane\",\n        true,\n        Flag::kind::\u003cPoint\u003e(),\n        Some(Box::new(Point { x: 0, y: 0 })),\n    );\n\n    let mut flagset = FlagSet::new();\n    flagset.add(\u0026point_flag);\n    let result = flagset.parse(\u0026mut env::args());\n    match result {\n        Err(e) =\u003e {\n            println!(\"{}\", e);\n            println!(\"{}\", flagset);\n            process::exit(1);\n        }\n        Ok(()) =\u003e {}\n    }\n    let coord = point_flag.borrow().get_value::\u003cPoint\u003e().ok().unwrap();\n    println!(\"co-ordinates: {:?}\", coord);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopcoder0%2Fflags","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopcoder0%2Fflags","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopcoder0%2Fflags/lists"}