Skip to content

Conversation

@jRimbault
Copy link
Contributor

@jRimbault jRimbault commented Jul 23, 2020

A use case I came around when I wanted the user to provide a ByteSize in my program CLI arguments and using a argument parser like structopt or argh or gumdrop.

Allows parsing a string directly into a ByteSize:

let size = "128GB".parse::<ByteSize>().unwrap();

I'm bad at writing negative test cases though.

Short example of a binary using structopt taking a size as a command line argument:

use bytesize::ByteSize;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Args {
    size: ByteSize,
}

fn main() {
    println!("{}", Args::from_args().size);
}

PS: the two 'static lifetime markers I removed because of clippy's suggestion

@orhun
Copy link

orhun commented Jul 20, 2021

In case someone is looking for something like this: https://crates.io/crates/byte-unit

e.g. https://docs.rs/byte-unit/4.0.12/byte_unit/struct.Byte.html#method.from_str

Copy link
Member

@hyunsik hyunsik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for a late review. The patch looks straightforward and good to me.

@hyunsik hyunsik merged commit 3cb1a70 into bytesize-rs:master Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants