msdimos / commander-rust Goto Github PK
View Code? Open in Web Editor NEWa more intuitive development method of CLI in Rust
License: MIT License
a more intuitive development method of CLI in Rust
License: MIT License
I've seen a lot of CLI programs these days. I found many of them support zero sub-command.
I think it's a good idea to add it support.
I would like to propose alternative API design to address some of the usability issues I have run into:
main
annotated with [entry]
which needs to callrun!();
to obtain Application
seems complicated and bit unclear. It also limits return types to ()
(Event tough Result is reasonable return value).[direct]
fits into all this and almost feels like that is what you would do in main
of no command matched the input.So I would like to propose alternative design that combine two concepts into one:
#![feature(proc_macro_hygiene)]
use commander_rust::{ option, command, Cli, main };
#[option(-c, --cn, "Chinese")]
#[option(-e, --en, "English")]
#[option(-j, --jp, "Japanese")]
#[option(-r, --ru, "Russian")]
#[option(-f, --fr, "French")]
#[option(-n, --name <name>, "Who I am?")]
#[command(hello, "Say hello")]
fn hello(cli: Cli) {
let who = cli.get_or("name", String::from("Double Dimos"));
if cli.has("cn") {
println!("你好,世界");
} else if cli.has("en") {
println!("hello, world!");
} else if cli.has("jp") {
println!("こんにちは、世界");
} else if cli.has("ru") {
println!("Здравствуй, мир");
} else if cli.has("fr") {
println!("Salut, le monde.");
}
if cli.has("peace") || !cli.has("peace") {
println!("Whether the world is peaceful or not, I still love peace.")
}
println!("I am {} ❤", who);
}
#[command(wish <who>, "best wishes")]
fn wish(who: String, cli: Cli) {
if !cli.has("peace") {
println!("Best wishes to you, from {}", who);
}
}
#[option(-p, --peace, "I love peace")]
#[main(<a> <b> [c] [d])]
fn default(a:String, b:String, cli: Cli) {
let peace = cli.has("peace");
if peace {
println!("It's peace!");
}
println!("hello! {} {}", a, b);
}
Idea here is that [main]
attribute will generates actual main function which will attempt to:
[main]
attribute (in this case default
) if matches delegate to it.help
(maybe with panic!
to ensure that generated return type does match of all other commands).This would also address #18 and #10 as shown below:
#[command(scan <path>, "Scans directory and submits all findings to knowledge-server")]
fn scan(path:String, cli: Cli) -> std::io::Result<()> {
// ...
}
#[option(-p, --port, "Port to be used by the knowledge-server (Default 8080)")]
#[command(serve, "Run server in the foreground")]
fn serve(cli: Cli) -> std::io::Result<()> {
// ...
}
#[option(-p, --port, "Port to be used by the knowledge-server (Default 8080)")]
#[main()]
fn default(cli: Cli) -> std::io::Result<()> {
serve(cli)
}
Consider following example:
#[option(-p, --port, "Port to be used by the knowledge-server (Default 8080)")]
#[direct()]
fn base(cli: Cli) -> () {
println!("Default action {:?}", cli);
}
#[entry]
fn main() {
run!();
}
Here is how it behaves:
target/debug/knowledge-server
Using `knowledge-server --help` for more help information.
Unknown usage. Using `knowledge-server --help` for more help information.
target/debug/knowledge-server --port 8080
Unknown option: --port
File from the example folder causes following errors on my machine
error[E0599]: no method named `borrow_mut` found for mutable reference `&mut std::option::Option<for<'r> fn(&'r std::vec::Vec<commander_core::raw::Raw>, commander_core::Cli)>` in the current scope
--> program/src/main.rs:39:1
|
39 | #[direct(<a> <b> [c] [d])]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ method not found in `&mut std::option::Option<for<'r> fn(&'r std::vec::Vec<commander_core::raw::Raw>, commander_core::Cli)>`
|
= help: items from traits can only be used if the trait is in scope
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
3 | use std::borrow::BorrowMut;
|
error: aborting due to previous error
For more information about this error, try `rustc --explain E0599`.
#[option(-d, --display-all, "display all")]
Code like above is invalid incommander-rust
, but I think it's one of most commonly used functions.
regex
provides a full support regular expression in Rus, it's so huge for commander-rust
.
In commander-core
, only a few functions used by it. I think it can be removed and do same things by my self?
It's good advice.
[pkg-name] -V
This wil not work well, it will display like
[pkg-name] --help
Bug found.
When you don't use Cli
, it will occur a error.
I have found the reason.
I would like to use this with async functions, but that does not because expected return type is ()
.
Here is the cargo output
cargo check
Updating git repository `https://github.com/msdimos/commander-rust`
error: failed to resolve patches for `https://github.com/rust-lang/crates.io-index`
Caused by:
failed to load source for dependency `commander-rust`
Caused by:
Unable to update https://github.com/msdimos/commander-rust
Caused by:
failed to update submodule `homepage/dist`
Caused by:
no URL configured for submodule 'homepage/dist'; class=Submodule (17)
It appears that issue with homepage/dist
submodule, which seems to point to something that no longer exists.
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.