Skip to content

Commit 44dfcdd

Browse files
committed
Do not steal arguments from the script
If run as e.g. "rust-script <script> <arg>", the <arg> should always be passed to the script, and not be interpreted by rust-script itself. This allows e.g. a script to handle "--help" and resolves #40.
1 parent aceff54 commit 44dfcdd

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/main.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ impl BuildKind {
9696

9797
fn parse_args() -> Args {
9898
use clap::{App, Arg, ArgGroup};
99+
use std::iter::FromIterator;
99100
let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
100101
let about = r#"Compiles and runs a Rust script."#;
101102

102103
let app = App::new(consts::PROGRAM_NAME)
103104
.version(version)
104105
.setting(clap::AppSettings::TrailingVarArg)
105-
.setting(clap::AppSettings::AllowLeadingHyphen)
106106
.about(about)
107107
.arg(Arg::new("script")
108108
.index(1)
@@ -117,12 +117,7 @@ fn parse_args() -> Args {
117117
} else {
118118
&["list-templates"]
119119
})
120-
)
121-
.arg(Arg::new("script-args")
122-
.index(2)
123-
.about("Arguments for the script to execute.")
124120
.multiple_values(true)
125-
.min_values(0)
126121
)
127122
.arg(Arg::new("expr")
128123
.about("Execute <script> as a literal expression and display the result.")
@@ -213,7 +208,7 @@ fn parse_args() -> Args {
213208
.about("Generate the Cargo package, but don't compile or run it.")
214209
.long("gen-pkg-only")
215210
.requires("script")
216-
.conflicts_with_all(&["script-args", "debug", "force", "test", "bench"])
211+
.conflicts_with_all(&["debug", "force", "test", "bench"])
217212
)
218213
.arg(Arg::new("pkg_path")
219214
.about("Specify where to place the generated Cargo package.")
@@ -225,12 +220,12 @@ fn parse_args() -> Args {
225220
.arg(Arg::new("test")
226221
.about("Compile and run tests.")
227222
.long("test")
228-
.conflicts_with_all(&["bench", "debug", "script-args", "force"])
223+
.conflicts_with_all(&["bench", "debug", "force"])
229224
)
230225
.arg(Arg::new("bench")
231226
.about("Compile and run benchmarks. Requires a nightly toolchain.")
232227
.long("bench")
233-
.conflicts_with_all(&["test", "debug", "script-args", "force"])
228+
.conflicts_with_all(&["test", "debug", "force"])
234229
)
235230
.arg(Arg::new("template")
236231
.about("Specify a template to use for expression scripts.")
@@ -281,9 +276,24 @@ fn parse_args() -> Args {
281276
.unwrap_or_default()
282277
}
283278

279+
let script_and_args: Option<Vec<&str>> = m.values_of("script").map(|o| o.collect());
280+
let script;
281+
let script_args: Vec<String>;
282+
if let Some(script_and_args) = script_and_args {
283+
script = script_and_args.get(0).map(|s| s.to_string());
284+
script_args = if script_and_args.len() > 1 {
285+
Vec::from_iter(script_and_args[1..].iter().map(|s| s.to_string()))
286+
} else {
287+
Vec::new()
288+
};
289+
} else {
290+
script = None;
291+
script_args = Vec::new();
292+
}
293+
284294
Args {
285-
script: m.value_of("script").map(Into::into),
286-
script_args: owned_vec_string(m.values_of("script-args")),
295+
script,
296+
script_args,
287297
features: m.value_of("features").map(Into::into),
288298

289299
expr: m.is_present("expr"),

tests/data/same-flags.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
println!("--output--");
2+
if let Some(arg) = std::env::args().skip(1).next() {
3+
println!("Argument: {}", arg);
4+
}

tests/tests/script.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,12 @@ fn test_nightly_toolchain() {
224224
.unwrap();
225225
assert!(out.success());
226226
}
227+
228+
#[test]
229+
fn test_same_flags() {
230+
let out = rust_script!("tests/data/same-flags.rs", "--help").unwrap();
231+
scan!(out.stdout_output();
232+
("Argument: --help") => ()
233+
)
234+
.unwrap()
235+
}

0 commit comments

Comments
 (0)