Giter VIP home page Giter VIP logo

commander-rust's People

Contributors

msdimos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

commander-rust's Issues

Add zero sub-command support?

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.

Proposal for alternative design

I would like to propose alternative API design to address some of the usability issues I have run into:

  1. Having to have 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).
  2. It is also somewhat confusing how [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:

  1. Try to match a command, if found delegate to it.
  2. If no match is found try to match command under [main] attribute (in this case default) if matches delegate to it.
  3. Otherwise just print 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)
}

Allow direct to run without any arguments

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

Unable to use direct

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`.

Add kebab-case option support

#[option(-d, --display-all, "display all")]

Code like above is invalid incommander-rust, but I think it's one of most commonly used functions.

Consider removing dependency`regex'

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.

Unable to use commander-rust with [patch.crates-io]

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.