{"id":17002035,"url":"https://github.com/sagiegurari/cliparser","last_synced_at":"2025-10-28T09:03:30.362Z","repository":{"id":47727064,"uuid":"505792258","full_name":"sagiegurari/cliparser","owner":"sagiegurari","description":"Simple command line parser","archived":false,"fork":false,"pushed_at":"2024-07-29T12:51:02.000Z","size":1131,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-27T22:00:29.433Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sagiegurari.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","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":"2022-06-21T10:19:19.000Z","updated_at":"2024-12-03T01:25:00.000Z","dependencies_parsed_at":"2024-01-13T17:37:50.716Z","dependency_job_id":"5f9c4a41-ebfd-4399-bc23-a87699b46a3b","html_url":"https://github.com/sagiegurari/cliparser","commit_stats":{"total_commits":8,"total_committers":1,"mean_commits":8.0,"dds":0.0,"last_synced_commit":"d4d860123ccea2d0ec116728a82598e8a55845bc"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagiegurari%2Fcliparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagiegurari%2Fcliparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagiegurari%2Fcliparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagiegurari%2Fcliparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sagiegurari","download_url":"https://codeload.github.com/sagiegurari/cliparser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243858929,"owners_count":20359260,"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-10-14T04:26:54.318Z","updated_at":"2025-10-28T09:03:25.323Z","avatar_url":"https://github.com/sagiegurari.png","language":"Rust","readme":"# cliparser\n\n[![crates.io](https://img.shields.io/crates/v/cliparser.svg)](https://crates.io/crates/cliparser) [![CI](https://github.com/sagiegurari/cliparser/workflows/CI/badge.svg?branch=master)](https://github.com/sagiegurari/cliparser/actions) [![codecov](https://codecov.io/gh/sagiegurari/cliparser/branch/master/graph/badge.svg)](https://codecov.io/gh/sagiegurari/cliparser)\u003cbr\u003e\n[![license](https://img.shields.io/crates/l/cliparser.svg)](https://github.com/sagiegurari/cliparser/blob/master/LICENSE) [![Libraries.io for GitHub](https://img.shields.io/librariesio/github/sagiegurari/cliparser.svg)](https://libraries.io/cargo/cliparser) [![Documentation](https://docs.rs/cliparser/badge.svg)](https://docs.rs/crate/cliparser/) [![downloads](https://img.shields.io/crates/d/cliparser.svg)](https://crates.io/crates/cliparser)\u003cbr\u003e\n[![Built with cargo-make](https://sagiegurari.github.io/cargo-make/assets/badges/cargo-make.svg)](https://sagiegurari.github.io/cargo-make)\n\n\u003e Simple command line parser.\n\n* [Overview](#overview)\n* [Usage](#usage)\n* [Installation](#installation)\n* [API Documentation](https://sagiegurari.github.io/cliparser/)\n* [Contributing](.github/CONTRIBUTING.md)\n* [Release History](CHANGELOG.md)\n* [License](#license)\n\n\u003ca name=\"overview\"\u003e\u003c/a\u003e\n## Overview\nThis library provide a very simple API to parse command line arguments.\u003cbr\u003e\nIt will not cover all possible use cases in order to ensure a simple thin API.\n\n\u003ca name=\"usage\"\u003e\u003c/a\u003e\n## Usage\nThe following is a simple usage example (there are more capabilities, see API docs):\n\n\u003c!--{ \"examples/example.rs\" | lines: 1 | code: rust }--\u003e\n```rust\nuse cliparser::types::{\n    Argument, ArgumentHelp, ArgumentOccurrence, ArgumentValueType, CliSpec, CliSpecMetaInfo,\n    PositionalArgument,\n};\nuse cliparser::{help, parse, version};\nuse std::collections::{HashMap, HashSet};\n\nfn main() {\n    let mut cli_spec = CliSpec::new();\n\n    // Add meta info to support help and version text generation\n    cli_spec = cli_spec\n        .set_meta_info(Some(CliSpecMetaInfo {\n            author: Some(\"Sagie Gur-Ari\".to_string()),\n            version: Some(\"1.2.3-beta\".to_string()),\n            description: Some(\"Amazing example\".to_string()),\n            project: Some(\"example\".to_string()),\n            help_post_text: Some(\n                \"See more info at: https://github.com/sagiegurari/cargo-make\".to_string(),\n            ),\n        }))\n        // Define the prefix of the arguments.\n        // It can be a command/s (path prefix ignored) and/or a sub command/s\n        // If not defined, the parsing will start checking the arguments only.\n        // In this example, the spec defines two ways to invoke a process, either\n        // as 'makers' or as 'cargo make' and afterwards the arguments.\n        .add_command(\"makers\")\n        .add_subcommand(vec![\"cargo\", \"make\"])\n        // Positional arguments come after all the known argument keys.\n        // If the positional is None, positional arguments are not allowed.\n        // Add -- to the command line forces positional arguments and stops key\n        // based argument parsing.\n        .set_positional_argument(Some(PositionalArgument {\n            name: \"args\".to_string(),\n            help: Some(ArgumentHelp::TextAndParam(\n                \"The command line arguments\".to_string(),\n                \"ARGS\".to_string(),\n            )),\n        }))\n        // Add a 'flag' only argument which is an argument that does not accept any value.\n        // You can define multiple variations of the parameter name.\n        // For example, in this case putting --flag or -f in the command line would be parsed\n        // as the 'flag' parameter.\n        .add_argument(Argument {\n            name: \"flag\".to_string(),\n            key: vec![\"--flag\".to_string(), \"-f\".to_string()],\n            argument_occurrence: ArgumentOccurrence::Single,\n            value_type: ArgumentValueType::None,\n            default_value: None,\n            help: Some(ArgumentHelp::Text(\n                \"A flag without value example\".to_string(),\n            )),\n        })\n        // Add an argument that accepts a single value, for example -s value\n        .add_argument(Argument {\n            name: \"single\".to_string(),\n            key: vec![\"--s1\".to_string(), \"-s\".to_string()],\n            argument_occurrence: ArgumentOccurrence::Single,\n            value_type: ArgumentValueType::Single,\n            default_value: None,\n            help: Some(ArgumentHelp::Text(\n                \"A parameter with single value example\".to_string(),\n            )),\n        })\n        // Add an argument that accepts multiple values\n        .add_argument(Argument {\n            name: \"mo\".to_string(),\n            key: vec![\"--mo1\".to_string(), \"-mo2\".to_string()],\n            argument_occurrence: ArgumentOccurrence::Multiple,\n            value_type: ArgumentValueType::Single,\n            default_value: None,\n            help: Some(ArgumentHelp::Text(\n                \"A parameter with multiple values example\".to_string(),\n            )),\n        })\n        // Add an argument that can appear multiple times.\n        // Even if the value type if Single, multiple occurrences will\n        // enable the argument to collect multiple values (one for each occurrence).\n        .add_argument(Argument {\n            name: \"mv\".to_string(),\n            key: vec![\"--mv1\".to_string(), \"-mv2\".to_string()],\n            argument_occurrence: ArgumentOccurrence::Single,\n            value_type: ArgumentValueType::Multiple,\n            default_value: None,\n            help: Some(ArgumentHelp::Text(\n                \"A parameter with single value but can appear multiple times example\".to_string(),\n            )),\n        })\n        // We can define a 'default' value.\n        // In case the argument is not in the command line, we will get the default value.\n        // However, the argument names list in the parsed struct will not include this\n        // argument as it was not found. Only the argument values will contain it.\n        // This is a good way to understand that we have a value but it was not entered by the caller.\n        .add_argument(Argument {\n            name: \"default\".to_string(),\n            key: vec![\"--d1\".to_string(), \"-d\".to_string()],\n            argument_occurrence: ArgumentOccurrence::Single,\n            value_type: ArgumentValueType::Single,\n            default_value: Some(\"some default\".to_string()),\n            help: Some(ArgumentHelp::Text(\n                \"A parameter with default value example\".to_string(),\n            )),\n        });\n\n    // Parsers the given command line based on the given spec and returns the result.\n    // In case of invalid input or the provided spec does not match the command line, an error will be returned.\n    // In order to parse the process command line instead of providing it, use the parse_process.\n    // Also, if you want to provide multiple specs and have each one checked, until the first one\n    // which fits is found, use the parse_any and parse_process_any functions.\n    let result = parse(\n        \u0026vec![\n            \"cargo\", \"make\", \"-mv2\", \"4\", \"5\", \"6\", \"--mo1=1\", \"-mo2\", \"2\", \"-f\", \"-s\", \"3\",\n            \"arg1\", \"arg2\", \"-mo2\", \"arg5\",\n        ],\n        \u0026cli_spec,\n    );\n\n    // The CliParsed struct includes multiple members that define what was found\n    // arguments - A collection of all arguments found (list of names not keys).\n    // Arguments that were not found by defaulted to a given value will not be listed here.\n    // argument_values - A map of all values for arguments found.\n    // The map will exclude arguments that do not accept value but include arguments not provided\n    // on the command line but were defaulted to a given value.\n    // The map keys are the argument names (not keys) and the value is the list of all values\n    // found for all occurrences.\n    assert!(result.is_ok());\n    let cli_parsed = result.unwrap();\n    println!(\"Cli Parsed:\\n{:?}\", \u0026cli_parsed);\n\n    let mut argument_names = HashSet::new();\n    argument_names.insert(\"flag\".to_string());\n    argument_names.insert(\"single\".to_string());\n    argument_names.insert(\"mo\".to_string());\n    argument_names.insert(\"mv\".to_string());\n    argument_names.insert(\"args\".to_string());\n    let mut argument_values = HashMap::new();\n    argument_values.insert(\"single\".to_string(), vec![\"3\".to_string()]);\n    argument_values.insert(\"mo\".to_string(), vec![\"1\".to_string(), \"2\".to_string()]);\n    argument_values.insert(\n        \"mv\".to_string(),\n        vec![\"4\".to_string(), \"5\".to_string(), \"6\".to_string()],\n    );\n    argument_values.insert(\n        \"args\".to_string(),\n        vec![\n            \"arg1\".to_string(),\n            \"arg2\".to_string(),\n            \"-mo2\".to_string(),\n            \"arg5\".to_string(),\n        ],\n    );\n    argument_values.insert(\"default\".to_string(), vec![\"some default\".to_string()]);\n    assert_eq!(cli_parsed.arguments, argument_names);\n    assert_eq!(cli_parsed.argument_values, argument_values);\n\n    // generate help text\n    let help_text = help(\u0026cli_spec);\n    println!(\"{}\", help_text);\n\n    // generate version text\n    let version_text = version(\u0026cli_spec);\n    println!(\"{}\", version_text);\n}\n```\n\u003c!--{ end }--\u003e\n\n\u003ca name=\"installation\"\u003e\u003c/a\u003e\n## Installation\nIn order to use this library, just add it as a dependency:\n\n```ini\n[dependencies]\ncliparser = \"^0.1.2\"\n```\n\n## API Documentation\nSee full docs at: [API Docs](https://sagiegurari.github.io/cliparser/)\n\n## Contributing\nSee [contributing guide](.github/CONTRIBUTING.md)\n\n\u003ca name=\"history\"\u003e\u003c/a\u003e\n## Release History\n\nSee [Changelog](CHANGELOG.md)\n\n\u003ca name=\"license\"\u003e\u003c/a\u003e\n## License\nDeveloped by Sagie Gur-Ari and licensed under the Apache 2 open source license.\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Command-line"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagiegurari%2Fcliparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsagiegurari%2Fcliparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagiegurari%2Fcliparser/lists"}