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
impl TinyArgs
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a TinyArgs instance
Examples found in repository?
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}Sourcepub fn define_help_program_name(&mut self, name: &str)
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?
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}Sourcepub fn define_help_description(&mut self, description: &str)
pub fn define_help_description(&mut self, description: &str)
Define the program description for the help section
Examples found in repository?
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}Sourcepub fn define_help_usage(&mut self, usage: &str)
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?
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}Sourcepub fn define_help_example(&mut self, examples: &str)
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?
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}Sourcepub fn define_arg_bool(
&mut self,
name: &'static str,
short_name: &'static str,
default_value: bool,
description: &'static str,
) -> ArgHandle<bool>
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?
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}Sourcepub fn define_arg_num(
&mut self,
name: &'static str,
short_name: &'static str,
default_value: impl Into<f64>,
description: &'static str,
) -> ArgHandle<f64>
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?
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}Sourcepub fn define_arg_txt(
&mut self,
name: &'static str,
short_name: &'static str,
default_value: &str,
description: &'static str,
) -> ArgHandle<String>
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?
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}Sourcepub fn get<T: FromValue>(&self, arg_handle: ArgHandle<T>) -> T
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?
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}Sourcepub fn parse_arguments(&mut self) -> Result<(), Error>
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?
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}Sourcepub fn was_set<T>(&self, arg_handle: ArgHandle<T>) -> bool
pub fn was_set<T>(&self, arg_handle: ArgHandle<T>) -> bool
Find if an argument was explicitly set by the user
Sourcepub fn get_vargs(&self) -> Iter<'_, String>
pub fn get_vargs(&self) -> Iter<'_, String>
Retrieve the rest of input vargs
Examples found in repository?
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}Sourcepub fn get_help_txt(&mut self) -> &str
pub fn get_help_txt(&mut self) -> &str
Get help as str
Sourcepub fn print_help(&mut self)
pub fn print_help(&mut self)
Print the program help
Sourcepub fn print_help_and_exit(&mut self, exit_code: i32)
pub fn print_help_and_exit(&mut self, exit_code: i32)
Print the program help and exit program with code