Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Table of Contents

Example: CLI subcommand

1. Search and Install

# Search for CLI subcommand gpacks
ggen search rust cli

# Install the gpack
ggen add io.ggen.rust.cli-subcommand

2. Generate Code

# Generate using the gpack template
ggen gen io.ggen.rust.cli-subcommand:cli/subcommand/rust.tmpl name=status description="Show application status"

3. Verify Output

# Check generated file
cat src/cmds/status.rs

Output:

#![allow(unused)]
fn main() {
// Generated by gpack: io.ggen.rust.cli-subcommand
// Template: cli/subcommand/rust.tmpl

use clap::Parser;

#[derive(Parser)]
pub struct StatusArgs {
    /// Show application status
    #[arg(short, long)]
    pub verbose: bool,
}

pub fn status(args: StatusArgs) -> Result<(), Box<dyn std::error::Error>> {
    println!("Application status: Running");
    if args.verbose {
        println!("Detailed status information...");
    }
    Ok(())
}
}

Using Local Template (Advanced)

Create templates/cli/subcommand/rust.tmpl (see quickstart for full template).

Generate:

ggen gen cli subcommand --vars cmd=status summary="Show status"

Outputs:

src/cmds/status.rs

Comparison

ApproachSetup TimeQualityUpdatesBest For
MarketplaceInstantCommunity testedAutomaticMost users
LocalManualCustomManualSpecial needs

Next Steps