Skip to content

Commit d091c03

Browse files
committed
chapter-2
1 parent 0c73435 commit d091c03

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

cli-book/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
resolver = "2"
33

44
members = [
5-
"cli-rs",
5+
"cli-rs",
6+
"echor",
67
]

cli-book/echor/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "echor"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
clap = "4.5.23"

cli-book/echor/src/main.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use clap::{Arg, ArgAction, Command};
2+
3+
/*
4+
// default will be "first value to be the path of the program itself"
5+
// and any arguments passed to the program while calling the cargo run
6+
// ex: cargo run a b c
7+
Args {
8+
inner: ["E:\\Learning-to-code\\rust-workspace\\cli-book\\target\\debug\\echor.exe", "a", "b", "c"]
9+
}
10+
*/
11+
fn main() {
12+
// println!("{:?}", std::env::args());
13+
let matches = Command::new("echor")
14+
.version("0.1.0")
15+
.author("Rakuram <[email protected]>")
16+
.about("Rust Echo Command")
17+
.help_template(
18+
"{name} {version} \n\
19+
{author} \n\
20+
{about} \n\n\
21+
USAGE: \n {usage}\n\n\
22+
{all-args}",
23+
)
24+
.arg(
25+
Arg::new("text")
26+
.value_name("TEXT")
27+
.help("Input Text")
28+
.required(true)
29+
.num_args(1..),
30+
)
31+
.arg(
32+
Arg::new("omit_newline")
33+
.short('n')
34+
.help("Do not print the newline")
35+
.action(ArgAction::SetTrue),
36+
)
37+
.get_matches();
38+
39+
println!("{:#?}", matches);
40+
}

inpyjama-30-days-of-rust/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ cargo run --quiet --bin true
163163
cargo test -- --test-threads=1
164164
```
165165

166+
### Cargo Arguments
167+
- ```shell
168+
# below command will result in error
169+
cargo run -n Hello world
170+
# below command will execute without any error
171+
cargo run -- -n Hello world
172+
```
173+
- Without `--`: Cargo tries to interpret the arguments as options for itself. Since `-n` is not a valid Cargo flag, it results in an error.
174+
- With `--`: The `--` tells Cargo, "Stop processing options for yourself; pass everything after this directly to the program."
175+
- **When to Use --?**
176+
- Use `--` whenever you need to pass arguments to your Rust program that might be confused as flags for Cargo. For example:
177+
- ```shell
178+
cargo run -- -v
179+
```
180+
- This passes `-v` to your program, not to Cargo. Without `--`, Cargo would interpret `-v` as a flag for itself (e.g., for verbose output).
181+
166182
### to install rust from rustup
167183
```shell
168184
# installs nightly (unstable) version

0 commit comments

Comments
 (0)