All Projects → google → Argh

google / Argh

Licence: bsd-3-clause
Rust derive-based argument parsing optimized for code size

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Argh

Argparse
Argparse for golang. Just because `flag` sucks
Stars: ✭ 294 (-63.39%)
Mutual labels:  argument-parser
Kotlin Argparser
Easy to use and concise yet powerful and robust command line argument parsing for Kotlin
Stars: ✭ 431 (-46.33%)
Mutual labels:  argument-parser
Clipp
easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation
Stars: ✭ 687 (-14.45%)
Mutual labels:  argument-parser
Caporal.js
A full-featured framework for building command line applications (cli) with node.js
Stars: ✭ 3,279 (+308.34%)
Mutual labels:  argument-parser
Fire Hpp
Fire for C++: Create fully functional CLIs using function signatures
Stars: ✭ 395 (-50.81%)
Mutual labels:  argument-parser
Ureq
Minimal request library in rust.
Stars: ✭ 545 (-32.13%)
Mutual labels:  rust-library
Lingua Rs
👄 The most accurate natural language detection library in the Rust ecosystem, suitable for long and short text alike
Stars: ✭ 260 (-67.62%)
Mutual labels:  rust-library
Argh
Argh! A minimalist argument handler.
Stars: ✭ 752 (-6.35%)
Mutual labels:  argument-parser
Printpdf
An easy-to-use library for writing PDF in Rust
Stars: ✭ 404 (-49.69%)
Mutual labels:  rust-library
Argparse
Argument Parser for Modern C++
Stars: ✭ 680 (-15.32%)
Mutual labels:  argument-parser
Structopt
Parse command line arguments by defining a struct
Stars: ✭ 323 (-59.78%)
Mutual labels:  argument-parser
Juniper
GraphQL server library for Rust
Stars: ✭ 4,187 (+421.42%)
Mutual labels:  rust-library
Redbpf
Rust library for building and running BPF/eBPF modules
Stars: ✭ 611 (-23.91%)
Mutual labels:  rust-library
Getopt Php
A PHP library for command-line argument processing
Stars: ✭ 305 (-62.02%)
Mutual labels:  argument-parser
Quicksilver
A simple framework for 2D games on desktop and web
Stars: ✭ 710 (-11.58%)
Mutual labels:  rust-library
Gba
A crate that helps you make GBA games
Stars: ✭ 286 (-64.38%)
Mutual labels:  rust-library
Clize
CLIze: Turn Python functions into command-line interfaces
Stars: ✭ 439 (-45.33%)
Mutual labels:  argument-parser
Not Yet Awesome Rust
A curated list of Rust code and resources that do NOT exist yet, but would be beneficial to the Rust community.
Stars: ✭ 789 (-1.74%)
Mutual labels:  rust-library
Lopdf
A Rust library for PDF document manipulation.
Stars: ✭ 720 (-10.34%)
Mutual labels:  rust-library
Clap
Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.
Stars: ✭ 7,174 (+793.4%)
Mutual labels:  argument-parser

Argh

Argh is an opinionated Derive-based argument parser optimized for code size

crates.io license docs.rs Argh

Derive-based argument parsing optimized for code size and conformance to the Fuchsia commandline tools specification

The public API of this library consists primarily of the FromArgs derive and the from_env function, which can be used to produce a top-level FromArgs type from the current program's commandline arguments.

Basic Example

use argh::FromArgs;

#[derive(FromArgs)]
/// Reach new heights.
struct GoUp {
    /// whether or not to jump
    #[argh(switch, short = 'j')]
    jump: bool,

    /// how high to go
    #[argh(option)]
    height: usize,

    /// an optional nickname for the pilot
    #[argh(option)]
    pilot_nickname: Option<String>,
}

fn main() {
    let up: GoUp = argh::from_env();
}

./some_bin --help will then output the following:

Usage: cmdname [-j] --height <height> [--pilot-nickname <pilot-nickname>]

Reach new heights.

Options:
  -j, --jump        whether or not to jump
  --height          how high to go
  --pilot-nickname  an optional nickname for the pilot
  --help            display usage information

The resulting program can then be used in any of these ways:

  • ./some_bin --height 5
  • ./some_bin -j --height 5
  • ./some_bin --jump --height 5 --pilot-nickname Wes

Switches, like jump, are optional and will be set to true if provided.

Options, like height and pilot_nickname, can be either required, optional, or repeating, depending on whether they are contained in an Option or a Vec. Default values can be provided using the #[argh(default = "<your_code_here>")] attribute, and in this case an option is treated as optional.

use argh::FromArgs;

fn default_height() -> usize {
    5
}

#[derive(FromArgs)]
/// Reach new heights.
struct GoUp {
    /// an optional nickname for the pilot
    #[argh(option)]
    pilot_nickname: Option<String>,

    /// an optional height
    #[argh(option, default = "default_height()")]
    height: usize,

    /// an optional direction which is "up" by default
    #[argh(option, default = "String::from(\"only up\")")]
    direction: String,
}

fn main() {
    let up: GoUp = argh::from_env();
}

Custom option types can be deserialized so long as they implement the FromArgValue trait (automatically implemented for all FromStr types). If more customized parsing is required, you can supply a custom fn(&str) -> Result<T, String> using the from_str_fn attribute:

use argh::FromArgs;

#[derive(FromArgs)]
/// Goofy thing.
struct FiveStruct {
    /// always five
    #[argh(option, from_str_fn(always_five))]
    five: usize,
}

fn always_five(_value: &str) -> Result<usize, String> {
    Ok(5)
}

Positional arguments can be declared using #[argh(positional)]. These arguments will be parsed in order of their declaration in the structure:

use argh::FromArgs;

#[derive(FromArgs, PartialEq, Debug)]
/// A command with positional arguments.
struct WithPositional {
    #[argh(positional)]
    first: String,
}

The last positional argument may include a default, or be wrapped in Option or Vec to indicate an optional or repeating positional argument.

Subcommands are also supported. To use a subcommand, declare a separate FromArgs type for each subcommand as well as an enum that cases over each command:

use argh::FromArgs;

#[derive(FromArgs, PartialEq, Debug)]
/// Top-level command.
struct TopLevel {
    #[argh(subcommand)]
    nested: MySubCommandEnum,
}

#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
enum MySubCommandEnum {
    One(SubCommandOne),
    Two(SubCommandTwo),
}

#[derive(FromArgs, PartialEq, Debug)]
/// First subcommand.
#[argh(subcommand, name = "one")]
struct SubCommandOne {
    #[argh(option)]
    /// how many x
    x: usize,
}

#[derive(FromArgs, PartialEq, Debug)]
/// Second subcommand.
#[argh(subcommand, name = "two")]
struct SubCommandTwo {
    #[argh(switch)]
    /// whether to fooey
    fooey: bool,
}

NOTE: This is not an officially supported Google product.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].