mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Tweaks for consistency with other commands, added support for -u, -V and -h, refs #4
This commit is contained in:
parent
7c8db7ffb4
commit
ed8f7d015f
1 changed files with 75 additions and 63 deletions
134
env/env.rs
vendored
134
env/env.rs
vendored
|
@ -1,3 +1,16 @@
|
||||||
|
#[link(name="env", vers="1.0.0", author="LeoTestard")];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the uutils coreutils package.
|
||||||
|
*
|
||||||
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* last synced with: env (GNU coreutils) 8.13 */
|
||||||
|
|
||||||
struct options {
|
struct options {
|
||||||
ignore_env: bool,
|
ignore_env: bool,
|
||||||
null: bool,
|
null: bool,
|
||||||
|
@ -10,11 +23,11 @@ fn usage(prog: &str) {
|
||||||
println!("Usage: {:s} [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]", prog);
|
println!("Usage: {:s} [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]", prog);
|
||||||
println!("Set each NAME to VALUE in the environment and run COMMAND\n");
|
println!("Set each NAME to VALUE in the environment and run COMMAND\n");
|
||||||
println!("Possible options are:");
|
println!("Possible options are:");
|
||||||
println!(" -i, --ignore-environment\t start with an empty environment");
|
println!(" -i --ignore-environment\t start with an empty environment");
|
||||||
println!(" -0, --null \t end each output line with a 0 byte rather than newline");
|
println!(" -0 --null \t end each output line with a 0 byte rather than newline");
|
||||||
println!(" -u, --unset NAME \t remove variable from the environment");
|
println!(" -u --unset NAME \t remove variable from the environment");
|
||||||
println!(" --help \t display this help and exit");
|
println!(" -h --help \t display this help and exit");
|
||||||
println!(" --version \t output version information and exit\n");
|
println!(" -V --version \t output version information and exit\n");
|
||||||
println!("A mere - implies -i. If no COMMAND, print the resulting environment");
|
println!("A mere - implies -i. If no COMMAND, print the resulting environment");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,8 +54,6 @@ fn main() {
|
||||||
let prog = args[0].as_slice();
|
let prog = args[0].as_slice();
|
||||||
|
|
||||||
// to handle arguments the same way than GNU env, we can't use getopts
|
// to handle arguments the same way than GNU env, we can't use getopts
|
||||||
// and have to do this:
|
|
||||||
|
|
||||||
let mut opts = ~options {
|
let mut opts = ~options {
|
||||||
ignore_env: false,
|
ignore_env: false,
|
||||||
null: false,
|
null: false,
|
||||||
|
@ -78,17 +89,47 @@ fn main() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if opt.starts_with("--") {
|
||||||
|
match *opt {
|
||||||
|
~"--help" => { usage(prog); return }
|
||||||
|
~"--version" => { version(); return }
|
||||||
|
|
||||||
else {
|
~"--ignore-environment" => opts.ignore_env = true,
|
||||||
if opt.starts_with("--") {
|
~"--null" => opts.null = true,
|
||||||
match *opt {
|
~"--unset" => {
|
||||||
~"--help" => { usage(prog); return }
|
let var = iter.next();
|
||||||
~"--version" => { version(); return }
|
|
||||||
|
|
||||||
~"--ignore-environment" => opts.ignore_env = true,
|
match var {
|
||||||
~"--null" => opts.null = true,
|
None => println!("{:s}: this option requires an argument: {:s}", prog, opt.as_slice()),
|
||||||
~"--unset" => {
|
Some(s) => opts.unsets.push(s.to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {
|
||||||
|
println!("{:s}: invalid option \"{:s}\"", prog, *opt);
|
||||||
|
println!("Type \"{:s} --help\" for detailed informations", prog);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if opt.starts_with("-") {
|
||||||
|
if opt.len() == 0 {
|
||||||
|
// implies -i and stop parsing opts
|
||||||
|
wait_cmd = true;
|
||||||
|
opts.ignore_env = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut chars = opt.iter();
|
||||||
|
chars.next();
|
||||||
|
|
||||||
|
for c in chars {
|
||||||
|
// short versions of options
|
||||||
|
match c {
|
||||||
|
'h' => { usage(prog); return }
|
||||||
|
'V' => { version(); return }
|
||||||
|
'i' => opts.ignore_env = true,
|
||||||
|
'0' => opts.null = true,
|
||||||
|
'u' => {
|
||||||
let var = iter.next();
|
let var = iter.next();
|
||||||
|
|
||||||
match var {
|
match var {
|
||||||
|
@ -96,57 +137,29 @@ fn main() {
|
||||||
Some(s) => opts.unsets.push(s.to_owned())
|
Some(s) => opts.unsets.push(s.to_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
println!("{:s}: invalid option \"{:s}\"", prog, *opt);
|
println!("{:s}: illegal option -- {:c}", prog, c);
|
||||||
println!("Type \"{:s} --help\" for detailed informations", prog);
|
println!("Type \"{:s} --help\" for detailed informations", prog);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// is it a NAME=VALUE like opt ?
|
||||||
|
let mut sp = opt.splitn_iter('=', 1);
|
||||||
|
let name = sp.next();
|
||||||
|
let value = sp.next();
|
||||||
|
|
||||||
else if opt.starts_with("-") {
|
match (name, value) {
|
||||||
if opt.len() == 0 {
|
(Some(n), Some(v)) => {
|
||||||
// implies -i and stop parsing opts
|
// yes
|
||||||
|
opts.sets.push((n.into_owned(), v.into_owned()));
|
||||||
wait_cmd = true;
|
wait_cmd = true;
|
||||||
opts.ignore_env = true;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
// no, its a program-like opt
|
||||||
let mut chars = opt.iter();
|
_ => {
|
||||||
chars.next();
|
opts.program.push(opt.to_owned());
|
||||||
|
break;
|
||||||
for c in chars {
|
|
||||||
// short versions of options
|
|
||||||
match c {
|
|
||||||
'i' => opts.ignore_env = true,
|
|
||||||
'0' => opts.null = true,
|
|
||||||
_ => {
|
|
||||||
println!("{:s}: illegal option -- {:c}", prog, c);
|
|
||||||
println!("Type \"{:s} --help\" for detailed informations", prog);
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
// is it a NAME=VALUE like opt ?
|
|
||||||
let mut sp = opt.splitn_iter('=', 1);
|
|
||||||
let name = sp.next();
|
|
||||||
let value = sp.next();
|
|
||||||
|
|
||||||
match (name, value) {
|
|
||||||
(Some(n), Some(v)) => {
|
|
||||||
// yes
|
|
||||||
opts.sets.push((n.into_owned(), v.into_owned()));
|
|
||||||
wait_cmd = true;
|
|
||||||
}
|
|
||||||
// no, its a program-like opt
|
|
||||||
_ => {
|
|
||||||
opts.program.push(opt.to_owned());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,8 +167,7 @@ fn main() {
|
||||||
item = iter.next();
|
item = iter.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
// read program arguments now
|
// read program arguments
|
||||||
|
|
||||||
for opt in iter {
|
for opt in iter {
|
||||||
opts.program.push(opt.to_owned());
|
opts.program.push(opt.to_owned());
|
||||||
}
|
}
|
||||||
|
@ -183,7 +195,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
[] => {
|
[] => {
|
||||||
// no program providen
|
// no program provided
|
||||||
print_env(opts.null);
|
print_env(opts.null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue