1 Star 0 Fork 0

tickbh/Commander

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Commander by tickbh

The complete solution for Rust command-line interfaces.

Build Status

How to install it?

Add this to the Cargo.toml file of your project

[dependencies]
commander = "0.1"

How to use it?

extern crate commander;
use commander::Commander;

How to get correct build time

the build time is the commander build time, if you publish release version, you can exec

cargo clean -p commander

first, that you will got right build time

Option parsing

Options with commander are defined with the .option(),.option_str(),.option_int(),.option_float(),.option_list() method. The example below parses args and options from std::env::args() or Vec<String>, leaving remaining args can get by func .get(), .get_str(), .get_int(), .get_float(), .get_list().

extern crate commander;
use commander::Commander;

fn main() {
    let command = Commander::new()
                .version(&env!("CARGO_PKG_VERSION").to_string())
                .usage("test")
                .usage_desc("Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.")
                .option_list("-l, --list [value]", "list", Some(vec!["a".to_string(), "b".to_string(), "c".to_string()]))
                .option_int("--enum [value]", "enum", None)
                .option_int("-d, --debug [value]", "debug", Some(123))
                .option_str("-c, --copy [value]", "copy content", Some("source".to_string()))
                .option("-r", "enable recursive", None)
                .parse_env_or_exit()
                ;
    
    if let Some(s) = command.get_str("c") {
        println!("arg c = {}", s);
    }

    if let Some(s) = command.get_str("copy") {
        println!("arg copy = {}", s);
    }

    if let Some(d) = command.get_int("d") {
        println!("arg d = {}", d);
    }

    if let Some(e) = command.get_int("enum") {
        println!("arg enum = {}", e);
    }

    if let Some(l) = command.get_list("list") {
        println!("arg list = {}", l);
    }

    if let Some(r) = command.get("r") {
        println!("arg r = {}", r);
    }
}

The output info if you build bin is test. follow examples show.

  1. ./test -c xxxx -d
arg c = xxxx
arg copy = xxxx
arg d = 123
//arg c and copy is the same arg, and we has d param but we not set the value, it read from default:123
  1. ./test -h #it will show help then exit the program
Usage:./test test
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Options:
  -v, --version               Show the bin version and build time
  -h, --help                  Show this help message and exit
  -l, --list [value]          list		 default:a, b, c
      --enum [value]          enum
  -d, --debug [value]         debug		 default:123
  -c, --copy [value]          拷贝内容		 default:aaa
  -r                          enable recursive
  1. ./test -v #it will show version and show build time then exit the program
Version:0.1.1
Build Time:2017-04-29T14:11:24+08:00
  1. ./test --enum aa -r --list aa bb cc #we provide enum and list arg
arg list = ["aa", "bb", "cc"]
arg r = true
// we has the arg enum, but the enum is not a vaild int, so convert failed, and we provide arg r, so r is true

Contributing

Contributions are welcome!

The MIT License (MIT) Copyright (c) 2017 tickbh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

rust Commander 展开 收起
Rust
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Rust
1
https://gitee.com/tickbh/commander.git
git@gitee.com:tickbh/commander.git
tickbh
commander
Commander
master

搜索帮助

Cb406eda 1850385 E526c682 1850385