gflights 0.2.1

Unofficial async Rust client for the Google Flights web API — search flights, price graphs, and booking offers.
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use anyhow::Result;
use chrono::NaiveDate;
use clap::{Parser, Subcommand, ValueEnum};
use gflights::parsers::common::{StopOptions, TravelClass, Travelers};
use gflights::requests::api::ApiClient;
use gflights::requests::config::{Config, Currency};
use rustyline::error::ReadlineError;
use rustyline::DefaultEditor;

pub mod cheap;
pub mod date_grid;
pub mod explore;
pub mod graph;
pub mod multi_city;
pub mod offer;
pub mod search;

use cheap::{cmd_cheap, CheapArgs};
use date_grid::{cmd_date_grid, DateGridArgs};
use explore::{cmd_explore, ExploreArgs};
use graph::{cmd_graph, GraphArgs};
use multi_city::{cmd_multi_city, MultiCityArgs};
use offer::{cmd_offer, OfferArgs};
use search::{cmd_search, SearchArgs};

// ---------------------------------------------------------------------------
// Output format
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, ValueEnum, Default)]
pub enum OutputFormat {
    /// Human-readable aligned table (default).
    #[default]
    Table,
    /// JSON — suitable for piping to `jq` or other tools.
    Json,
}

// ---------------------------------------------------------------------------
// Top-level CLI  (optional subcommand → REPL when absent)
// ---------------------------------------------------------------------------

/// Unofficial Google Flights command-line client.
///
/// Run without arguments to enter the interactive REPL.
#[derive(Parser, Debug)]
#[command(name = "gflights", version, about, long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,
}

/// gflights interactive REPL.  Type 'help' for usage, 'quit' to exit.
#[derive(Parser, Debug)]
#[command(name = "gflights")]
pub struct ReplCommand {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Search for available flights.
    Search(SearchArgs),
    /// Show cheapest prices across a date range (price graph).
    Graph(GraphArgs),
    /// Show a price grid across departure × return date windows (round trips only).
    #[command(name = "dgrid")]
    DateGrid(DateGridArgs),
    /// Show booking offers (with airline prices and URLs) for a specific itinerary.
    Offer(OfferArgs),
    /// Multi-city (open-jaw) flight search across 2+ legs.
    ///
    /// Specify each leg with --leg FROM,TO,DATE (repeatable).
    ///
    /// Example: gflights mcity --leg LUX,FCO,2026-09-10 --leg FCO,MAD,2026-09-13 --leg MAD,LUX,2026-09-17
    #[command(name = "mcity")]
    MultiCity(MultiCityArgs),
    /// Find the cheapest departure dates for a route over a range of months.
    ///
    /// Use --trip-days N to search for round trips of exactly N nights.
    /// Omit --trip-days for one-way date discovery.
    #[command(name = "cheap")]
    Cheap(CheapArgs),
    /// Explore cheap destinations from an origin airport (Google Flights Explore).
    ///
    /// Example: gflights explore --from LUX --month 7 --duration week --budget 300
    #[command(name = "explore")]
    Explore(ExploreArgs),
    /// Exit the interactive REPL (alias: exit).
    #[command(alias = "exit")]
    Quit,
}

// ---------------------------------------------------------------------------
// Shared options
// ---------------------------------------------------------------------------

/// Options shared by all subcommands that use a single date-pair route.
#[derive(Parser, Debug)]
pub struct CommonArgs {
    /// Departure airport IATA code or city name (e.g. LHR, "London").
    #[arg(long)]
    pub from: String,

    /// Destination airport IATA code or city name (e.g. JFK, "New York").
    #[arg(long)]
    pub to: String,

    /// Outbound departure date in YYYY-MM-DD format.
    #[arg(long)]
    pub date: NaiveDate,

    /// Return date in YYYY-MM-DD format (omit for one-way).
    #[arg(long)]
    pub r#return: Option<NaiveDate>,

    /// Number of adult passengers.
    #[arg(long, default_value = "1")]
    pub adults: u32,

    /// Travel class.
    #[arg(long, default_value = "economy")]
    pub class: TravelClass,

    /// Stop filter.
    #[arg(long, default_value = "all")]
    pub stops: StopOptions,

    /// Currency for prices.
    #[arg(long, default_value = "euro")]
    pub currency: Currency,

    /// BCP-47 language subtag (e.g. en, fr, de).
    #[arg(long, default_value = "en")]
    pub lang: String,

    /// ISO 3166-1 alpha-2 country code (e.g. GB, FR, US).
    #[arg(long, default_value = "GB")]
    pub country: String,

    /// Output format.
    #[arg(long, default_value = "table")]
    pub format: OutputFormat,
}

// ---------------------------------------------------------------------------
// Build Config from CommonArgs (shared helper)
// ---------------------------------------------------------------------------

pub async fn build_config(common: &CommonArgs, client: &ApiClient) -> Result<Config> {
    let travelers = Travelers::new(vec![common.adults as i32, 0, 0, 0])?;

    let mut builder = Config::builder()
        .departure(&common.from, client)
        .await?
        .destination(&common.to, client)
        .await?
        .departing_date(common.date)
        .travelers(travelers)
        .travel_class(common.class)
        .stop_options(common.stops)
        .currency(common.currency.clone())
        .language(common.lang.clone())
        .country(common.country.clone());

    if let Some(ret) = common.r#return {
        builder = builder.return_date(ret);
    }

    builder.build()
}

// ---------------------------------------------------------------------------
// Dispatch a parsed command
// ---------------------------------------------------------------------------

pub async fn run_command(cmd: Commands, client: &ApiClient) -> Result<()> {
    match cmd {
        Commands::Search(args) => cmd_search(args, client).await,
        Commands::Graph(args) => cmd_graph(args, client).await,
        Commands::DateGrid(args) => cmd_date_grid(args, client).await,
        Commands::Offer(args) => cmd_offer(args, client).await,
        Commands::MultiCity(args) => cmd_multi_city(args, client).await,
        Commands::Cheap(args) => cmd_cheap(args, client).await,
        Commands::Explore(args) => cmd_explore(args, client).await,
        Commands::Quit => Ok(()),
    }
}

// ---------------------------------------------------------------------------
// Interactive REPL — help text
// ---------------------------------------------------------------------------

fn print_repl_help() {
    println!(
        "\
Commands:

  search --from <CODE> --to <CODE> --date <YYYY-MM-DD> [--return <DATE>]
    Filters:  --stops all|nonstop|one-stop
              --airline <CODE|ALLIANCE>  (repeatable; e.g. --airline LX --airline ONEWORLD)
              --exclude-airline <CODE>   (repeatable)
              --via <CODE>               (require connection through this airport)
              --min-layover <MINS>       --max-layover <MINS>
              --lower-emissions          (restrict to below-average CO₂ flights)
    Output:   --sort best|price|duration|departure|arrival
              --show-co2                 (add CO₂ kg column to table)
              --detail                   (show layover airports; +1 for next-day arrivals)
              --format table|json
    Locale:   --adults <N>  --class economy|premium-economy|business|first
              --currency <NAME>  --lang <BCP47>  --country <ISO2>

  graph --from <CODE> --to <CODE> --date <YYYY-MM-DD>
    Options:  --months <N>  (number of months to scan, default 3)
              --adults --class --stops --currency --lang --country --format

  dgrid --from <CODE> --to <CODE>
        --dep-start <DATE> --dep-end <DATE>
        --ret-start <DATE> --ret-end <DATE>
    Options:  --adults --class --stops --currency --lang --country --format

  offer --from <CODE> --to <CODE> --date <YYYY-MM-DD> [--return <DATE>]
    (same filters as search)

  cheap --from <CODE> --to <CODE> --date <YYYY-MM-DD>
    Options:  --months <N>       (months to scan, default 3)
              --trip-days <N>    (round-trip length in nights; omit for one-way)
              --adults --class --currency --lang --country

  mcity --leg FROM,TO,DATE [--leg FROM,TO,DATE ...]
    Options:  --sort best|price|duration
              --adults --class --currency --lang --country --format

  explore --from <CODE> [--to <CODE|REGION>]
    Options:  --month <1-12>       (travel month; omit for any)
              --duration week|weekend|2weeks
              --budget <N>         (max price in chosen currency)
              --interest <NAME|/m/MID>
                known names: outdoors, beaches, museums, history, skiing, climbing
                aliases: nature, beach, art, heritage, snow, rock-climbing
                raw MID: /m/01rwk  (any Knowledge-Graph MID accepted)
              --max-flight-hours <N>
              --carry-on <N>  --checked <N>
              --adults --class --currency --lang --country --format

  quit / exit

Tip: type '<command> --help' for full clap-generated details on any command."
    );
}

// ---------------------------------------------------------------------------
// Interactive REPL
// ---------------------------------------------------------------------------

pub async fn run_repl(client: &ApiClient) -> Result<()> {
    let mut rl = DefaultEditor::new()?;
    println!("gflights interactive mode  (type 'help' for usage, 'quit' to exit)");

    loop {
        match rl.readline("gflights> ") {
            Ok(line) => {
                let line = line.trim().to_string();
                if line.is_empty() {
                    continue;
                }
                let _ = rl.add_history_entry(&line);

                if line == "help" || line == "--help" || line == "-h" {
                    print_repl_help();
                    continue;
                }

                // Prepend program name so clap can parse "search --from …" correctly.
                let parts: Vec<String> = std::iter::once("gflights".to_string())
                    .chain(line.split_whitespace().map(String::from))
                    .collect();

                match ReplCommand::try_parse_from(&parts) {
                    Ok(rc) => {
                        if matches!(rc.command, Commands::Quit) {
                            break;
                        }
                        if let Err(e) = run_command(rc.command, client).await {
                            eprintln!("Error: {e:#}");
                        }
                    }
                    // For all clap errors (missing required args, --help, --version,
                    // unknown flags, …) use clap's own formatter so the user sees
                    // coloured output with a "Usage:" hint instead of a raw error
                    // string.  We never call process::exit here so the REPL continues.
                    Err(e) => {
                        e.print().unwrap_or_default();
                    }
                }
            }
            Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,
            Err(e) => {
                eprintln!("Readline error: {e}");
                break;
            }
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests for REPL command parsing (no network, pure clap)
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use clap::error::ErrorKind;

    fn parse(args: &[&str]) -> Result<ReplCommand, clap::Error> {
        let parts: Vec<String> = std::iter::once("gflights")
            .chain(args.iter().copied())
            .map(String::from)
            .collect();
        ReplCommand::try_parse_from(&parts)
    }

    #[test]
    fn repl_parse_quit_command() {
        let rc = parse(&["quit"]).expect("quit should parse");
        assert!(matches!(rc.command, Commands::Quit));
    }

    #[test]
    fn repl_parse_exit_alias() {
        let rc = parse(&["exit"]).expect("exit should parse");
        assert!(matches!(rc.command, Commands::Quit));
    }

    #[test]
    fn repl_parse_search_minimal() {
        let rc = parse(&[
            "search",
            "--from",
            "LHR",
            "--to",
            "JFK",
            "--date",
            "2026-08-01",
        ])
        .expect("minimal search should parse");
        match rc.command {
            Commands::Search(args) => {
                assert_eq!(args.common.from, "LHR");
                assert_eq!(args.common.to, "JFK");
            }
            other => panic!("expected Search, got {other:?}"),
        }
    }

    #[test]
    fn repl_parse_search_with_return() {
        let rc = parse(&[
            "search",
            "--from",
            "MXP",
            "--to",
            "SVO",
            "--date",
            "2026-08-01",
            "--return",
            "2026-08-15",
        ])
        .expect("search with return should parse");
        match rc.command {
            Commands::Search(args) => {
                assert!(args.common.r#return.is_some());
            }
            other => panic!("expected Search, got {other:?}"),
        }
    }

    #[test]
    fn repl_parse_dgrid_command() {
        let rc = parse(&[
            "dgrid",
            "--from",
            "LHR",
            "--to",
            "JFK",
            "--dep-start",
            "2026-08-01",
            "--dep-end",
            "2026-08-07",
            "--ret-start",
            "2026-08-15",
            "--ret-end",
            "2026-08-22",
        ])
        .expect("dgrid should parse");
        assert!(matches!(rc.command, Commands::DateGrid(_)));
    }

    #[test]
    fn repl_parse_graph_with_months() {
        let rc = parse(&[
            "graph",
            "--from",
            "SVO",
            "--to",
            "CDG",
            "--date",
            "2026-09-01",
            "--months",
            "6",
        ])
        .expect("graph with months should parse");
        match rc.command {
            Commands::Graph(args) => assert_eq!(args.months, 6),
            other => panic!("expected Graph, got {other:?}"),
        }
    }

    #[test]
    fn repl_parse_offer_command() {
        let rc = parse(&[
            "offer",
            "--from",
            "FRA",
            "--to",
            "NRT",
            "--date",
            "2026-09-01",
        ])
        .expect("offer should parse");
        assert!(matches!(rc.command, Commands::Offer(_)));
    }

    #[test]
    fn repl_parse_invalid_command_returns_error() {
        let result = parse(&["bogus"]);
        assert!(result.is_err(), "unknown subcommand should error");
    }

    #[test]
    fn repl_parse_missing_required_returns_error() {
        // search without any arguments should error (all three of --from/--to/--date required)
        let result = parse(&["search"]);
        assert!(result.is_err(), "search without required args should error");
        let e = result.unwrap_err();
        assert!(
            matches!(e.kind(), ErrorKind::MissingRequiredArgument),
            "expected MissingRequiredArgument, got: {:?}",
            e.kind()
        );
    }

    #[test]
    fn repl_parse_help_flag_returns_display_help() {
        // `--help` on any subcommand produces DisplayHelp.  The REPL catches this
        // via `e.print()` and continues without calling process::exit.
        let result = parse(&["search", "--help"]);
        assert!(
            result.is_err(),
            "--help must produce an error (DisplayHelp)"
        );
        let e = result.unwrap_err();
        assert_eq!(
            e.kind(),
            ErrorKind::DisplayHelp,
            "expected DisplayHelp, got: {:?}",
            e.kind()
        );
    }

    #[test]
    fn repl_parse_search_filter_flags() {
        let rc = parse(&[
            "search",
            "--from",
            "LHR",
            "--to",
            "JFK",
            "--date",
            "2026-08-01",
            "--min-layover",
            "60",
            "--max-layover",
            "180",
            "--lower-emissions",
            "--airline",
            "LX",
            "--airline",
            "ONEWORLD",
            "--exclude-airline",
            "FR",
            "--via",
            "CDG",
        ])
        .expect("search with filter flags should parse");
        match rc.command {
            Commands::Search(args) => {
                assert_eq!(args.min_layover, Some(60));
                assert_eq!(args.max_layover, Some(180));
                assert!(args.lower_emissions);
                assert_eq!(args.airlines.len(), 2);
                assert_eq!(args.exclude_airlines.len(), 1);
                assert_eq!(args.connecting_airports, vec!["CDG"]);
            }
            other => panic!("expected Search, got {other:?}"),
        }
    }

    #[test]
    fn repl_parse_search_detail_and_co2_flags() {
        let rc = parse(&[
            "search",
            "--from",
            "LUX",
            "--to",
            "BCN",
            "--date",
            "2026-09-01",
            "--show-co2",
            "--detail",
        ])
        .expect("search with --show-co2 --detail should parse");
        match rc.command {
            Commands::Search(args) => {
                assert!(args.show_co2);
                assert!(args.detail);
            }
            other => panic!("expected Search, got {other:?}"),
        }
    }

    #[test]
    fn repl_parse_invalid_date_returns_error() {
        let result = parse(&[
            "search",
            "--from",
            "LHR",
            "--to",
            "JFK",
            "--date",
            "not-a-date",
        ]);
        assert!(result.is_err(), "invalid date should error");
    }

    #[test]
    fn repl_parse_mcity_two_legs() {
        let rc = parse(&[
            "mcity",
            "--leg",
            "LUX,FCO,2026-09-10",
            "--leg",
            "FCO,MAD,2026-09-13",
        ])
        .expect("mcity with 2 legs should parse");
        match rc.command {
            Commands::MultiCity(args) => {
                assert_eq!(args.legs.len(), 2);
                assert_eq!(args.legs[0].from, "LUX");
                assert_eq!(args.legs[0].to, "FCO");
                assert_eq!(args.legs[1].from, "FCO");
            }
            other => panic!("expected MultiCity, got {other:?}"),
        }
    }

    #[test]
    fn repl_parse_mcity_three_legs() {
        let rc = parse(&[
            "mcity",
            "--leg",
            "LUX,FCO,2026-09-10",
            "--leg",
            "FCO,MAD,2026-09-13",
            "--leg",
            "MAD,LUX,2026-09-17",
        ])
        .expect("mcity with 3 legs should parse");
        match rc.command {
            Commands::MultiCity(args) => assert_eq!(args.legs.len(), 3),
            other => panic!("expected MultiCity, got {other:?}"),
        }
    }

    #[test]
    fn repl_parse_mcity_invalid_leg_format() {
        let result = parse(&["mcity", "--leg", "LUX-FCO-2026-09-10"]);
        assert!(result.is_err(), "invalid leg format should error");
    }

    #[test]
    fn repl_parse_search_accepts_sort_order_values() {
        for sort in &["best", "price", "duration"] {
            let rc = parse(&[
                "search",
                "--from",
                "LHR",
                "--to",
                "JFK",
                "--date",
                "2026-08-01",
                "--sort",
                sort,
            ])
            .unwrap_or_else(|e| panic!("sort={sort} should parse: {e}"));
            assert!(matches!(rc.command, Commands::Search(_)));
        }
    }
}