Skip to content

Disable print arguments by default #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ Simple class to parse arguments. The Highlights are:
Example use:

```rust
extern crate argparse;

use std::collections::HashMap;

use argparse::{ArgParser, ArgType, hashmap_parser, vec_parser};
use argparse_rs::{ArgParser, ArgType, hashmap_parser, vec_parser};
const LONG_STR: &'static str = r#"Check your proxy settings or contact your network administrator to make sure the proxy server is working. If you don't believe you should be using a proxy server: Go to the Chromium menu > Settings > Show advanced settings... > Change proxy settings... and make sure your configuration is set to "no proxy" or "direct.""#;

fn main() {
Expand All @@ -39,6 +37,7 @@ fn main() {
"Is the User Chairman Mao?", ArgType::Flag);
parser.add_opt("socks", None, 's', false,
"If you wear socks that day", ArgType::Dict);
// paresr.set_debug_print(true); // This will print the arguments as they are parsed

let test_1 = "./go -l -60 -h -6001.45e-2 -n Johnny -m -f 1 2 3 4 5 -s Monday:true Friday:false".split_whitespace()
.map(|s| s.into())
Expand Down Expand Up @@ -75,13 +74,13 @@ fn main() {
Terminal Output:

```
socks:Some("Monday:true Friday:false ")
length:Some("-60")
frequencies:Some("1 2 3 4 5 ")
height:Some("-6001.45e-2")
help:Some("true")
name:Some("Johnny")
mao:Some("true")
# socks:Some("Monday:true Friday:false ")
# length:Some("-60")
# frequencies:Some("1 2 3 4 5 ")
# height:Some("-6001.45e-2")
# help:Some("true")
# name:Some("Johnny")
# mao:Some("true")
Usage: ./argparse [--socks k:v k2:v2...] [--length LENGTH] [--frequencies FREQUENCIES...] [--height HEIGHT] [--help ] [--name NAME] [--mao ]
Options:

Expand Down
12 changes: 11 additions & 1 deletion src/argparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct ArgParser {
arguments: HashMap<String, Arg>,
name: String,
done: bool,
print_args: bool,
}

/// Simple type alias to reduce typing. The return type of
Expand All @@ -83,13 +84,19 @@ impl ArgParser {
arguments: HashMap::new(),
name: name,
done: false,
print_args: false,
};

me.add_opt("help", Some("false"), 'h', false,
"Show this help message", ArgType::Flag);

me
}

/// if set true, the parser will print the arguments it has parsed
pub fn set_debug_print(&mut self, val: bool) {
self.print_args = val;
}

/// Add another option to parse.
/// # Example
Expand Down Expand Up @@ -241,7 +248,10 @@ impl ArgParser {
}

let res = ArgParseResults::new(self.name.clone(), new_args);
res.p_args();

if self.print_args {
res.p_args();
}

Ok(res)
}
Expand Down