Skip to content

Remove deprecated modes from libstd/getopts.rs #3288

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

Closed
wants to merge 1 commit into from
Closed
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
93 changes: 50 additions & 43 deletions src/libstd/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
* }
*/

#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];

import core::result::{err, ok};
import core::option;
import core::option::{Some, None};
Expand Down Expand Up @@ -93,37 +96,38 @@ enum occur { req, optional, multi, }
/// A description of a possible option
type opt = {name: name, hasarg: hasarg, occur: occur};

fn mkname(nm: ~str) -> name {
fn mkname(nm: &str) -> name {
let unm = str::from_slice(nm);
return if str::len(nm) == 1u {
short(str::char_at(nm, 0u))
} else { long(nm) };
short(str::char_at(unm, 0u))
} else { long(unm) };
}

/// Create an option that is required and takes an argument
fn reqopt(name: ~str) -> opt {
fn reqopt(name: &str) -> opt {
return {name: mkname(name), hasarg: yes, occur: req};
}

/// Create an option that is optional and takes an argument
fn optopt(name: ~str) -> opt {
fn optopt(name: &str) -> opt {
return {name: mkname(name), hasarg: yes, occur: optional};
}

/// Create an option that is optional and does not take an argument
fn optflag(name: ~str) -> opt {
fn optflag(name: &str) -> opt {
return {name: mkname(name), hasarg: no, occur: optional};
}

/// Create an option that is optional and takes an optional argument
fn optflagopt(name: ~str) -> opt {
fn optflagopt(name: &str) -> opt {
return {name: mkname(name), hasarg: maybe, occur: optional};
}

/**
* Create an option that is optional, takes an argument, and may occur
* multiple times
*/
fn optmulti(name: ~str) -> opt {
fn optmulti(name: &str) -> opt {
return {name: mkname(name), hasarg: yes, occur: multi};
}

Expand All @@ -135,18 +139,18 @@ enum optval { val(~str), given, }
*/
type matches = {opts: ~[opt], vals: ~[~[optval]], free: ~[~str]};

fn is_arg(arg: ~str) -> bool {
fn is_arg(arg: &str) -> bool {
return str::len(arg) > 1u && arg[0] == '-' as u8;
}

fn name_str(nm: name) -> ~str {
return match nm {
fn name_str(nm: &name) -> ~str {
return match *nm {
short(ch) => str::from_char(ch),
long(s) => s
};
}

fn find_opt(opts: ~[opt], nm: name) -> Option<uint> {
fn find_opt(opts: &[opt], +nm: name) -> Option<uint> {
vec::position(opts, |opt| opt.name == nm)
}

Expand All @@ -163,7 +167,7 @@ enum fail_ {
}

/// Convert a `fail_` enum into an error string
fn fail_str(f: fail_) -> ~str {
fn fail_str(+f: fail_) -> ~str {
return match f {
argument_missing(nm) => ~"Argument to option '" + nm + ~"' missing.",
unrecognized_option(nm) => ~"Unrecognized option: '" + nm + ~"'.",
Expand All @@ -188,7 +192,7 @@ type result = result::result<matches, fail_>;
* `opt_str`, etc. to interrogate results. Returns `err(fail_)` on failure.
* Use <fail_str> to get an error message.
*/
fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
fn getopts(args: &[~str], opts: &[opt]) -> result unsafe {
let n_opts = vec::len::<opt>(opts);
fn f(_x: uint) -> ~[optval] { return ~[]; }
let vals = vec::to_mut(vec::from_fn(n_opts, f));
Expand Down Expand Up @@ -261,12 +265,12 @@ fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
name_pos += 1u;
let optid = match find_opt(opts, nm) {
Some(id) => id,
None => return err(unrecognized_option(name_str(nm)))
None => return err(unrecognized_option(name_str(&nm)))
};
match opts[optid].hasarg {
no => {
if !option::is_none::<~str>(i_arg) {
return err(unexpected_argument(name_str(nm)));
return err(unexpected_argument(name_str(&nm)));
}
vec::push(vals[optid], given);
}
Expand All @@ -283,7 +287,7 @@ fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
vec::push(vals[optid],
val(option::get::<~str>(i_arg)));
} else if i + 1u == l {
return err(argument_missing(name_str(nm)));
return err(argument_missing(name_str(&nm)));
} else { i += 1u; vec::push(vals[optid], val(args[i])); }
}
}
Expand All @@ -297,42 +301,44 @@ fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
let occ = opts[i].occur;
if occ == req {
if n == 0u {
return err(option_missing(name_str(opts[i].name)));
return err(option_missing(name_str(&(opts[i].name))));
}
}
if occ != multi {
if n > 1u {
return err(option_duplicated(name_str(opts[i].name)));
return err(option_duplicated(name_str(&(opts[i].name))));
}
}
i += 1u;
}
return ok({opts: opts, vals: vec::from_mut(vals), free: free});
return ok({opts: vec::from_slice(opts),
vals: vec::from_mut(vals),
free: free});
}

fn opt_vals(m: matches, nm: ~str) -> ~[optval] {
return match find_opt(m.opts, mkname(nm)) {
Some(id) => m.vals[id],
fn opt_vals(+mm: matches, nm: &str) -> ~[optval] {
return match find_opt(mm.opts, mkname(nm)) {
Some(id) => mm.vals[id],
None => {
error!("No option '%s' defined", nm);
fail
}
};
}

fn opt_val(m: matches, nm: ~str) -> optval { return opt_vals(m, nm)[0]; }
fn opt_val(+mm: matches, nm: &str) -> optval { return opt_vals(mm, nm)[0]; }

/// Returns true if an option was matched
fn opt_present(m: matches, nm: ~str) -> bool {
return vec::len::<optval>(opt_vals(m, nm)) > 0u;
fn opt_present(+mm: matches, nm: &str) -> bool {
return vec::len::<optval>(opt_vals(mm, nm)) > 0u;
}

/// Returns true if any of several options were matched
fn opts_present(m: matches, names: ~[~str]) -> bool {
fn opts_present(+mm: matches, names: &[~str]) -> bool {
for vec::each(names) |nm| {
match find_opt(m.opts, mkname(nm)) {
match find_opt(mm.opts, mkname(nm)) {
Some(_) => return true,
_ => ()
None => ()
}
}
return false;
Expand All @@ -345,8 +351,8 @@ fn opts_present(m: matches, names: ~[~str]) -> bool {
* Fails if the option was not matched or if the match did not take an
* argument
*/
fn opt_str(m: matches, nm: ~str) -> ~str {
return match opt_val(m, nm) { val(s) => s, _ => fail };
fn opt_str(+mm: matches, nm: &str) -> ~str {
return match opt_val(mm, nm) { val(s) => s, _ => fail };
}

/**
Expand All @@ -355,9 +361,9 @@ fn opt_str(m: matches, nm: ~str) -> ~str {
* Fails if the no option was provided from the given list, or if the no such
* option took an argument
*/
fn opts_str(m: matches, names: ~[~str]) -> ~str {
fn opts_str(+mm: matches, names: &[~str]) -> ~str {
for vec::each(names) |nm| {
match opt_val(m, nm) {
match opt_val(mm, nm) {
val(s) => return s,
_ => ()
}
Expand All @@ -372,17 +378,17 @@ fn opts_str(m: matches, names: ~[~str]) -> ~str {
*
* Used when an option accepts multiple values.
*/
fn opt_strs(m: matches, nm: ~str) -> ~[~str] {
fn opt_strs(+mm: matches, nm: &str) -> ~[~str] {
let mut acc: ~[~str] = ~[];
for vec::each(opt_vals(m, nm)) |v| {
for vec::each(opt_vals(mm, nm)) |v| {
match v { val(s) => vec::push(acc, s), _ => () }
}
return acc;
}

/// Returns the string argument supplied to a matching option or none
fn opt_maybe_str(m: matches, nm: ~str) -> Option<~str> {
let vals = opt_vals(m, nm);
fn opt_maybe_str(+mm: matches, nm: &str) -> Option<~str> {
let vals = opt_vals(mm, nm);
if vec::len::<optval>(vals) == 0u { return None::<~str>; }
return match vals[0] { val(s) => Some::<~str>(s), _ => None::<~str> };
}
Expand All @@ -395,10 +401,11 @@ fn opt_maybe_str(m: matches, nm: ~str) -> Option<~str> {
* present but no argument was provided, and the argument if the option was
* present and an argument was provided.
*/
fn opt_default(m: matches, nm: ~str, def: ~str) -> Option<~str> {
let vals = opt_vals(m, nm);
fn opt_default(+mm: matches, nm: &str, def: &str) -> Option<~str> {
let vals = opt_vals(mm, nm);
if vec::len::<optval>(vals) == 0u { return None::<~str>; }
return match vals[0] { val(s) => Some::<~str>(s), _ => Some::<~str>(def) }
return match vals[0] { val(s) => Some::<~str>(s),
_ => Some::<~str>(str::from_slice(def)) }
}

#[cfg(test)]
Expand All @@ -414,7 +421,7 @@ mod tests {
unexpected_argument_,
}

fn check_fail_type(f: fail_, ft: fail_type) {
fn check_fail_type(+f: fail_, ft: fail_type) {
match f {
argument_missing(_) => assert ft == argument_missing_,
unrecognized_option(_) => assert ft == unrecognized_option_,
Expand Down Expand Up @@ -877,7 +884,7 @@ mod tests {
let opts = ~[optopt(~"e"), optopt(~"encrypt")];
let matches = match getopts(args, opts) {
result::ok(m) => m,
result::err(f) => fail
result::err(_f) => fail
};
assert opts_present(matches, ~[~"e"]);
assert opts_present(matches, ~[~"encrypt"]);
Expand All @@ -898,7 +905,7 @@ mod tests {
let opts = ~[optmulti(~"L")];
let matches = match getopts(args, opts) {
result::ok(m) => m,
result::err(f) => fail
result::err(_f) => fail
};
assert opts_present(matches, ~[~"L"]);
assert opts_str(matches, ~[~"L"]) == ~"foo";
Expand Down