argpars 0.1.3

Dependency-less, simple yet functional Command Line Argument Parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! # argpars
//!
//! Dependency-less, simple yet functional Command Line Argument Parser
//!
//! # Usage
//!
//! Basic usage (checkout the examples/usage.rs file for more information)
//!
//! ```rust
//! let mut args: ArgsObj = Argpars::new();
//!
//! // Setting basic info about the app
//! args.help_usage = format!("Usage: {} [OPTION]... [TEST]\n", args.arguments_passed[0]);
//! args.help_name = "Test App".to_string();
//! args.help_description = "This is a test description".to_string();
//! args.help_version = "v1.0".to_string();
//!
//! // Adding arguments into the app
//! args.add_argument("--print-stuff", "display \"stuff\"");
//!
//! // This is how you execute something when no arguments were passed
//! if args.no_arguments_passed() {
//!     args.display_help_screen();
//! }
//! // This is how you ignore other arguments when the default (help, version) or wrong ones were passed
//! else if args.default_arguments_passed() || args.wrong_arguments_passed() {
//! }
//! // Here you handle the rest of the arguments
//! else {
//!     if args.passed("--print-stuff") {
//!         println!("stuff");
//!     }
//! }
//!
//! // Executing Argpars parser and exiting from the app with a return value
//! std::process::exit(args.pars());
//! ```
//!
//! # LICENSE
//!
//! This project is distributed under MIT license.

use std::collections::HashMap;

/// Returns vector of passed arguments
fn get_args() -> Vec<String> {
    std::env::args().collect::<Vec<String>>()
}

// Returns true if a vector contains given value
fn is_value_in_a_vector_str(value: &str, vector: &[String]) -> bool {
    return vector.iter().any(|a| a == value);
}

/// Argpars trait
pub trait Argpars {
    fn new() -> Self;
    fn no_arguments_passed(&self) -> bool;
    fn passed(&self, arg: &str) -> bool;
    fn add_argument(&mut self, argument: &str, description: &str);
    fn default_arguments_passed(&self) -> bool;
    fn wrong_arguments_passed(&self) -> bool;
    fn get_parameter_for(&self, arg: &str) -> &str;
    fn display_error_message(&self, err_type: &str, additional: &str);
    fn no_default_arguments(&mut self);
    fn display_help_screen(&self);
    fn add_help_section(&mut self, section: &str, content: &str);
    fn pars(&self) -> i32;
    fn lookup_update(&mut self);
}

/// ArgsObj struct
pub struct ArgsObj {
    pub arguments_passed_args: std::env::Args,
    pub arguments_passed: Vec<String>,
    pub number_of_arguments: u32,
    pub arguments: Vec<String>,
    pub default_arguments: bool,
    pub help_usage: String,
    pub help_name: String,
    pub help_description: String,
    pub help_version: String,
    pub arg_desc_vec: Vec<String>,
    pub help_sections: Vec<String>,
    pub help_sections_content: Vec<String>,
    pub passed_arguments_lookup: HashMap<String, bool>,
    pub parameters_lookup: HashMap<String, String>,
    pub last_param_ok: bool,
}

/// Implementation of Argpars for the ArgsObj struct
impl Argpars for ArgsObj {
    /// ArgsObj constructor
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// ```
    fn new() -> ArgsObj {
        ArgsObj {
            arguments_passed_args: std::env::args(),
            arguments_passed: get_args(),
            number_of_arguments: std::env::args().len() as u32,
            arguments: vec!["--help".to_string(), "--version".to_string()],
            default_arguments: true,
            help_usage: format!("Usage: {} [OPTION]...\n", get_args()[0]),
            help_name: "Default name".to_string(),
            help_description: "Default description".to_string(),
            help_version: "Default version".to_string(),
            arg_desc_vec: vec![
                "--help".to_string(),
                "\tdisplay this help and exit".to_string(),
                "--version".to_string(),
                "output version information and exit".to_string(),
            ],
            help_sections: Vec::new(),
            help_sections_content: Vec::new(),
            passed_arguments_lookup: HashMap::from([
                ("--help".to_string(), false),
                ("--version".to_string(), false),
            ]),
            parameters_lookup: HashMap::from([
                ("--help".to_string(), "".to_string()),
                ("--version".to_string(), "".to_string()),
            ]),
            last_param_ok: false,
        }
    }

    /// Function which updates lookup HashMaps such as passed_arguments_lookup or parameters_lookup
    fn lookup_update(&mut self) {
        for arg in &self.arguments {
            if self.arguments_passed.contains(arg) {
                *self.passed_arguments_lookup.get_mut(arg).unwrap() = true;
                *self.parameters_lookup.get_mut(arg).unwrap() =
                    self.get_parameter_for(arg).to_string();
            }
        }
    }

    /// Function which, when called, disables default arguments (--help, --version, ...)
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// args.no_default_arguments();
    /// ```
    fn no_default_arguments(&mut self) {
        for _ in 0..2 {
            self.arguments.remove(0);
        }
        for _ in 0..4 {
            self.arg_desc_vec.remove(0);
        }
        self.passed_arguments_lookup.remove_entry("--help");
        self.passed_arguments_lookup.remove_entry("--version");
        self.parameters_lookup.remove_entry("--help");
        self.parameters_lookup.remove_entry("--version");
        self.default_arguments = false;
    }

    /// Function returning if no arguments were passed
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// if args.no_arguments_passed() {
    ///     println!("no arguments passed");
    /// }
    /// ```
    fn no_arguments_passed(&self) -> bool {
        self.number_of_arguments == 1
    }

    /// Function which checks if an arguments was passed
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// if args.passed("--test") {
    ///     println!("--test passed");
    /// }
    /// ```
    fn passed(&self, arg: &str) -> bool {
        is_value_in_a_vector_str(arg, &self.arguments_passed)
    }

    /// Function used to add an argument into the app
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// args.add_argument("--test", "test argument");
    /// ```
    fn add_argument(&mut self, argument: &str, description: &str) {
        self.arguments.push(argument.to_string());
        self.arg_desc_vec.push(argument.to_string());
        self.arg_desc_vec.push(description.to_string());
        self.passed_arguments_lookup
            .insert(argument.to_string(), false);
        self.parameters_lookup
            .insert(argument.to_string(), "".to_string());
        self.lookup_update();
    }

    /// Function returning if default arguments were passed
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// if args.default_arguments_passed() {
    ///     println!("default arguments passed")
    /// }
    /// ```
    fn default_arguments_passed(&self) -> bool {
        self.passed("--help") || self.passed("--version")
    }

    /// Function returning if wrong (non existent) arguments / parameters were passed
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// if args.wrong_arguments_passed() {
    ///     println!("wrong arguments passed")
    /// }
    /// ```
    fn wrong_arguments_passed(&self) -> bool {
        let mut loop_end: usize = self.number_of_arguments as usize;
        if self.last_param_ok {
            loop_end -= 1;
        }
        for i in 1..loop_end {
            if self.arguments_passed[i as usize].starts_with('-') {
                if !self.arguments.contains(&self.arguments_passed[i as usize]) {
                    return true;
                }
            } else if !self
                .arguments
                .contains(&self.arguments_passed[(i - 1) as usize])
            {
                return true;
            }
        }
        false
    }

    /// Function used to retrive passed parameter to an argument
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// println!("parameter for --help: {}", args.get_parameter_for("--help"));
    /// ```
    fn get_parameter_for(&self, arg: &str) -> &str {
        let index_of_argument: usize = self.arguments_passed.iter().position(|r| r == arg).unwrap();
        let index_of_parameter: usize = index_of_argument + 1;
        if index_of_parameter < self.arguments_passed.len()
            && !self
                .arguments
                .contains(&self.arguments_passed[index_of_parameter])
        {
            return &self.arguments_passed[index_of_parameter];
        }

        ""
    }

    /// Function used to display error messages
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// args.display_error_message("no_such_option", "additional");
    /// ```
    fn display_error_message(&self, err_type: &str, additional: &str) {
        if err_type == "no_such_option" {
            eprintln!("ERROR: No such option: \'{}\'", additional);
            eprintln!(
                "Try: \'{} --help\' for more information.",
                self.arguments_passed[0]
            );
        }
    }

    /// Function used to display the help screen
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// args.display_help_screen();
    /// ```
    fn display_help_screen(&self) {
        println!("{}", self.help_usage);
        println!("Name: {}", self.help_name);
        println!("Description: {}", self.help_description);
        println!("Version: {}\n", self.help_version);
        println!("Possible options:");
        for arg in &self.arguments {
            if self.arg_desc_vec.contains(arg) {
                let desc_index: usize =
                    self.arg_desc_vec.iter().position(|a| a == arg).unwrap() + 1;
                println!("\t{}\t{}", arg, self.arg_desc_vec[desc_index]);
            } else {
                println!("\t{}", arg);
            }
        }
        if !self.help_sections.is_empty() {
            println!();
            for section in &self.help_sections {
                println!("{}", section);
                if self.help_sections_content.contains(section) {
                    let content_index: usize = self
                        .help_sections_content
                        .iter()
                        .position(|a| a == section)
                        .unwrap()
                        + 1;
                    println!("{}", self.help_sections_content[content_index]);
                }
            }
        }
    }

    /// Function used to add a section into the help screen
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// args.add_help_section("TEST SECTION:", "\tthis is a test section!\n");
    /// ```
    fn add_help_section(&mut self, section: &str, content: &str) {
        self.help_sections.push(section.to_string());
        self.help_sections_content.push(section.to_string());
        self.help_sections_content.push(content.to_string());
    }

    /// Main Argpars parser
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use argpars::*;
    ///
    /// let mut args: ArgsObj = Argpars::new();
    /// // Executing Argpars parser and exiting from the app with a return value
    /// std::process::exit(args.pars());
    /// ```
    fn pars(&self) -> i32 {
        if self.no_arguments_passed() {
            // // Displaying help screen if no arguments were passed (disabled by default):
            // self.display_help_screen();
        } else {
            let mut loop_end: usize = self.number_of_arguments as usize;
            if self.last_param_ok {
                loop_end -= 1;
            }
            for i in 1..loop_end {
                // If there is a '-' character at the beginning and it is not an known argument, throw an error
                if self.arguments_passed[i as usize].starts_with('-') {
                    if !self.arguments.contains(&self.arguments_passed[i as usize]) {
                        self.display_error_message(
                            "no_such_option",
                            &self.arguments_passed[i as usize],
                        );
                        return 1;
                    }
                }
                // If there is no '-' character at the beginning and the previous argument is now a known one, throw an error
                else if !is_value_in_a_vector_str(
                    &self.arguments_passed[(i - 1) as usize],
                    &self.arguments,
                ) {
                    self.display_error_message(
                        "no_such_option",
                        &self.arguments_passed[i as usize],
                    );
                    return 1;
                }
            }
            if self.default_arguments {
                if self.passed("--help") {
                    self.display_help_screen();
                }
                if self.passed("--version") {
                    println!("{} version: {}", self.help_name, self.help_version);
                }
            }
        }
        0
    }
}