-
Notifications
You must be signed in to change notification settings - Fork 223
Consider offering a way to specify command line arguments #371
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
Labels
enhancement
Something new the playground could do
Comments
You cannot currently. I've changed this issue to track that as a possible enhancement.
I'd pass a string or an iterator of strings to the parsing code. See how clap does something similar |
This could be a candidate for an FAQ and not really a bug or extra feature. I was able to get it working with your suggestion.
Thanks!
The only things I changed from the clap exampl<https://github.com/clap-rs/clap#quick-example>e are highlighted in bold in the code below.
------------------------------------------------------------------------------------------
extern crate clap;
use clap::{App, Arg, SubCommand};
fn main() {
let arg_vec = vec![
"my_prog",
"input_file",
"--config",
"args",
"-v",
"-v",
"-v",
"test",
];
let matches = App::new("My Super Program")
.version("1.0")
.author("Kevin K. <[email protected]>")
.about("Does awesome things")
.arg(
Arg::with_name("config")
.short("c")
.long("config")
.value_name("FILE")
.help("Sets a custom config file")
.takes_value(true),
).arg(
Arg::with_name("INPUT")
.help("Sets the input file to use")
.required(true)
.index(1),
).arg(
Arg::with_name("v")
.short("v")
.multiple(true)
.help("Sets the level of verbosity"),
).subcommand(
SubCommand::with_name("test")
.about("controls testing features")
.version("1.3")
.author("Someone E. <[email protected]>")
.arg(
Arg::with_name("debug")
.short("d")
.help("print debug information verbosely"),
),
).get_matches_from(arg_vec);
// Gets a value for config if supplied by user, or defaults to "default.conf"
let config = matches.value_of("config").unwrap_or("default.conf");
println!("Value for config: {}", config);
// Calling .unwrap() is safe here because "INPUT" is required (if "INPUT" wasn't
// required we could have used an 'if let' to conditionally get the value)
println!("Using input file: {}", matches.value_of("INPUT").unwrap());
// Vary the output based on how many times the user used the "verbose" flag
// (i.e. 'myprog -v -v -v' or 'myprog -vvv' vs 'myprog -v'
match matches.occurrences_of("v") {
0 => println!("No verbose info"),
1 => println!("Some verbose info"),
2 => println!("Tons of verbose info"),
3 | _ => println!("Don't be crazy"),
}
// You can handle information about subcommands by requesting their matches by name
// (as below), requesting just the name used, or both at the same time
if let Some(matches) = matches.subcommand_matches("test") {
if matches.is_present("debug") {
println!("Printing debug info...");
} else {
println!("Printing normally...");
}
}
// more program logic goes here...
}
------------------------------------------------------------------------------------------
From: Jake Goulding <[email protected]>
Reply-To: integer32llc/rust-playground <[email protected]>
Date: Wednesday, August 1, 2018 at 4:28 PM
To: integer32llc/rust-playground <[email protected]>
Cc: "Curtis, Darren S" <[email protected]>, Author <[email protected]>
Subject: Re: [integer32llc/rust-playground] Consider offering a way to specify standard input (#371)
supply command line arguments
You cannot currently. I've changed this issue to track that as a possible enhancement.
or environment variables
std::env::set_var<https://doc.rust-lang.org/std/env/fn.set_var.html>
test command line argument parsing
I'd pass a string or an iterator of strings to the parsing code. See how clap does something similar<https://docs.rs/clap/2.32.0/clap/struct.App.html#method.get_matches_from>
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#371 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AGRGsLBBdadullqCWX4TCBQB5I9YxS6sks5uMjlcgaJpZM4VrFLN>.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to test command line argument parsing. How do you supply command line arguments or environment variables to playground?
The text was updated successfully, but these errors were encountered: