{"id":13503895,"url":"https://github.com/google/argh","last_synced_at":"2025-12-12T14:52:13.227Z","repository":{"id":37627003,"uuid":"237297802","full_name":"google/argh","owner":"google","description":"Rust derive-based argument parsing optimized for code size","archived":false,"fork":false,"pushed_at":"2025-02-04T19:37:56.000Z","size":139,"stargazers_count":1750,"open_issues_count":66,"forks_count":88,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-22T00:41:08.558Z","etag":null,"topics":["argh","argument-parser","arguments","positional-arguments","rust","rust-library","subcommands"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/google.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"code-of-conduct.md","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":"2020-01-30T20:19:33.000Z","updated_at":"2025-03-17T15:18:26.000Z","dependencies_parsed_at":"2025-02-04T20:25:06.940Z","dependency_job_id":"5ffcab0a-775e-49b7-bb71-c80e117c8c89","html_url":"https://github.com/google/argh","commit_stats":{"total_commits":90,"total_committers":29,"mean_commits":3.103448275862069,"dds":0.7,"last_synced_commit":"3f3c29726a21c4b541bb2b9aa2c592461897ded0"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fargh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fargh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fargh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fargh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/google","download_url":"https://codeload.github.com/google/argh/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246227071,"owners_count":20743880,"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":["argh","argument-parser","arguments","positional-arguments","rust","rust-library","subcommands"],"created_at":"2024-07-31T23:00:49.265Z","updated_at":"2025-12-12T14:52:13.198Z","avatar_url":"https://github.com/google.png","language":"Rust","funding_links":[],"categories":["Libraries","Rust","库 Libraries","Rust 🦀"],"sub_categories":["Command-line","命令行 Command-line"],"readme":"# Argh\n**Argh is an opinionated Derive-based argument parser optimized for code size**\n\n[![crates.io](https://img.shields.io/crates/v/argh.svg)](https://crates.io/crates/argh)\n[![license](https://img.shields.io/badge/license-BSD3.0-blue.svg)](https://github.com/google/argh/LICENSE)\n[![docs.rs](https://docs.rs/argh/badge.svg)](https://docs.rs/crate/argh/)\n![Argh](https://github.com/google/argh/workflows/Argh/badge.svg)\n\nDerive-based argument parsing optimized for code size and conformance\nto the Fuchsia commandline tools specification\n\nThe public API of this library consists primarily of the `FromArgs`\nderive and the `from_env` function, which can be used to produce\na top-level `FromArgs` type from the current program's commandline\narguments.\n\n## Basic Example\n\n```rust,no_run\nuse argh::FromArgs;\n\n#[derive(FromArgs)]\n/// Reach new heights.\nstruct GoUp {\n    /// whether or not to jump\n    #[argh(switch, short = 'j')]\n    jump: bool,\n\n    /// how high to go\n    #[argh(option)]\n    height: usize,\n\n    /// an optional nickname for the pilot\n    #[argh(option)]\n    pilot_nickname: Option\u003cString\u003e,\n}\n\nfn main() {\n    let up: GoUp = argh::from_env();\n}\n```\n\n`./some_bin --help` will then output the following:\n\n```\nUsage: cmdname [-j] --height \u003cheight\u003e [--pilot-nickname \u003cpilot-nickname\u003e]\n\nReach new heights.\n\nOptions:\n  -j, --jump        whether or not to jump\n  --height          how high to go\n  --pilot-nickname  an optional nickname for the pilot\n  --help, help      display usage information\n```\n\nThe resulting program can then be used in any of these ways:\n- `./some_bin --height 5`\n- `./some_bin -j --height 5`\n- `./some_bin --jump --height 5 --pilot-nickname Wes`\n\nSwitches, like `jump`, are optional and will be set to true if provided.\n\nOptions, like `height` and `pilot_nickname`, can be either required,\noptional, or repeating, depending on whether they are contained in an\n`Option` or a `Vec`. Default values can be provided using the\n`#[argh(default = \"\u003cyour_code_here\u003e\")]` attribute, and in this case an\noption is treated as optional.\n\n```rust\nuse argh::FromArgs;\n\nfn default_height() -\u003e usize {\n    5\n}\n\n#[derive(FromArgs)]\n/// Reach new heights.\nstruct GoUp {\n    /// an optional nickname for the pilot\n    #[argh(option)]\n    pilot_nickname: Option\u003cString\u003e,\n\n    /// an optional height\n    #[argh(option, default = \"default_height()\")]\n    height: usize,\n\n    /// an optional direction which is \"up\" by default\n    #[argh(option, default = \"String::from(\\\"only up\\\")\")]\n    direction: String,\n}\n\nfn main() {\n    let up: GoUp = argh::from_env();\n}\n```\n\nCustom option types can be deserialized so long as they implement the\n`FromArgValue` trait (automatically implemented for all `FromStr` types).\nIf more customized parsing is required, you can supply a custom\n`fn(\u0026str) -\u003e Result\u003cT, String\u003e` using the `from_str_fn` attribute:\n\n```rust\nuse argh::FromArgs;\n\n#[derive(FromArgs)]\n/// Goofy thing.\nstruct FiveStruct {\n    /// always five\n    #[argh(option, from_str_fn(always_five))]\n    five: usize,\n}\n\nfn always_five(_value: \u0026str) -\u003e Result\u003cusize, String\u003e {\n    Ok(5)\n}\n```\n\nPositional arguments can be declared using `#[argh(positional)]`.\nThese arguments will be parsed in order of their declaration in\nthe structure:\n\n```rust\nuse argh::FromArgs;\n\n#[derive(FromArgs, PartialEq, Debug)]\n/// A command with positional arguments.\nstruct WithPositional {\n    #[argh(positional)]\n    first: String,\n}\n```\n\nThe last positional argument may include a default, or be wrapped in\n`Option` or `Vec` to indicate an optional or repeating positional argument.\n\nSubcommands are also supported. To use a subcommand, declare a separate\n`FromArgs` type for each subcommand as well as an enum that cases\nover each command:\n\n```rust\nuse argh::FromArgs;\n\n#[derive(FromArgs, PartialEq, Debug)]\n/// Top-level command.\nstruct TopLevel {\n    #[argh(subcommand)]\n    nested: MySubCommandEnum,\n}\n\n#[derive(FromArgs, PartialEq, Debug)]\n#[argh(subcommand)]\nenum MySubCommandEnum {\n    One(SubCommandOne),\n    Two(SubCommandTwo),\n}\n\n#[derive(FromArgs, PartialEq, Debug)]\n/// First subcommand.\n#[argh(subcommand, name = \"one\")]\nstruct SubCommandOne {\n    #[argh(option)]\n    /// how many x\n    x: usize,\n}\n\n#[derive(FromArgs, PartialEq, Debug)]\n/// Second subcommand.\n#[argh(subcommand, name = \"two\")]\nstruct SubCommandTwo {\n    #[argh(switch)]\n    /// whether to fooey\n    fooey: bool,\n}\n```\n\nNOTE: This is not an officially supported Google product.\n\n\n## How to debug the expanded derive macro for `argh`\n\nThe `argh::FromArgs` derive macro can be debugged with the [cargo-expand](https://crates.io/crates/cargo-expand) crate.\n\n### Expand the derive macro in `examples/simple_example.rs`\n\nSee [argh/examples/simple_example.rs](./argh/examples/simple_example.rs) for the example struct we wish to expand.\n\nFirst, install `cargo-expand` by running `cargo install cargo-expand`. Note this requires the nightly build of Rust.\n\nOnce installed, run `cargo expand` with in the `argh` package and you can see the expanded code.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogle%2Fargh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoogle%2Fargh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogle%2Fargh/lists"}