fli 1.2.0

The commander.js like CLI Parser for rust
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
# Migration Guide: v0.1.x to v1.0.0

This document provides a comprehensive comparison between v0.1.x and v1.0.0 of the Fli library.

## Table of Contents
- [Overview]#overview
- [Breaking Changes]#breaking-changes
- [API Comparison]#api-comparison
- [Migration Examples]#migration-examples

## Overview

Version 1.0.0 is a complete rewrite of Fli with a focus on:
- **Type Safety**: Move from string-based parsing to enum-based type system
- **Error Handling**: Proper `Result` types with detailed error variants
- **Better API**: More intuitive methods and clearer separation of concerns
- **Improved UX**: Beautiful help output with tables and better error messages

## Breaking Changes

### 1. Initialization

**v0.1.x:**
```rust
use fli::{Fli, init_fli_from_toml};

let mut app = Fli::init("app-name", "description");
app.set_version("0.0.1");

// OR
let mut app = init_fli_from_toml!();
```

**v1.0.0:**
```rust
use fli::Fli;

let mut app = Fli::new("app-name", "1.0.0", "description");

// OR
let mut app = fli::init_fli_from_toml!();
```

### 2. Option Definition

**v0.1.x:**
```rust
app.option("-n --name, <>", "Name to call you", |x: &Fli| {
    match x.get_values("name".to_owned()) {
        Ok(value) => println!("Hello {}", value[0]),
        Err(_) => {},
    }
});
```

**v1.0.0:**
```rust
use fli::{ValueTypes, Value};

app.add_option(
    "name",
    "Name to call you",
    "-n",
    "--name",
    ValueTypes::RequiredSingle(Value::Str(String::new()))
);

app.set_callback(|data| {
    let name = data.get_option_value("name")
        .and_then(|v| v.as_str())
        .unwrap_or("World");
    println!("Hello {}", name);
});
```

### 3. Value Type Mapping

| v0.1.x Syntax | Meaning | v1.0.0 Type |
|---------------|---------|-------------|
| (no symbol) | Flag only | `ValueTypes::OptionalSingle(Some(Value::Bool(false)))` |
| `<>` | Required single | `ValueTypes::RequiredSingle(Value::Str(String::new()))` |
| `[]` | Optional single | `ValueTypes::OptionalSingle(None)` |
| `<...>` | Required multiple (at least 1) | `ValueTypes::RequiredMultiple(vec![], None)` |
| `[...]` | Optional multiple | `ValueTypes::OptionalMultiple(None, None)` |

### 4. Callback Signature

**v0.1.x:**
```rust
fn callback(app: &Fli) {
    // Access app state directly
    let values = app.get_values("name".to_owned()).unwrap();
    let is_passed = app.is_passed("--verbose");
}
```

**v1.0.0:**
```rust
fn callback(data: &FliCallbackData) {
    // Access through data object
    let name = data.get_option_value("name")
        .and_then(|v| v.as_str());
    let verbose = data.get_option_value("verbose").is_some();
}
```

### 5. Commands

**v0.1.x:**
```rust
app.command("greet", "An app that respects")
    .default(greet)
    .allow_inital_no_param_values(false)
    .option("-n --name, <>", "Your name", greet);
```

**v1.0.0:**
```rust
let greet_cmd = app.command("greet", "An app that respects")?;

greet_cmd.add_option(
    "name",
    "Your name",
    "-n",
    "--name",
    ValueTypes::RequiredSingle(Value::Str(String::new()))
);

greet_cmd.set_callback(greet);
greet_cmd.set_expected_positional_args(1);
```

### 6. Value Access Methods

| v0.1.x Method | v1.0.0 Equivalent | Notes |
|---------------|-------------------|-------|
| `app.get_values("name")` | `data.get_option_value("name").and_then(\|v\| v.as_str())` | Type-safe extraction |
| `app.is_passed("--flag")` | `data.get_option_value("flag").is_some()` | Check if option passed |
| `app.has_a_value("-n")` | `data.get_option_value("n").and_then(\|v\| v.as_str()).is_some()` | Check if has value |
| `app.get_arg_at(0)` | `data.get_argument_at(0)` | Positional arguments |

## API Comparison

### Removed Methods (v0.1.x → v1.0.0)

| v0.1.x Method | Reason | v1.0.0 Alternative |
|---------------|--------|-------------------|
| `.allow_duplicate_callback()` | Architecture change | Callbacks managed differently |
| `.allow_inital_no_param_values()` | Replaced | `.set_expected_positional_args()` |
| `.default()` | Renamed | `.set_callback()` |
| `.init()` | Renamed | `.new()` |
| `.set_version()` | Combined with init | Pass version to `.new()` |
| `.get_values()` | On `Fli` | Use `FliCallbackData::get_option_value()` |
| `.is_passed()` | On `Fli` | Use `FliCallbackData::get_option_value().is_some()` |
| `.has_a_value()` | On `Fli` | Check value type with `get_option_value()` |
| `.get_arg_at()` | On `Fli` | Use `FliCallbackData::get_argument_at()` |
| `.print_help()` | Manual | Automatic via `--help` flag |

### New Methods (v1.0.0)

| Method | Description |
|--------|-------------|
| `.add_option()` | Type-safe option definition |
| `.set_callback()` | Set command callback |
| `.with_debug()` | Enable debug mode |
| `.add_debug_option()` | Add `-D/--debug` flag |
| `.set_expected_positional_args()` | Set expected arg count |
| `.get_option_value()` | Get parsed option value |
| `.as_str()` | Extract string from value |
| `.as_strings()` | Extract strings from multiple values |
| `.get_arguments()` | Get all positional args |

## Migration Examples

### Example 1: Simple Flag

**v0.1.x:**
```rust
use fli::Fli;

fn main() {
    let mut app = Fli::init("myapp", "My app");
    
    app.option("-v --verbose", "Enable verbose", |x: &Fli| {
        println!("Verbose mode enabled!");
    });
    
    app.run();
}
```

**v1.0.0:**
```rust
use fli::{Fli, ValueTypes};

fn main() {
    let mut app = Fli::new("myapp", "1.0.0", "My app");
    
    app.add_option(
        "verbose",
        "Enable verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false)))
    );
    
    app.set_callback(|data| {
        if data.get_option_value("verbose").is_some() {
            println!("Verbose mode enabled!");
        }
    });
    
    app.run();
}
```

### Example 2: Required Value

**v0.1.x:**
```rust
app.option("-n --name, <>", "Your name", |x: &Fli| {
    match x.get_values("name".to_owned()) {
        Ok(values) => println!("Hello, {}!", values[0]),
        Err(e) => eprintln!("Error: {}", e),
    }
});
```

**v1.0.0:**
```rust
use fli::{ValueTypes, Value};

app.add_option(
    "name",
    "Your name",
    "-n",
    "--name",
    ValueTypes::RequiredSingle(Value::Str(String::new()))
);

app.set_callback(|data| {
    let name = data.get_option_value("name")
        .and_then(|v| v.as_str())
        .unwrap_or("World");
    println!("Hello, {}!", name);
});
```

### Example 3: Commands with Options

**v0.1.x:**
```rust
fn main() {
    let mut app = init_fli_from_toml!();
    
    app.command("greet", "Greeting command")
        .default(greet)
        .option("-n --name, <>", "Your name", greet)
        .option("-t --time, []", "Time of day", greet);
    
    app.run();
}

fn greet(x: &Fli) {
    let name = x.get_values("name".to_owned())
        .unwrap_or(vec!["friend".to_string()])[0].clone();
    println!("Hello, {}!", name);
}
```

**v1.0.0:**
```rust
use fli::{Fli, ValueTypes, Value};

fn main() {
    let mut app = fli::init_fli_from_toml!();
    
    let greet_cmd = app.command("greet", "Greeting command").unwrap();
    
    greet_cmd.add_option(
        "name",
        "Your name",
        "-n",
        "--name",
        ValueTypes::RequiredSingle(Value::Str(String::new()))
    );
    
    greet_cmd.add_option(
        "time",
        "Time of day",
        "-t",
        "--time",
        ValueTypes::OptionalSingle(None)
    );
    
    greet_cmd.set_callback(greet);
    
    app.run();
}

fn greet(data: &FliCallbackData) {
    let name = data.get_option_value("name")
        .and_then(|v| v.as_str())
        .unwrap_or("friend");
    
    let time = data.get_option_value("time")
        .and_then(|v| v.as_str());
    
    let greeting = match time {
        Some("morning") => "Good morning",
        Some("evening") => "Good evening",
        _ => "Hello",
    };
    
    println!("{}, {}!", greeting, name);
}
```

### Example 4: Multiple Values

**v0.1.x:**
```rust
app.command("move", "Move files")
    .option("-p --path, <...>", "Files to move", |x: &Fli| {
        match x.get_values("path".to_owned()) {
            Ok(files) => {
                for file in files {
                    println!("Moving: {}", file);
                }
            },
            Err(_) => {},
        }
    });
```

**v1.0.0:**
```rust
use fli::{ValueTypes, Value};

let move_cmd = app.command("move", "Move files")?;

move_cmd.add_option(
    "path",
    "Files to move",
    "-p",
    "--path",
    ValueTypes::RequiredMultiple(vec![], None)
);

move_cmd.set_callback(|data| {
    let files = data.get_option_value("path")
        .and_then(|v| v.as_strings())
        .unwrap_or_default();
    
    for file in files {
        println!("Moving: {}", file);
    }
});
```

## Benefits of v1.0.0

1. **Type Safety**: Compile-time guarantees about option values
2. **Better Errors**: Detailed error messages with suggestions
3. **Cleaner API**: More intuitive method names and organization
4. **Documentation**: Comprehensive docs with examples
5. **Testing**: 110+ tests ensuring reliability
6. **Help System**: Beautiful auto-generated help with tables
7. **Error Recovery**: Better error handling with `Result` types
8. **Performance**: More efficient parsing with state machine
9. **Maintainability**: Modular codebase easier to extend

## Quick Reference Card

### Common Tasks

| Task | v0.1.x | v1.0.0 |
|------|--------|--------|
| Create app | `Fli::init("name", "desc")` | `Fli::new("name", "ver", "desc")` |
| Add flag | `option("-v --verbose", "...", cb)` | `add_option("verbose", "...", "-v", "--verbose", ValueTypes::OptionalSingle(Some(Value::Bool(false))))` |
| Required value | `option("-n --name, <>", "...", cb)` | `add_option("name", "...", "-n", "--name", RequiredSingle(_))` |
| Optional value | `option("-f --file, []", "...", cb)` | `add_option("file", "...", "-f", "--file", OptionalSingle(_))` |
| Multiple values | `option("-f --files, <...>", "...", cb)` | `add_option("files", "...", "-f", "--files", RequiredMultiple(_, None))` |
| Set callback | `.default(callback)` | `.set_callback(callback)` |
| Get value | `app.get_values("name")` | `data.get_option_value("name").as_str()` |
| Check flag | `app.is_passed("--flag")` | `data.get_option_value("flag").is_some()` |
| Positional args | `app.get_arg_at(0)` | `data.get_argument_at(0)` |

## Troubleshooting

### Common Migration Issues

1. **Callback signature errors**: Update from `&Fli` to `&FliCallbackData`
2. **Option syntax errors**: Replace string templates with `add_option()` calls
3. **Value access errors**: Use `get_option_value()` instead of `get_values()`
4. **Command errors**: Handle `Result` from `.command()` method
5. **Import errors**: Update imports to include `ValueTypes` and `Value`

## Need Help?

- Check the [examples directory]https://github.com/codad5/fli/tree/master/examples
- Read the [API documentation]https://docs.rs/fli
- Open an issue on [GitHub]https://github.com/codad5/fli/issues