cargo-run 0.5.1

A powerful, fast, and developer-friendly CLI tool for managing project scripts in Rust. Think npm scripts, make, or just — but built specifically for the Rust ecosystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# cargo-run


<!-- prettier-ignore-start -->


[![crates.io](https://img.shields.io/crates/v/cargo-run?label=latest)](https://crates.io/crates/cargo-run)
[![Documentation](https://docs.rs/cargo-run/badge.svg)](https://docs.rs/cargo-run)
![Version](https://img.shields.io/badge/rustc-1.79+-ab6000.svg)
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/cargo-run.svg)
[![Dependency Status](https://deps.rs/crate/cargo-run/latest/status.svg)](https://deps.rs/crate/cargo-run/latest)
<br />
[![Download](https://img.shields.io/crates/d/cargo-run.svg)](https://crates.io/crates/cargo-run)

<!-- prettier-ignore-end -->


> **A powerful, fast, and developer-friendly CLI tool for managing project scripts in Rust**  
> Think `npm scripts`, `make`, or `just` — but built specifically for the Rust ecosystem with modern CLI best practices.

## Why `cargo-run`?


**Stop writing one-off shell scripts.** `cargo-run` provides a unified, type-safe way to manage all your project automation:

-   **Zero runtime dependencies** — Single binary, fast startup
-**Cross-platform** — Works on Windows, macOS, and Linux
-**Modern CLI UX** — Shell completions, dry-run mode, helpful errors
-**Powerful features** — Script chaining, environment variables, toolchain support
-**CI/CD ready** — Validation command catches errors early
-**Rust-native** — Built with Rust, for Rust projects

### Quick Comparison


| Feature                | `cargo-run` | `make` | `just` | `npm scripts` |
| ---------------------- | ----------- | ------ | ------ | ------------- |
| Zero dependencies      |||| ❌ (Node.js)  |
| Shell completions      || ⚠️     |||
| Dry-run mode           |||||
| Validation             ||| ⚠️     ||
| Toolchain support      |||||
| Environment precedence || ⚠️     | ⚠️     ||

## 📦 Installation


```sh
cargo install cargo-run
```

After installation, you'll have multiple ways to invoke the tool:

-   `cargo script`**Recommended**: Use as a Cargo subcommand (e.g., `cargo script run build`)
-   `cargo-script` — Direct binary invocation
-   `cgs` — Short alias (used in examples below for brevity)

**Note:** When installed via `cargo install`, the `cargo-script` binary is automatically available in your PATH, enabling `cargo script` subcommand usage.

## ⚡ Quick Start


1. **Initialize** a `Scripts.toml` file:

    ```sh
    # Using Cargo subcommand (recommended)

    cargo script init


    # Or using direct binary

    cgs init

    ```

2. **Run** a script:

    ```sh
    # Using Cargo subcommand

    cargo script run build


    # Or using direct binary

    cgs run build

    ```

3. **Preview** what would run (dry-run):

    ```sh
    cargo script run build --dry-run

    ```

4. **Validate** your configuration:

    ```sh
    cargo script validate

    ```

5. **Show** all available scripts:

    ```sh
    cargo script show

    ```

That's it! You're ready to go. 🎉

> **💡 Tip:** Using `cargo script` integrates seamlessly with Cargo's ecosystem and provides a familiar interface for Rust developers.

## 📚 Features


### Core Features


-   **Script Execution** — Run scripts defined in `Scripts.toml`
-   **Script Chaining** — Compose complex workflows with `include`
-   **Environment Variables** — Global, script-specific, and command-line overrides
-   **Multiple Interpreters** — bash, zsh, PowerShell, cmd, or custom
-   **Toolchain Support** — Rust toolchains via rustup, Python versions
-   **Requirements Checking** — Validate tool versions before execution

### Developer Experience


-   **Shell Completions** — Tab completion for bash, zsh, fish, and PowerShell
-   **Dry-Run Mode** — Preview execution without side effects
-   **Helpful Errors** — Actionable error messages with suggestions
-   **Validation** — Catch configuration errors early
-   **Performance Metrics** — Track script execution times

## 📖 Usage Guide


### Initialize `Scripts.toml`


Create a new `Scripts.toml` file with sensible defaults:

```sh
# Using Cargo subcommand (recommended)

cargo script init

# Or using direct binary

cgs init
```

This creates a `Scripts.toml` file with:

```toml
[global_env]

[scripts]
dev = "cargo run"
build = { command = "cargo build", env = { RUST_LOG = "info" } }
release = "cargo build --release"
test = { command = "cargo test", env = { RUST_LOG = "warn" } }
doc = "cargo doc --no-deps --open"
```

### Run Scripts


```sh
# Using Cargo subcommand (recommended)

cargo script run build

# Simple script

cgs run build

# With environment variable override

cargo script run test --env RUST_LOG=debug

# Preview execution (dry-run)

cargo script run release --dry-run
```

### Script Configuration


#### Simple Script


```toml
[scripts]
build = "cargo build"
```

#### Script with Metadata


```toml
[scripts]
build = {
    command = "cargo build",
    info = "Build the project in release mode",
    env = { RUST_LOG = "info" }
}
```

#### Script with Interpreter


```toml
[scripts]
deploy = {
    interpreter = "bash",
    command = "./scripts/deploy.sh",
    info = "Deploy to production"
}
```

#### Script Chaining (Includes)


```toml
[scripts]
prepublish_clean = "cargo clean"
prepublish_doc = "cargo doc --no-deps"
prepublish_dry = "cargo publish --dry-run"
prepublish_check = "cargo package --list"

prepublish = {
    include = ["prepublish_clean", "prepublish_doc", "prepublish_dry", "prepublish_check"],
    info = "Run all prepublish checks"
}
```

#### Script with Requirements


```toml
[scripts]
deploy = {
    command = "./deploy.sh",
    requires = ["docker >= 19.03", "kubectl >= 1.18"],
    toolchain = "stable",
    info = "Deploy application"
}
```

#### CI/CD-like Format


```toml
[scripts.build]
script = "build"
command = "cargo build"
info = "Build the project"

[scripts.test]
script = "test"
command = "cargo test"
requires = ["rustup >= 1.70"]
toolchain = "stable"
```

### Environment Variables


#### Global Environment Variables


```toml
[global_env]
RUST_BACKTRACE = "1"
RUST_LOG = "info"
```

#### Script-Specific Environment Variables


```toml
[scripts]
test = {
    command = "cargo test",
    env = { RUST_LOG = "debug" }
}
```

#### Command-Line Overrides


```sh
# Using Cargo subcommand

cargo script run test --env RUST_LOG=trace

# Or using direct binary

cgs run test --env RUST_LOG=trace
```

**Precedence Order:**

1. Command-line overrides (`--env`)
2. Script-specific (`env` in script)
3. Global (`[global_env]`)

### Show All Scripts


```sh
# Using Cargo subcommand (recommended)

cargo script show

# Or using direct binary

cgs show
```

Output:

```
📋 Available Scripts:
  • build     - Build the project
  • test      - Run tests
  • release   - Build release version
  • prepublish - Run all prepublish checks
```

### Dry-Run Mode


Preview what would be executed without actually running it:

```sh
# Using Cargo subcommand (recommended)

cargo script run prepublish --dry-run

# Or using direct binary

cgs run prepublish --dry-run
```

**Output:**

```
DRY-RUN MODE: Preview of what would be executed
================================================================================

📋  Would run script: [ prepublish ]
    Description: Run all prepublish checks
    Would run include scripts:
      📋  Would run script: [ prepublish_clean ]
          Command: cargo clean

      📋  Would run script: [ prepublish_doc ]
          Command: cargo doc --no-deps

      📋  Would run script: [ prepublish_dry ]
          Command: cargo publish --dry-run

      📋  Would run script: [ prepublish_check ]
          Command: cargo package --list

No commands were actually executed.
```

### Shell Completions


Enable tab completion for a better developer experience:

**Bash:**

```sh
# Using Cargo subcommand (recommended)

cargo script completions bash > ~/.bash_completion.d/cargo-script
# Or system-wide:

cargo script completions bash | sudo tee /etc/bash_completion.d/cargo-script

# Or using direct binary

cgs completions bash > ~/.bash_completion.d/cgs
```

**Zsh:**

```sh
mkdir -p ~/.zsh/completions
# Using Cargo subcommand (recommended)

cargo script completions zsh > ~/.zsh/completions/_cargo-script
# Or using direct binary

cgs completions zsh > ~/.zsh/completions/_cgs
# Add to ~/.zshrc:
fpath=(~/.zsh/completions $fpath)

autoload -U compinit && compinit
```

**Fish:**

```sh
# Using Cargo subcommand (recommended)

cargo script completions fish > ~/.config/fish/completions/cargo-script.fish
# Or using direct binary

cgs completions fish > ~/.config/fish/completions/cgs.fish
```

**PowerShell:**

```powershell
# Using Cargo subcommand (recommended)

cargo script completions power-shell > $PROFILE
# Or using direct binary

cgs completions power-shell > completions.ps1
. .\completions.ps1
```

After installation, restart your shell and enjoy tab completion! 🎉

### Validation


Catch configuration errors before they cause problems:

```sh
# Using Cargo subcommand (recommended)

cargo script validate

# Or using direct binary

cgs validate
```

**What it checks:**

-   ✅ TOML syntax validity
-   ✅ Script references in `include` arrays
-   ✅ Tool requirements (checks if tools are installed)
-   ✅ Toolchain requirements (checks if Rust/Python toolchains are installed)

**Example output:**

```
✓ All validations passed!
```

**With errors:**

```
❌ Validation Errors:
  1. Script 'release': Script 'release' references non-existent script 'build'
  2. Script 'deploy': Required tool 'docker' is not installed or not in PATH

✗ Found 2 error(s)
```

**CI/CD Integration:**

```yaml
# .github/workflows/ci.yml

- name: Validate Scripts.toml
  run: cargo script validate
```

### Error Messages

`cargo-run` provides helpful, actionable error messages:

**Script Not Found:**

```bash
$ cargo script run buid
❌ Script not found

Error:
  Script 'buid' not found in Scripts.toml

Did you mean:
  • build

Suggestion:
  Use 'cargo script show' to see all available scripts
```

**Invalid TOML:**

```bash
$ cargo script run test
❌ Invalid TOML syntax

Error:
  File: Scripts.toml
  Message: invalid table header
  Line 10: See error details above

Suggestion:
  Check your Scripts.toml syntax. Common issues:
  - Missing quotes around strings
  - Trailing commas in arrays
  - Invalid table syntax
```

**Missing Tool:**

```bash
$ cargo script run deploy
❌ Required tool not found

Error:
  Tool 'docker' is not installed or not in PATH

Suggestion:
  Install docker and ensure it's available in your PATH
```

## Use Cases


### Development Workflow


```toml
[scripts]
dev = "cargo run"
test = "cargo test"
test-watch = { command = "cargo watch -x test", requires = ["cargo-watch"] }
lint = "cargo clippy -- -D warnings"
fmt = "cargo fmt --check"
check = { include = ["fmt", "lint", "test"], info = "Run all checks" }
```

### CI/CD Pipeline


```toml
[scripts]
ci = {
    include = ["check", "test", "build"],
    info = "Run CI pipeline"
}

[scripts.check]
command = "cargo clippy -- -D warnings"

[scripts.test]
command = "cargo test --all-features"

[scripts.build]
command = "cargo build --release"
```

### Multi-Language Projects


```toml
[scripts]
build-rust = "cargo build"
build-python = {
    command = "python setup.py build",
    requires = ["python >= 3.8"],
    toolchain = "python:3.8"
}
build-all = { include = ["build-rust", "build-python"] }
```

### Deployment Scripts


```toml
[scripts]
deploy-staging = {
    command = "./scripts/deploy.sh staging",
    requires = ["docker >= 19.03", "kubectl >= 1.18"],
    env = { ENV = "staging" }
}

deploy-production = {
    command = "./scripts/deploy.sh production",
    requires = ["docker >= 19.03", "kubectl >= 1.18"],
    env = { ENV = "production" }
}
```

## 🔧 Advanced Configuration


### Custom Scripts Path


Use a different `Scripts.toml` file:

```sh
# Using Cargo subcommand

cargo script run build --scripts-path ./config/scripts.toml

# Or using direct binary

cgs run build --scripts-path ./config/scripts.toml
```

### Performance Metrics


Script execution times are automatically tracked and displayed:

```
Scripts Performance
--------------------------------------------------------------------------------
✔️  Script: prepublish_clean        🕒 Running time: 1.23s
✔️  Script: prepublish_doc          🕒 Running time: 3.45s
✔️  Script: prepublish_dry          🕒 Running time: 2.10s

🕒 Total running time: 6.78s
```

## 🤝 Contributing


Contributions are welcome! Please feel free to submit a Pull Request.

## 📄 License


This project is licensed under the [MIT License](LICENSE).

## 🙏 Acknowledgments


-   Inspired by `npm scripts`, `make`, and `just`
-   Built with [clap]https://github.com/clap-rs/clap for excellent CLI experience
-   Uses [colored]https://github.com/mackwic/colored for beautiful terminal output

---

**Made with ❤️ for the Rust community**