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 cmds: HashMap<String, Command>,
    pub opts: HashMap<String, Argument>,
    pub va_args: Vec<String>,
    pub active_cmd: Option<Command>,
}

Fields§

§program_name: String§description: String§help: String§usage: String§examples: Vec<String>§cmds: HashMap<String, Command>§opts: HashMap<String, Argument>§va_args: Vec<String>§active_cmd: Option<Command>

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");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
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 automatically derived 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");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
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");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
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");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
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");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
Source

pub fn define_command( &mut self, name: &'static str, description: &'static str, ) -> CmdHandle

Define a command

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");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
Source

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

Define a boolean option

Examples found in repository?
examples/demo.rs (line 18)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
Source

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

Define a numerical option

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");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
Source

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

Define a text option

Examples found in repository?
examples/demo.rs (line 16)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
Source

pub fn get_option<T: FromValue>(&self, opt_handle: OptHandle<T>) -> T

Get the option’s value from the stored handle

Examples found in repository?
examples/demo.rs (line 25)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
Source

pub fn command(&self) -> CmdHandle

Get the active command handle CmdHandle::NONE is returned if no command is set Example:

    if args.command() == version {
        println!("Version: 1.2.3.4");
    }
Examples found in repository?
examples/demo.rs (line 29)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
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) = args.parse_arguments() {
       eprintln!("Error: {e}");
       return ExitCode::FAILURE;
   }
Examples found in repository?
examples/demo.rs (line 20)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
Source

pub fn parse_arguments_from_vec( &mut self, args: Vec<String>, ) -> Result<(), Error>

Parse arguments from a provided vector of Strings

Source

pub fn was_option_set<T>(&self, arg_handle: OptHandle<T>) -> bool

Find if an argument was explicitly set by the user

Source

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

Retrieve the rest of input va args

Examples found in repository?
examples/demo.rs (line 34)
4fn main() -> ExitCode {
5    let mut args = TinyArgs::new();
6
7    // Optional help definitions:
8    args.define_help_program_name("demo");
9    args.define_help_description("A demo program for TinyArgs");
10    args.define_help_usage("[OPTIONS] [COMMAND] [ARGS]...");
11    args.define_help_example("--name=test some/path/  - Sets some values");
12
13    let list = args.define_command("list", "List vargs");
14    let version = args.define_command("version", "Display version");
15
16    let name = args.define_option_txt("name", "", "test", "A name of something");
17    let context = args.define_option_num("context", "c", 4, "Context lines");
18    let verbose = args.define_option_bool("verbose", "v", false, "Verbose mode");
19
20    if let Err(e) = args.parse_arguments() {
21        eprintln!("Error: {e}");
22        return ExitCode::FAILURE;
23    }
24
25    println!("name: {}", args.get_option(name));
26    println!("context: {}", args.get_option(context));
27    println!("verbose: {}", args.get_option(verbose));
28
29    if args.command() == version {
30        println!("Version: 1.2.3.4");
31    }
32
33    if args.command() == list {
34        for arg in args.get_va_args() {
35            println!("{arg}");
36        }
37    }
38
39    ExitCode::SUCCESS
40}
Source

pub fn get_help_text(&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.