Skip to main content

Crate clawless

Crate clawless 

Source
Expand description

§🦦 Clawless

clawless is a framework for building command-line applications with Rust. Its goal is to provide high-level building blocks and well-designed conventions so that users can focus on their applications.

The library exports a few macros that create a command-line application, parse arguments, and then call user-defined functions.

Β§Project Status

Clawless is in a very early prototyping phase and not considered ready for production use. Follow the project and check out the open issues to understand the crate’s current limitations.

Β§Usage

First of all, generate a new binary crate using cargo new --bin <name>. Inside the crate, open src/main.rs and replace the generated contents with the following snippet:

β“˜
mod commands;

clawless::main!();

Next, create src/commands.rs (or src/commands/mod.rs) to set up your commands module:

β“˜
clawless::commands!();

You can now start creating commands for your application. Commands should be defined in modules under the commands module. For example, create src/commands/greet.rs:

use clawless::prelude::*;

#[derive(Debug, Args)]
pub struct GreetArgs {
    #[arg(short, long)]
    name: String,
}

#[command]
pub async fn greet(args: GreetArgs, context: Context) -> CommandResult {
    println!("Hello, {}!", args.name);
    Ok(())
}

Don’t forget to declare the module in src/commands.rs:

β“˜
mod greet;

clawless::commands!();

You can execute the command by calling your command-line application:

cargo run -- greet --name World

Β§Organizing Commands

For larger applications, you can organize commands into nested modules. The module hierarchy naturally maps to subcommand groups:

src/
β”œβ”€β”€ main.rs
└── commands/
    β”œβ”€β”€ mod.rs
    β”œβ”€β”€ greet.rs
    └── db/
        β”œβ”€β”€ mod.rs
        β”œβ”€β”€ migrate.rs
        └── seed.rs

With this structure:

  • cargo run -- greet runs the greet command
  • cargo run -- db migrate runs the db::migrate command
  • cargo run -- db seed runs the db::seed command

Β§License

Licensed under either of

at your option.

Β§Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Re-exportsΒ§

pub use cancellation::Cancellation;

ModulesΒ§

cancellation
Cooperative shutdown
context
Context for Clawless commands
prelude
A prelude module to easily import Clawless essentials

MacrosΒ§

commands
Set up the commands module for a Clawless application
main
Initialize and run a Clawless application

StructsΒ§

Error
The Error type, a wrapper around a dynamic error type.

TraitsΒ§

ErrorContext
Trait for adding context to errors

Type AliasesΒ§

CommandResult
Result type for Clawless commands

Attribute MacrosΒ§

command
Add a command to a Clawless application