doumanash / arg.rs Goto Github PK
View Code? Open in Web Editor NEWSimple Rust code generator for command line argument parsing
License: Other
Simple Rust code generator for command line argument parsing
License: Other
Hello,
I was excited to see an arg parser with "No dependencies", and confused to see that it does indeed have 6 dependencies as shown by cargo tree
:
prj v0.1.0 (/home/sam/prj)
└── arg v0.4.1
└── arg-derive v0.4.1 (proc-macro)
├── quote v1.0.23
│ └── proc-macro2 v1.0.51
│ └── unicode-ident v1.0.6
├── syn v1.0.109
│ ├── proc-macro2 v1.0.51 (*)
│ ├── quote v1.0.23 (*)
│ └── unicode-ident v1.0.6
└── tabwriter v1.2.1
└── unicode-width v0.1.10
I suspect these maybe compile time dependencies, and the claim should actually be "No runtime dependencies" - is it reasonable to update the README with that ?
Sorry for the pedantry BTW, there are situations where all external dependencies need to be accounted for, so I am sensitive to this distinction :)
The program will exit with an error if a default_value
is set for -n
:
╭ emma@noire-carnation
│ ~/s/T/yac/coreutils
╰─[tail]# ./tail -n -12
./tail: Unknown flag 'n' is provided
from src/tail.rs
:
#[derive(Args, Debug)]
struct Arguments {
_argv0: String,
#[arg(short = "c")]
c: Option<String>,
#[arg(short = "f")]
f: bool,
#[arg(short = "n", default_value: String = "-10")]
n: String,
#[arg(default_value = vec!["-"])]
paths: Vec<String>,
}
fn main() {
let (args, argv) = match get_args::<Arguments>() {
Ok((args, argv)) => (args, argv),
Err(err) => {
eprintln!("{}", err);
exit(EX_USAGE);
},
};
let head = match Path::new(&argv[0]).file_name() {
Some(val) => val.eq(OsStr::new("head")),
None => false,
};
if head && (args.c.is_some() || args.f) {
eprintln!("Usage: {} [options...] [file]", argv[0]);
exit(EX_USAGE);
}
let count: isize;
let opt = match args.c.is_some() {
true => args.c.unwrap(),
false => args.n,
};
match signs(&argv, opt) {
Ok(RealSignedInt::Unsigned(val)) => {
let out = val as isize;
count = -out;
},
Ok(RealSignedInt::Signed(val)) => count = val,
Err(err) => {
eprintln!("{}", err);
exit(EX_USAGE);
},
};
}
relevant function:
pub fn get_args<T: arg::Args>() -> Result<(T, Vec<String>), String> {
let argv = args().collect::<Vec<String>>();
match T::from_args(argv
.iter()
.map(String::as_str)
.collect::<Vec<&str>>()
) {
Ok(args) => Ok((args, argv)),
Err(ParseKind::Top(kind)) => {
Err(format!("{}: {}", argv[0], kind))
},
Err(ParseKind::Sub(command, kind)) => {
Err(format!("Usage: {} {} [options...]", argv[0], command))
}
}
}
When passing "-" to a command to give it stdin, the command returns:
'target/x86_64-unknown-linux-gnu…' terminated by signal SIGILL (Illegal instruction)
this command passes -n
to tail
even though it should not:
$ target/x86_64-unknown-linux-gnu/release/tail '-n' +2 Cargo.toml
[package]
name = "coreutils"
let mut args = std::env::args();
let _program_name = args.next().unwrap();
let args = ProgramArgs::from_args(args);
//let args = ProgramArgs::from_args(args.map(|arg| arg.as_str()));
It would be cool to have support for non-opt subcommands that don't have dashes before them. Would this be possible/in scope for this crate?
#[derive(Args, Debug)]
struct Arguments {
argv0: String,
#[arg(subcommand)]
get: Vec<String>,
}
and then argv0 get string1 string2 string3
gives you:
Arguments.argv0 == "argv0";
Arguments.get == [ "string1", "string2", "string3" ]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.