Next-generation, type-safe CLI parser for Rust

Related tags

Command-line cli
Overview

cli-compose

Next-generation, type-safe CLI parser for Rust

clap クレートの代替となることを目指して開発を進めています。

まだ初期段階なので、未実装あるいは動作が不安定な部分が含まれていることをご了承ください。

開発目標

以下のように動作することを目指しています。

ディレクトリ構造

project
├ opts
│ ├ src
│ │ └ lib.rs
│ └ Cargo.toml
├ src
│ └ main.rs
├ build.rs
└ Cargo.toml

Cargo.toml

[workspace]
members = [ "opts" ]

[package]
name = "cli-example"
edition = "2021"

[dependencies]
opts = { path = "./opts" }

[dependencies.cli-compose]
git = "https://github.com/0918nobita/cli-compose"
package = "cli-compose"
features = [ "runtime" ]

[build-dependencies.cli-compose]
git = "https://github.com/0918nobita/cli-compose"
package = "cli-compose"
features = [ "codegen" ]

opts/Cargo.toml

[package]
name = "opts"
edition = "2021"

[dependencies.cli-compose]
git = "https://github.com/0918nobita/cli-compose"
package = "cli-compose"
features = [ "schema" ]

opts/src/lib.rs

Self { InputFormat::Json } } /// 出力するファイルのパス #[derive(Debug, ArgOpt)] #[arg_opt(short = 'o')] // 短縮名の指定 pub struct Output(String); /// 標準出力に出力する #[derive(Debug, Opt)] #[opt(long = "stdout")] pub struct StdoutOpt; /// 出力関連の設定 #[derive(SingleSelect)] pub enum OutputGroup { Output(Output), StdoutOpt(StdoutOpt), } #[derive(Opt)] pub struct Verbose;">
use cli_compose::schema::{ArgOpt, FromKebabStr, SingleSelect, Opt, PosArg};

// ドキュメンテーションコメントはヘルプメッセージとして扱われます

/// ソースファイルのパス
#[derive(Debug, PosArg)]
pub struct Input(String);

/// ソースコードを標準入力から読み込む
#[derive(Debug, Opt)]
#[opt(long = "stdin")] // オプション名の上書き
pub struct StdinOpt;

/// 入力関連の設定
#[derive(SingleSelect)]
pub enum InputGroup {
    Input(Input),
    StdinOpt(StdinOpt),
}

/// ソースファイルの形式
#[derive(Debug, ArgOpt, FromKebabStr)]
#[arg_opt(use_default)]
pub enum InputFormat {
    Json,
    Yaml,
}

impl Default for InputFormat {
    fn default() -> Self {
        InputFormat::Json
    }
}

/// 出力するファイルのパス
#[derive(Debug, ArgOpt)]
#[arg_opt(short = 'o')] // 短縮名の指定
pub struct Output(String);

/// 標準出力に出力する
#[derive(Debug, Opt)]
#[opt(long = "stdout")]
pub struct StdoutOpt;

/// 出力関連の設定
#[derive(SingleSelect)]
pub enum OutputGroup {
    Output(Output),
    StdoutOpt(StdoutOpt),
}

#[derive(Opt)]
pub struct Verbose;

build.rs

use cli_compose::codegen::define_cli;

fn main() {
    define_cli! {
        Cli,

        version = from_crate,

        description = from_crate,

        members = {
            input = opts::InputGroup,

            // input_format = opts::InputFormat は↓のように略記できる
            opts::InputFormat,

            output = opts::OutputGroup,

            opts::Verbose,
        },
    }
}

/*
// define_cli! マクロが $OUT_DIR/cli_compose/cli.rs を生成する
struct Cli {
    input: opts::InputGroup,
    input_format: opts::InputFormat,
    output: opts::OutputGroup,
    verbose: Option
   
    ,
   
}

impl Cli {
    fn parse(args: impl Iterator
   
    ) -> Self {
   
        ...
    }
}
*/

src/main.rs

use cli_compose::runtime::use_cli;

// $OUT_DIR/cli_compose/cli.rs を include する
use_cli!(Cli);

pub fn main() {
    let cli = Cli::parse(std::env::args());

    println!("Input: {:?}", cli.input);
    println!("InputFormat: {:?}", cli.input_format);
    println!("Output: {:?}", cli.output);
    println!("Verbose: {:?}", cli.verbose);
}

実行時の挙動

$ cargo run -- --verbose --stdin --stdout

Input: Stdin(StdinOpt)
InputFormat: Json
Output: Stdout(StdoutOpt)
Verbose: Some(Verbose)

$ cargo run -- --input-format yaml -o output.txt input.yaml

Input: File("input.txt")
InputFormat: Yaml
Output: File("output.txt")
Verbose: None

サンプルの実行方法

cargo run -p playground
Comments
  • Update Rust crate syn to 1.0.98

    Update Rust crate syn to 1.0.98

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | syn | dependencies | patch | 1.0.96 -> 1.0.98 |


    Release Notes

    dtolnay/syn

    v1.0.98

    Compare Source

    v1.0.97

    Compare Source

    • Update examples

    Configuration

    📅 Schedule: Branch creation - "every weekend" in timezone Asia/Tokyo, Automerge - At any time (no schedule defined).

    🚦 Automerge: Enabled.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    🔕 Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Update Rust crate quote to 1.0.19

    Update Rust crate quote to 1.0.19

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | quote | dependencies | patch | 1.0.18 -> 1.0.19 |


    Release Notes

    dtolnay/quote

    v1.0.19

    Compare Source

    • Improve the way rustc's -Zunpretty=expanded renders quote's expanded code (#​221)

    Configuration

    📅 Schedule: Branch creation - "every weekend" in timezone Asia/Tokyo, Automerge - At any time (no schedule defined).

    🚦 Automerge: Enabled.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    🔕 Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Update Rust crate anyhow to 1.0.58

    Update Rust crate anyhow to 1.0.58

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | anyhow | dependencies | patch | 1.0.57 -> 1.0.58 |


    Release Notes

    dtolnay/anyhow

    v1.0.58

    Compare Source

    • Fix some broken links in documentation

    Configuration

    📅 Schedule: Branch creation - "every weekend" in timezone Asia/Tokyo, Automerge - At any time (no schedule defined).

    🚦 Automerge: Enabled.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    🔕 Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Update Rust crate insta to 1.15.0

    Update Rust crate insta to 1.15.0

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | insta (source) | dev-dependencies | minor | 1.14.1 -> 1.15.0 |


    Release Notes

    mitsuhiko/insta

    v1.15.0

    Compare Source

    • Bump minimum version of Rust to 1.56.1. This was done because the used serde-yaml dependency no longer supports older versions of Rust.

    Configuration

    📅 Schedule: Branch creation - "every weekend" in timezone Asia/Tokyo, Automerge - At any time (no schedule defined).

    🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    🔕 Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Lock file maintenance

    Lock file maintenance

    Mend Renovate

    This PR contains the following updates:

    | Update | Change | |---|---| | lockFileMaintenance | All locks refreshed |

    🔧 This Pull Request updates lock files to use the latest dependency versions.


    Configuration

    📅 Schedule: Branch creation - "before 3am on Monday" in timezone Asia/Tokyo, Automerge - At any time (no schedule defined).

    🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    👻 Immortal: This PR will be recreated if closed unmerged. Get config help if that's undesired.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Update Rust crate syn to 1.0.96

    Update Rust crate syn to 1.0.96

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | syn | dependencies | patch | 1.0.95 -> 1.0.96 |


    Release Notes

    dtolnay/syn

    v1.0.96

    Compare Source

    • Add a punct_mut() method on syn::punctuated::Pair to return Option<&mut P> (#​1183)

    Configuration

    📅 Schedule: Branch creation - "every weekend" in timezone Asia/Tokyo, Automerge - At any time (no schedule defined).

    🚦 Automerge: Enabled.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    🔕 Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Update Rust crate insta to 1.14.1

    Update Rust crate insta to 1.14.1

    Mend Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | insta (source) | dev-dependencies | patch | 1.14.0 -> 1.14.1 |


    Release Notes

    mitsuhiko/insta

    v1.14.1

    Compare Source

    • Update uuid crate to 1.0.0. (#​228)
    • Use inline block format also for strings of form "foo\n". (#​225)

    Configuration

    📅 Schedule: Branch creation - "every weekend" in timezone Asia/Tokyo, Automerge - At any time (no schedule defined).

    🚦 Automerge: Enabled.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    🔕 Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Lock file maintenance

    Lock file maintenance

    Mend Renovate

    This PR contains the following updates:

    | Update | Change | |---|---| | lockFileMaintenance | All locks refreshed |

    🔧 This Pull Request updates lock files to use the latest dependency versions.


    Configuration

    📅 Schedule: "before 3am on Monday" in timezone Asia/Tokyo.

    🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    👻 Immortal: This PR will be recreated if closed unmerged. Get config help if that's undesired.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Lock file maintenance

    Lock file maintenance

    Mend Renovate

    This PR contains the following updates:

    | Update | Change | |---|---| | lockFileMaintenance | All locks refreshed |

    🔧 This Pull Request updates lock files to use the latest dependency versions.


    Configuration

    📅 Schedule: "before 3am on Monday" in timezone Asia/Tokyo.

    🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    👻 Immortal: This PR will be recreated if closed unmerged. Get config help if that's undesired.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by Mend Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Update Rust crate syn to 1.0.95

    Update Rust crate syn to 1.0.95

    WhiteSource Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | syn | dependencies | patch | 1.0.94 -> 1.0.95 |


    Release Notes

    dtolnay/syn

    v1.0.95

    Compare Source

    • Replace unicode-xid with unicode-ident crate: https://github.com/dtolnay/unicode-ident

    Configuration

    📅 Schedule: "every weekend" in timezone Asia/Tokyo.

    🚦 Automerge: Enabled.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    🔕 Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by WhiteSource Renovate. View repository job log here.

    opened by renovate[bot] 1
  • Update Rust crate serde_dhall to 0.11.1

    Update Rust crate serde_dhall to 0.11.1

    WhiteSource Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | serde_dhall | dependencies | patch | 0.11.0 -> 0.11.1 |


    Release Notes

    Nadrieril/dhall-rust

    v0.11.1

    Compare Source


    Configuration

    📅 Schedule: "every weekend" in timezone Asia/Tokyo.

    🚦 Automerge: Enabled.

    Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

    🔕 Ignore: Close this PR and you won't be reminded about this update again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by WhiteSource Renovate. View repository job log here.

    opened by renovate[bot] 1
Owner
0918nobita
world.execute(me);
0918nobita
Next generation compute platform for the post-modern data stack

Tower CLI The Tower CLI is one of the main ways to interact with the Tower environment. You can do basically everything you need inside the Tower CLI,

tower.dev 3 Nov 25, 2024
A crate that allows you to mostly-safely cast one type into another type.

A crate that allows you to mostly-safely cast one type into another type. This is mostly useful for generic functions, e.g. pub fn foo<S>(s: S) {

Bincode 3 Sep 23, 2023
laydown is a simple CLI application to help you prepare for your next Daily Standup

laydown is a simple CLI application to help you prepare for your next Daily Standup. No longer shall your name be called on only for you to stare into the abyss while you struggle to remember what you did yesterday.

badjr13 83 Dec 26, 2022
Shiva library: Implementation in Rust of a parser and generator for documents of any type

Shiva Shiva library: Implementation in Rust of a parser and generator for documents of any type Features Common Document Model (CDM) for all document

Igumnoff Software 149 Jul 25, 2024
A language parser tool to build recursive descent top down parser.

lang-pt A language parser tool to generate recursive descent top down parser. Overview Parsers written for the languages like Javascript are often cus

Creative Forest 7 Jan 4, 2023
Turbine is a toy CLI app for converting Rails schema declarations into equivalent type declarations in other languages.

Turbine Turbine is a toy CLI app for converting Rails schema declarations into equivalent type declarations in other languages. It’s described as a to

Justin 2 Jan 21, 2022
LSD (LSDeluxe) - The next gen ls command

LSD (LSDeluxe) Table of Contents Description Screenshot Installation Configuration External Configurations Required Optional F.A.Q. Contributors Credi

Pierre Peltier 8.9k Jan 1, 2023
Safe Unix shell-like parameter expansion/variable substitution via cross-platform CLI or Rust API

Safe Unix shell-like parameter expansion/variable substitution for those who need a more powerful alternative to envsubst but don't want to resort to

Isak Wertwein 4 Oct 4, 2022
lemmy-help is a emmylua parser as well as a CLI which takes that parsed tree and converts it into vim help docs.

lemmy-help is a emmylua parser as well as a CLI which takes that parsed tree and converts it into vim help docs.

Vikas Raj 117 Jan 3, 2023
Chemical structure generation for protein sequences as SMILES string.

proteinogenic Chemical structure generation for protein sequences as SMILES string. ?? Usage This crate builds on top of purr, a crate providing primi

Martin Larralde 4 Aug 4, 2022
EVA ICS v4 is a new-generation Industrial-IoT platform for Industry-4.0 automated control systems.

EVA ICS v4 EVA ICS® v4 is a new-generation Industrial-IoT platform for Industry-4.0 automated control systems. The world-first and only Enterprise aut

EVA ICS 25 Feb 1, 2023
Tooling and library for generation, validation and verification of supply chain metadata documents and frameworks

Spector Spector is both tooling and a library for the generation, validation and verification of supply chain metadata documents and frameworks. Many

Kusari 13 May 4, 2023
🦀 OpenAPI code generation 🐷

Pig ?? OpenAPI code generation ?? Install cargo install --git [email protected]:truchi/pig.git --locked Usage ?? OpenAPI code generation ?? Usage: pig [

null 3 Sep 29, 2023
Blockoli is a high-performance tool for code indexing, embedding generation and semantic search tool for use with LLMs.

blockoli ???? Blockoli is a high-performance tool for code indexing, embedding generation and semantic search tool for use with LLMs. blockoli is buil

Asterisk 76 Jul 24, 2024
👩‍💻Type-checked JSX for Rust

This crate provides the html! macro for building fully type checked HTML documents inside your Rust code using roughly JSX compatible syntax.

axo 50 Jan 29, 2023
Dynamic, Type-Erased Key-Value Maps in Rust

Dynamic Objects in Rust Do you love Rust but are tired of being constrained by static typing when you need a map to hold values of different types? Do

Travis A. Wagner 12 Feb 25, 2024
A new type of shell

A new type of shell

Nushell Project 22.5k Jan 8, 2023
Web-based tool that allows browsing and comparing symbol and type information of Microsoft Windows binaries across different versions of the OS.

WinDiff About WinDiff is an open-source web-based tool that allows browsing and comparing symbol and type information of Microsoft Windows binaries ac

Erwan Grelet 208 Jun 15, 2023
Like HashSet but retaining INSERTION order and without `Hash` requirement on the Element type.

identified_vec A collection of unique identifiable elements which retains insertion order, inspired by Pointfree's Swift Identified Collections. Simil

Alexander Cyon 4 Dec 11, 2023