Skip to main content

TinyArgs

Struct TinyArgs 

Source
pub struct TinyArgs {
    pub program_name: String,
    pub description: String,
    pub help: String,
    pub usage: String,
    pub examples: Vec<String>,
    pub args: HashMap<String, Argument>,
    pub vargs: Vec<String>,
}

Fields§

§program_name: String§description: String§help: String§usage: String§examples: Vec<String>§args: HashMap<String, Argument>§vargs: Vec<String>

Implementations§

Source§

impl TinyArgs

Source

pub fn new() -> Self

Create a TinyArgs instance

Examples found in repository?
examples/demo.rs (line 5)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn define_help_program_name(&mut self, name: &str)

Define the program name displayed in the help section If not defined, the program name is derived automatically from the command line

Examples found in repository?
examples/demo.rs (line 8)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn define_help_description(&mut self, description: &str)

Define the program description for the help section

Examples found in repository?
examples/demo.rs (line 9)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn define_help_usage(&mut self, usage: &str)

Define program usage for the help section The program name gets automatically prefixed,

Examples found in repository?
examples/demo.rs (line 10)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn define_help_example(&mut self, examples: &str)

Define examples in for the help section You can this function multiple times to add more execution examples The program name gets automatically prefixed

Examples found in repository?
examples/demo.rs (line 11)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn define_arg_bool( &mut self, name: &'static str, short_name: &'static str, default_value: bool, description: &'static str, ) -> ArgHandle<bool>

Define a boolean argument

Examples found in repository?
examples/demo.rs (line 15)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn define_arg_num( &mut self, name: &'static str, short_name: &'static str, default_value: impl Into<f64>, description: &'static str, ) -> ArgHandle<f64>

Define a numerical argument

Examples found in repository?
examples/demo.rs (line 14)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn define_arg_txt( &mut self, name: &'static str, short_name: &'static str, default_value: &str, description: &'static str, ) -> ArgHandle<String>

Define a textual argument

Examples found in repository?
examples/demo.rs (line 13)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn get<T: FromValue>(&self, arg_handle: ArgHandle<T>) -> T

Gets the argument value from the stored handle T is known at compile time from the handle

Examples found in repository?
examples/demo.rs (line 22)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn parse_arguments(&mut self) -> Result<(), Error>

This function MUST be run for the input arguments to be processed Automatically handles the help printout if “help” or “h” is encountered Call example:


   if let Err(e) = ta.parse_arguments() {
       eprintln!("Error: {e}");
       return ExitCode::FAILURE;
   }
Examples found in repository?
examples/demo.rs (line 17)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn was_set<T>(&self, arg_handle: ArgHandle<T>) -> bool

Find if an argument was explicitly set by the user

Source

pub fn get_vargs(&self) -> Iter<'_, String>

Retrieve the rest of input vargs

Examples found in repository?
examples/demo.rs (line 30)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo_program");
9    args.define_help_description("A demo for TinyArgs");
10    args.define_help_usage("[OPTION] [PATHS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let name = args.define_arg_txt("name", "", "test", "A name of something");
14    let times = args.define_arg_num("times", "t", 22, "How many times");
15    let version = args.define_arg_bool("version", "v", false, "Display version number");
16
17    if let Err(e) = args.parse_arguments() {
18        eprintln!("Error: {e}");
19        return ExitCode::FAILURE;
20    }
21
22    if args.get(version) {
23        println!("Version: 1.2.3.4");
24    }
25
26    println!("name: {}", args.get(name));
27    println!("times: {}", args.get(times));
28
29    println!("Paths:");
30    for arg in args.get_vargs() {
31        println!("{arg}");
32    }
33
34    ExitCode::SUCCESS
35}
Source

pub fn get_help_txt(&mut self) -> &str

Get help as str

Source

pub fn print_help(&mut self)

Print the program help

Source

pub fn print_help_and_exit(&mut self, exit_code: i32)

Print the program help and exit program with code

Trait Implementations§

Source§

impl Clone for TinyArgs

Source§

fn clone(&self) -> TinyArgs

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TinyArgs

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TinyArgs

Source§

fn default() -> TinyArgs

Returns the “default value” for a type. Read more
Source§

impl PartialEq for TinyArgs

Source§

fn eq(&self, other: &TinyArgs) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for TinyArgs

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.