{"id":17057010,"url":"https://github.com/yuhr/fncmd","last_synced_at":"2026-02-28T19:30:56.856Z","repository":{"id":36963115,"uuid":"431259253","full_name":"yuhr/fncmd","owner":"yuhr","description":"Command line interface as a function.","archived":false,"fork":false,"pushed_at":"2023-05-10T08:22:36.000Z","size":88,"stargazers_count":76,"open_issues_count":3,"forks_count":3,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-10-25T05:21:26.821Z","etag":null,"topics":["cli","macro","rust"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/fncmd","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yuhr.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,"publiccode":null,"codemeta":null}},"created_at":"2021-11-23T21:31:27.000Z","updated_at":"2025-02-17T07:31:57.000Z","dependencies_parsed_at":"2024-10-31T10:03:52.213Z","dependency_job_id":"61a3fcc8-d8de-44d5-8f15-3f342908552b","html_url":"https://github.com/yuhr/fncmd","commit_stats":{"total_commits":59,"total_committers":4,"mean_commits":14.75,"dds":0.05084745762711862,"last_synced_commit":"a27a84fbcab8c968935a9f34fb626ba12fbe8c2f"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/yuhr/fncmd","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhr%2Ffncmd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhr%2Ffncmd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhr%2Ffncmd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhr%2Ffncmd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuhr","download_url":"https://codeload.github.com/yuhr/fncmd/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhr%2Ffncmd/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29948838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-28T18:42:55.706Z","status":"ssl_error","status_checked_at":"2026-02-28T18:42:48.811Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["cli","macro","rust"],"created_at":"2024-10-14T10:26:14.519Z","updated_at":"2026-02-28T19:30:56.834Z","avatar_url":"https://github.com/yuhr.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\u003cbr\u003e\u003cbr\u003e\n\n\u003cp\u003eCommand line interface as a function.\u003c/p\u003e\n\u003ch1\u003efncmd\u003c/h1\u003e\n\n```rust\n#[fncmd::fncmd] pub fn main() { println!(\"Hello, World!\"); }\n```\n\n[![crates.io](https://img.shields.io/crates/v/fncmd)](https://crates.io/crates/fncmd)\n[![docs.rs](https://img.shields.io/docsrs/fncmd)](https://docs.rs/fncmd/latest/fncmd/)\n[![License](https://img.shields.io/github/license/yuhr/fncmd)](https://github.com/yuhr/fncmd/blob/develop/LICENSE)\n\n\u003cbr\u003e\u003cbr\u003e\u003c/div\u003e\n\n`fncmd` is an opinionated command line parser frontend that wraps around [`clap`](https://crates.io/crates/clap). The functionality is mostly identical to `clap`, but provides much more automated and integrated experience.\n\n## Motivation\n\nImagine a command line program you want to create. Essentially, it can be abstracted as a simple function that takes command line options as arguments. Then there should be nothing to stop you from being able to write it *literally* as a function, without using structs or builders like today's Rustaceans do.\n\nThis concept is tremendously inspired by [`argopt`](https://crates.io/crates/argopt), I really appreciate the work. However, it still requires a bit of cumbersome code, especially for handling subcommands. `fncmd` has been rewritten from scratch to get rid of all the complexities. Dig into [Subcommands](#subcommands) section to see how we can handle it.\n\n## Installation\n\n**This crate is nightly-only**. Make sure you have set up your toolchain as nightly before using (e.g. having [`rust-toolchain`](https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file) file). You might be interested in [Why nightly](#why-nightly).\n\nTo install, run in your project directory:\n\n```sh\ncargo add fncmd\n```\n\nFor those who prefer case studies: see [`examples`](./examples).\n\n## Basics\n\nThis crate exposes just a single attribute macro, [`fncmd`], which can **only** be attached to the `main` function:\n\n```rust\n// main.rs\n\n/// Description of the command line tool\n#[fncmd::fncmd]\npub fn main(\n  /// Argument foo\n  #[opt(short, long)]\n  foo: String,\n  /// Argument bar\n  #[opt(short, long)]\n  bar: Option\u003cString\u003e,\n) {\n  println!(\"{:?} {:?}\", foo, bar);\n}\n```\n\nThat's all, and now you got a command line program with options handled by `clap`. With above code, the help message will be like below:\n\n```console\n$ crate-name --help\nDescription of the command line tool\n\nUsage: crate-name[EXE] [OPTIONS] --foo \u003cFOO\u003e\n\nOptions:\n  -f, --foo \u003cFOO\u003e  Argument foo\n  -b, --bar \u003cBAR\u003e  Argument bar\n  -h, --help       Print help\n  -V, --version    Print version\n\n```\n\nThe name and the version of your command are automatically inferred from Cargo metadata.\n\nThe usage of the `opt` attribute is almost exactly the same as the underlying [`arg` attribute](https://docs.rs/clap/4.2.4/clap/_derive/index.html#arg-attributes). They're just passed as is, except that `(long)` is implied if no configuration was provided, i.e. `#[opt]` means `#[opt(long)]`. If you want to take the argument `foo` without `--foo`, just omit `#[opt]`.\n\n## Subcommands\n\nAs you may know, in Cargo project [you can put entrypoints for additional binaries into `src/bin`](https://doc.rust-lang.org/cargo/guide/project-layout.html). If 1) their names are prefixed by `crate-name` and 2) their `main` functions are decorated with the `#[fncmd]` attribute and 3) exposed as `pub`, then those are automatically wrapped up as subcommands of the default binary target `crate-name`. Say you have the following directory structure:\n\n```plaintext\nsrc\n├── main.rs\n└── bin\n    ├── crate-name-subcommand1.rs\n    └── crate-name-subcommand2.rs\n```\n\nYou'll get the following subcommand structure:\n\n```plaintext\ncrate-name\n├── crate-name subcommand1\n└── crate-name subcommand2\n```\n\n## Multiple commands and nested subcommands\n\nActually `fncmd` doesn't have any distinction between the “default” binary and “additional” binaries. It determines subcommand structure just based on prefix structure instead. Therefore, configuring binary targets in your `Cargo.toml` should work as intended, for example:\n\n```toml\n[[bin]]\nname = \"crate-name\"\npath = \"src/clis/crate-name.rs\"\n\n[[bin]]\nname = \"another\"\npath = \"src/clis/another.rs\"\n\n[[bin]]\nname = \"another-sub\" # `pub`\npath = \"src/clis/another-sub.rs\"\n\n[[bin]]\nname = \"another-sub-subsub\" # `pub`\npath = \"src/clis/another-sub-subsub.rs\"\n\n[[bin]]\nname = \"another-orphan\" # non-`pub`\npath = \"src/clis/another-orphan.rs\"\n\n[[bin]]\nname = \"another-orphan-sub\" # `pub`\npath = \"src/clis/another-orphan-sub.rs\"\n```\n\nThis configuration yields up into these commands:\n\n```plaintext\ncrate-name\n\nanother\n└── another sub\n    └── another sub subsub\n\nanother-orphan\n└── another-orphan sub\n```\n\nNote that `another-orphan` is not contained within `another`, because it's not exposed as `pub`. As seen here, making the `main` non-`pub` is only meaningful when you want it to have a common prefix with others but not to be included by another command, so in most cases you can set `pub` without thinking.\n\nOf course the same structure can be achieved without manually editing `Cargo.toml`, by placing files into the default location:\n\n```plaintext\nsrc\n├── main.rs\n└── bin\n    ├── another.rs\n    ├── another-sub.rs\n    ├── another-sub-subsub.rs\n    ├── another-orphan.rs\n    └── another-orphan-sub.rs\n```\n\n## Use with exotic attribute macros\n\nSometimes you may want to transform the `main` function with another attribute macro such as `#[tokio::main]` and `#[async_std::main]`. In such case you have to put `#[fncmd]` at the outmost level:\n\n```rust\n/// Description of the command line tool\n#[fncmd]\n#[tokio::main]\npub async fn main(hello: String) -\u003e anyhow::Result\u003c()\u003e {\n  ...\n}\n```\n\nBut not:\n\n```rust\n/// Description of the command line tool\n#[tokio::main]\n#[fncmd]\npub async fn main(hello: String) -\u003e anyhow::Result\u003c()\u003e {\n  ...\n}\n```\n\nThis is because the macros like `#[tokio::main]` do some assertions on their own, so we need to feed them a well-mannered version of `main` function, e.g. removing parameters.\n\nPosition of the doc comment doesn't matter.\n\n## Restrictions\n\n`fncmd` won't support following features by design. That's why `fncmd` states “opinionated”.\n\n### Can't show authors in the help message\n\nShowing authors in the help will simply be a noise from general user's point of view.\n\n### Can't change the name and the version of the commands to different values\n\nChanging metadata such as `name` and `version` to different values from the ones defined in `Cargo.toml` can easily undermine maintainability and consistency of them.\n\n### Can't attach `#[fncmd]` to functions other than `main`\n\nAttaching `#[fncmd]` to arbitrary functions can lead to a bloated single file codebase, which should be avoided in general.\n\n## Why nightly\n\nThe way it automatically determines which targets are subcommands or not requires the `#[fncmd]` macro itself to know the name of the attached target, and thus the path of the file at which it has been called. This can be achieved by [`Span::source_file`](https://doc.rust-lang.org/proc_macro/struct.Span.html#method.source_file), which is behind an unstable feature flag `proc_macro_span`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuhr%2Ffncmd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuhr%2Ffncmd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuhr%2Ffncmd/lists"}