Skip to content

Commit a1896e5

Browse files
authored
improve documentation
1 parent 81b6ba8 commit a1896e5

File tree

1 file changed

+100
-10
lines changed

1 file changed

+100
-10
lines changed

README.md

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,46 @@ A flexible, composable string transformation CLI tool and library for Rust origi
1313
- **Append and prepend**: Add text before or after.
1414
- **Escaping**: Use `\:` and `\\` to include literal colons and backslashes in arguments.
1515
- **Negative indices**: Support for negative indices (like Python) in ranges and slices.
16+
- **Stdin support**: Read input from stdin when no input argument is provided.
1617
- **Tested**: Comprehensive test suite.
1718

1819
## Usage
1920

2021
### As a CLI
2122

2223
```sh
23-
cargo run -- "input string" "{template}"
24+
# With input argument
25+
cargo run -- "{template}" "input string"
26+
27+
# With stdin input
28+
echo "input string" | cargo run -- "{template}"
2429
```
2530

2631
**Examples:**
2732

2833
```sh
2934
# Get the second item in a comma-separated list
30-
cargo run -- "a,b,c" "{split:,:1}"
35+
cargo run -- "{split:,:1}" "a,b,c"
36+
# Output: b
37+
38+
# Using stdin
39+
echo "a,b,c" | cargo run -- "{split:,:1}"
3140
# Output: b
3241

3342
# Replace all spaces with underscores and uppercase
34-
cargo run -- "foo bar baz" "{replace:s/ /_/g:upper}"
43+
cargo run -- "{replace:s/ /_/g:upper}" "foo bar baz"
3544
# Output: FOO_BAR_BAZ
3645

3746
# Trim, split, and append
38-
cargo run -- " a, b,c , d , e " "{split:,:..:trim:append:!}"
47+
cargo run -- "{split:,:..:trim:append:!}" " a, b,c , d , e "
3948
# Output: a!,b!,c!,d!,e!
49+
50+
# Using stdin for processing file content
51+
cat data.txt | cargo run -- "{split:\n:..:trim:prepend:- }"
52+
53+
# Pipeline processing
54+
echo "hello,world,test" | cargo run -- "{split:,:0..2:join: | :upper}"
55+
# Output: HELLO | WORLD
4056
```
4157

4258
### Template Syntax
@@ -67,20 +83,60 @@ To include a literal `:` or `\` in an argument, escape it as `\:` or `\\`.
6783

6884
```sh
6985
# Get the last item
70-
cargo run -- "a,b,c" "{split:,:-1}"
86+
cargo run -- "{split:,:-1}" "a,b,c"
7187
# Output: c
7288

73-
# Replace 'foo' with 'bar'
74-
cargo run -- "foo foo" "{replace:s/foo/bar/g}"
89+
# Get a range of items
90+
cargo run -- "{split:,:1..=3}" "a,b,c,d,e"
91+
# Output: b,c,d
92+
93+
# Replace 'foo' with 'bar' globally
94+
cargo run -- "{replace:s/foo/bar/g}" "foo foo"
7595
# Output: bar bar
7696

77-
# Uppercase, then append
78-
cargo run -- "hello" "{upper:append:!}"
97+
# Chain operations: uppercase, then append
98+
cargo run -- "{upper:append:!}" "hello"
7999
# Output: HELLO!
80100

81101
# Prepend with a colon (escaped)
82-
cargo run -- "bar" "{prepend:\:foo}"
102+
cargo run -- "{prepend:\:foo}" "bar"
83103
# Output: :foobar
104+
105+
# Complex chaining: split, select range, join, replace, uppercase
106+
cargo run -- "{split:,:0..2:join:-:replace:s/a/X/:upper}" "a,b,c"
107+
# Output: X-B
108+
109+
# Slice string characters
110+
cargo run -- "{slice:1..=3}" "hello"
111+
# Output: ell
112+
113+
# Split, trim each item, then prepend
114+
echo " a , b , c " | cargo run -- "{split:,:..:trim:prepend:item_}"
115+
# Output: item_a,item_b,item_c
116+
117+
# Strip custom characters
118+
cargo run -- "{strip:xy}" "xyhelloxy"
119+
# Output: hello
120+
```
121+
122+
### Advanced Examples
123+
124+
```sh
125+
# Process CSV-like data
126+
echo "name,age,city" | cargo run -- "{split:,:1..}"
127+
# Output: age,city
128+
129+
# Format file paths
130+
echo "/home/user/documents/file.txt" | cargo run -- "{split:/:-1:prepend:dir }"
131+
# Output: dir file.txt
132+
133+
# Extract file extension
134+
echo "document.pdf" | cargo run -- "{split:.:-1:upper}"
135+
# Output: PDF
136+
137+
# Process log entries with timestamps
138+
echo "2023-01-01 ERROR Failed to connect" | cargo run -- "{split: :1..:join: :lower}"
139+
# Output: error failed to connect
84140
```
85141

86142
## Library Usage
@@ -100,6 +156,32 @@ use your_crate::process;
100156

101157
let result = process("foo,bar,baz", "{split:,:1:upper}").unwrap();
102158
assert_eq!(result, "BAR");
159+
160+
// Chain multiple operations
161+
let result = process(" hello world ", "{trim:split: :join:_:upper}").unwrap();
162+
assert_eq!(result, "HELLO_WORLD");
163+
164+
// Work with ranges
165+
let result = process("a,b,c,d,e", "{split:,:1..=3:join:-}").unwrap();
166+
assert_eq!(result, "b-c-d");
167+
```
168+
169+
## Error Handling
170+
171+
The tool provides helpful error messages for common issues:
172+
173+
```sh
174+
# Invalid template format
175+
cargo run -- "split:,:0" "test"
176+
# Error: Template must start with '{' and end with '}'
177+
178+
# Invalid range
179+
cargo run -- "{split:,:abc}" "a,b,c"
180+
# Error: Invalid index
181+
182+
# Invalid regex
183+
cargo run -- "{replace:s/[/replacement/}" "test"
184+
# Error: regex parse error
103185
```
104186

105187
## Running Tests
@@ -108,6 +190,14 @@ assert_eq!(result, "BAR");
108190
cargo test
109191
```
110192

193+
## Use Cases
194+
195+
- **Data extraction**: Parse CSV, logs, or structured text
196+
- **Text transformation**: Clean and format strings in pipelines
197+
- **File processing**: Extract parts of filenames or paths
198+
- **Configuration parsing**: Process environment variables or config files
199+
- **Shell scripting**: Quick text manipulation in scripts
200+
111201
## License
112202

113203
MIT

0 commit comments

Comments
 (0)