adrs 0.7.5

Command line tool for managing Architecture Decision Records
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
//! adrs - Architecture Decision Records CLI tool.

use adrs_core::{ConfigSource, discover};
use anyhow::{Context, Result};
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::{Shell, generate};
use std::io;
use std::path::PathBuf;

mod commands;

#[cfg(feature = "mcp")]
mod mcp;

#[derive(Parser)]
#[command(name = "adrs")]
#[command(author, version)]
#[command(about = "Manage Architecture Decision Records")]
#[command(long_about = "\
A command-line tool for creating and managing Architecture Decision Records (ADRs).

Compatible with adr-tools repositories. Supports both Nygard and MADR 4.0.0 formats.

GETTING STARTED:
  adrs init                    Create a new ADR repository
  adrs new \"My Decision\"       Create your first ADR
  adrs list                    View all ADRs
  adrs doctor                  Check repository health

FORMATS:
  nygard    Classic adr-tools format (default)
  madr      MADR 4.0.0 with structured metadata

MODES:
  Compatible (default)    Works with adr-tools, metadata in markdown
  NextGen (--ng)          YAML frontmatter for richer metadata (tags, custom fields)

EXAMPLES:
  adrs init                                      Initialize repository
  adrs new --format madr \"Use PostgreSQL\"       Create MADR-format ADR
  adrs new --supersedes 2 \"Use MySQL instead\"   Supersede an ADR
  adrs link 3 Amends 1                          Link two ADRs (auto-derives reverse)
  adrs generate toc > doc/adr/README.md         Generate table of contents

DOCUMENTATION: https://joshrotenberg.com/adrs/")]
struct Cli {
    /// Enable NextGen mode with YAML frontmatter for richer metadata
    #[arg(
        long,
        global = true,
        help = "Enable NextGen mode with YAML frontmatter"
    )]
    ng: bool,

    /// Run from a different directory
    #[arg(short = 'C', long = "cwd", global = true, value_name = "DIR")]
    working_dir: Option<PathBuf>,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Initialize a new ADR repository
    #[command(after_long_help = "\
EXAMPLES:
  adrs init                    Initialize in doc/adr (default)
  adrs init docs/decisions     Use custom directory
  adrs --ng init               Initialize with NextGen mode (YAML frontmatter)

Creates the ADR directory and an initial ADR documenting the use of ADRs.
If ADRs already exist in the directory, they are preserved.")]
    Init {
        /// Directory to store ADRs [default: doc/adr]
        #[arg(default_value = "doc/adr")]
        directory: PathBuf,
    },

    /// Create a new ADR
    #[command(after_long_help = "\
EXAMPLES:
  adrs new \"Use PostgreSQL for persistence\"      Basic ADR
  adrs new --format madr \"Use React\"             MADR format with structured sections
  adrs new --supersedes 2 \"Use MySQL instead\"    Supersede ADR 2
  adrs new --link \"2:Amends:Amended by\" \"...\"    Create with link to ADR 2
  adrs new --status accepted \"Already decided\"   Start with accepted status
  adrs new --no-edit \"Quick note\"                Create without opening editor

NEXTGEN MODE (--ng):
  adrs --ng new -t api,security \"Auth Design\"   Create with tags (requires --ng)
  adrs --ng new \"My Decision\"                   Enable YAML frontmatter metadata

LINK FORMAT:
  The --link option uses format: TARGET:KIND:REVERSE_KIND
  Example: \"2:Amends:Amended by\" links to ADR 2 with bidirectional links")]
    New {
        /// Title of the ADR
        title: String,

        /// Supersede an existing ADR by number
        #[arg(short, long, value_name = "NUMBER")]
        supersedes: Option<u32>,

        /// Link to another ADR (format: "TARGET:KIND:REVERSE_KIND")
        #[arg(short, long, value_name = "LINK")]
        link: Option<String>,

        /// Template format: nygard, madr [default: nygard]
        #[arg(short, long, value_name = "FORMAT")]
        format: Option<String>,

        /// Template variant: full, minimal, bare [default: full]
        #[arg(short, long, value_name = "VARIANT")]
        variant: Option<String>,

        /// Path to a custom template file (overrides --format/--variant and config)
        #[arg(long, value_name = "PATH")]
        template: Option<PathBuf>,

        /// Initial status [default: Proposed]
        #[arg(long, value_name = "STATUS")]
        status: Option<String>,

        /// Tags for categorization (comma-separated, requires --ng)
        #[arg(short = 't', long, value_name = "TAGS", value_delimiter = ',')]
        tags: Option<Vec<String>>,

        /// Create ADR without opening editor (for scripting/CI)
        #[arg(long)]
        no_edit: bool,
    },

    /// Edit an existing ADR
    Edit {
        /// ADR number or title (supports fuzzy matching)
        adr: String,
    },

    /// List all ADRs
    #[command(after_long_help = "\
EXAMPLES:
  adrs list                              List all ADRs (default format)
  adrs list -l                           Detailed view with status and date
  adrs list --status accepted            Show only accepted ADRs
  adrs list --since 2024-01-01           ADRs created since Jan 1, 2024
  adrs list --until 2024-06-30           ADRs created before July 2024
  adrs list --tag security               Filter by tag (requires --ng mode)
  adrs list --decider \"Alice\"            Filter by decision maker (MADR)

COMBINING FILTERS:
  adrs list -l --status accepted --since 2024-01-01
  adrs list --tag api --status proposed")]
    List {
        /// Filter by status (e.g., proposed, accepted, deprecated, superseded)
        #[arg(short, long, value_name = "STATUS")]
        status: Option<String>,

        /// Filter by date (show ADRs from this date onwards, YYYY-MM-DD)
        #[arg(long, value_name = "DATE")]
        since: Option<String>,

        /// Filter by date (show ADRs up to this date, YYYY-MM-DD)
        #[arg(long, value_name = "DATE")]
        until: Option<String>,

        /// Filter by decision maker (MADR format)
        #[arg(long, value_name = "NAME")]
        decider: Option<String>,

        /// Filter by tag
        #[arg(short = 't', long, value_name = "TAG")]
        tag: Option<String>,

        /// Show detailed output (number, status, date, title)
        #[arg(short = 'l', long)]
        long: bool,
    },

    /// Search ADRs for matching content
    #[command(after_long_help = "\
EXAMPLES:
  adrs search postgres                   Search all content for 'postgres'
  adrs search -t database                Search titles only
  adrs search --status accepted auth     Search accepted ADRs for 'auth'
  adrs search -c PostgreSQL              Case-sensitive search

TIPS:
  - Search is case-insensitive by default
  - Searches both title and full content unless -t is used
  - Combine with --status to narrow results")]
    Search {
        /// Search query
        query: String,

        /// Search titles only
        #[arg(short = 't', long)]
        title: bool,

        /// Filter by status
        #[arg(short, long, value_name = "STATUS")]
        status: Option<String>,

        /// Case-sensitive search
        #[arg(short = 'c', long)]
        case_sensitive: bool,
    },

    /// Link two ADRs together
    #[command(after_long_help = "\
EXAMPLES:
  adrs link 3 Supersedes 1               ADR 3 supersedes ADR 1
  adrs link 5 Amends 2                   ADR 5 amends ADR 2
  adrs link 4 \"Relates to\" 3             ADR 4 relates to ADR 3

CUSTOM REVERSE LINK:
  adrs link 3 Extends 1 \"Extended by\"    Specify custom reverse link

COMMON LINK TYPES (reverse auto-derived):
  Supersedes    ->  Superseded by
  Amends        ->  Amended by
  Relates to    ->  Relates to (symmetric)

The reverse link is automatically added to the target ADR.")]
    Link {
        /// Source ADR number
        source: u32,

        /// Link description (e.g., "Amends", "Supersedes", "Relates to")
        link: String,

        /// Target ADR number
        target: u32,

        /// Reverse link description (auto-derived if omitted)
        reverse_link: Option<String>,
    },

    /// Change an ADR's status
    #[command(after_long_help = "\
EXAMPLES:
  adrs status 3 accepted                 Mark ADR 3 as accepted
  adrs status 2 deprecated               Mark ADR 2 as deprecated
  adrs status 1 superseded --by 5        Mark ADR 1 as superseded by ADR 5
  adrs status 4 rejected                 Mark ADR 4 as rejected
  adrs status 3 \"In Review\"              Use custom status

STANDARD STATUSES:
  proposed      Initial state (default for new ADRs)
  accepted      Decision has been approved
  deprecated    No longer recommended but not replaced
  superseded    Replaced by another ADR (use --by)
  rejected      Decision was not approved

Note: Use --by with 'superseded' to create a link to the replacing ADR.")]
    Status {
        /// ADR number
        adr: u32,

        /// New status (proposed, accepted, deprecated, superseded, rejected, or custom)
        status: String,

        /// For 'superseded' status: the ADR number that supersedes this one
        #[arg(long, value_name = "NUMBER")]
        by: Option<u32>,
    },

    /// Show configuration
    Config,

    /// Check repository health
    Doctor,

    /// Generate documentation
    Generate {
        #[command(subcommand)]
        command: GenerateCommands,
    },

    /// Export ADRs to different formats
    Export {
        #[command(subcommand)]
        command: ExportCommands,
    },

    /// Import ADRs from different formats
    Import {
        #[command(subcommand)]
        command: ImportCommands,
    },

    /// Manage ADR templates
    Template {
        #[command(subcommand)]
        command: TemplateCommands,
    },

    /// Generate shell completions
    #[command(after_long_help = "\
EXAMPLES:
  adrs completions bash > ~/.bash_completion.d/adrs
  adrs completions zsh > ~/.zfunc/_adrs
  adrs completions fish > ~/.config/fish/completions/adrs.fish
  adrs completions powershell > _adrs.ps1

BASH:
  Add to ~/.bashrc:
    source ~/.bash_completion.d/adrs

ZSH:
  Add to ~/.zshrc (before compinit):
    fpath=(~/.zfunc $fpath)
    autoload -Uz compinit && compinit

FISH:
  Completions are loaded automatically from ~/.config/fish/completions/")]
    Completions {
        /// Shell to generate completions for
        #[arg(value_enum)]
        shell: ShellArg,
    },

    /// Start MCP server for AI agent integration (included by default)
    #[cfg(feature = "mcp")]
    #[command(after_long_help = "\
Starts an MCP (Model Context Protocol) server on stdio for AI agent integration.

TOOLS PROVIDED (15):
  Read-only:
    list_adrs, get_adr, search_adrs, get_repository_info, get_related_adrs,
    validate_adr, get_adr_sections, compare_adrs, suggest_tags
  Write:
    create_adr, update_status, update_content, update_tags, link_adrs,
    bulk_update_status

USAGE WITH CLAUDE:
  Add to your Claude Desktop config (claude_desktop_config.json):
  {
    \"mcpServers\": {
      \"adrs\": {
        \"command\": \"adrs\",
        \"args\": [\"mcp\", \"serve\"],
        \"cwd\": \"/path/to/your/project\"
      }
    }
  }

The server reads ADRs from the current working directory's repository.")]
    Mcp {
        #[command(subcommand)]
        command: McpCommands,
    },

    /// Show quick reference for common workflows
    #[command(alias = "qr")]
    Cheatsheet,
}

#[cfg(feature = "mcp")]
#[derive(Subcommand)]
enum McpCommands {
    /// Start the MCP server
    #[command(after_long_help = "\
EXAMPLES:
  adrs mcp serve                         Start on stdio (for Claude Desktop)
  adrs mcp serve --http 127.0.0.1:3000   Start HTTP server (requires mcp-http feature)

STDIO MODE (default):
  Claude Desktop spawns this process. Server lifecycle tied to Claude session.

HTTP MODE (--http):
  Server runs independently. Can restart without restarting Claude.
  Configure Claude with: {\"url\": \"http://127.0.0.1:3000/mcp\"}

Build with HTTP support:
  cargo build --release --features mcp-http")]
    Serve {
        /// Run HTTP server on this address instead of stdio
        #[cfg(feature = "mcp-http")]
        #[arg(long, value_name = "ADDR")]
        http: Option<std::net::SocketAddr>,
    },
}

/// Shell types for completion generation
#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]
enum ShellArg {
    /// Bourne Again Shell
    Bash,
    /// Z Shell
    Zsh,
    /// Fish Shell
    Fish,
    /// PowerShell
    Powershell,
    /// Elvish Shell
    Elvish,
}

#[derive(Subcommand)]
enum GenerateCommands {
    /// Generate a table of contents
    #[command(after_long_help = "\
EXAMPLES:
  adrs generate toc                      Generate markdown TOC
  adrs generate toc > doc/adr/README.md  Save to README
  adrs generate toc --ordered            Use numbered list (1. 2. 3.)
  adrs generate toc --prefix ./          Adjust link paths
  adrs generate toc --intro header.md    Prepend content from file")]
    Toc {
        /// Use ordered list (1. 2. 3.)
        #[arg(short, long)]
        ordered: bool,

        /// Prepend content from file
        #[arg(short, long, value_name = "FILE")]
        intro: Option<PathBuf>,

        /// Append content from file
        #[arg(short = 'O', long, value_name = "FILE")]
        outro: Option<PathBuf>,

        /// Prefix for ADR links
        #[arg(short, long, value_name = "PREFIX")]
        prefix: Option<String>,
    },

    /// Generate a Graphviz graph
    #[command(after_long_help = "\
EXAMPLES:
  adrs generate graph                    Generate DOT format graph
  adrs generate graph | dot -Tpng > g.png  Render as PNG
  adrs generate graph --prefix https://example.com/adr/
                                         Add URLs to nodes
  adrs generate graph -e html            Use .html extension for links")]
    Graph {
        /// Prefix for node URLs
        #[arg(short, long, value_name = "PREFIX")]
        prefix: Option<String>,

        /// File extension for links [default: md]
        #[arg(short, long, default_value = "md")]
        extension: String,
    },

    /// Generate an mdbook
    #[command(after_long_help = "\
EXAMPLES:
  adrs generate book                     Generate in ./book directory
  adrs generate book -o docs/adr-book    Custom output directory
  adrs generate book -t \"Our ADRs\"       Set book title
  cd book && mdbook serve                Preview the generated book")]
    Book {
        /// Output directory [default: book]
        #[arg(short, long, default_value = "book")]
        output: PathBuf,

        /// Book title
        #[arg(short, long)]
        title: Option<String>,

        /// Book description
        #[arg(short, long)]
        description: Option<String>,
    },
}

#[derive(Subcommand)]
enum ExportCommands {
    /// Export ADRs to JSON-ADR format
    #[command(after_long_help = "\
EXAMPLES:
  adrs export json                       Export all ADRs as JSON array
  adrs export json --pretty              Pretty-printed JSON output
  adrs export json 3                     Export only ADR 3
  adrs export json --dir ./adrs          Export from directory (no repo needed)

FOR DOCUMENTATION/CATALOGS:
  adrs export json --metadata-only       Export metadata without full content
  adrs export json --base-url https://github.com/org/repo/blob/main/doc/adr
                                         Include source URLs in export

PIPING:
  adrs export json --pretty > adrs.json  Save to file
  adrs export json | jq '.[] | .title'   Process with jq")]
    Json {
        /// Export a single ADR by number
        #[arg(value_name = "NUMBER")]
        adr: Option<u32>,

        /// Export from a directory (no repository required)
        #[arg(short, long, value_name = "PATH")]
        dir: Option<PathBuf>,

        /// Pretty-print the JSON output
        #[arg(short, long)]
        pretty: bool,

        /// Export metadata only (excludes content, includes source_uri)
        #[arg(long)]
        metadata_only: bool,

        /// Base URL for source_uri (e.g., https://github.com/org/repo/blob/main/doc/adr)
        #[arg(long, value_name = "URL")]
        base_url: Option<String>,
    },
}

#[derive(Subcommand)]
enum ImportCommands {
    /// Import ADRs from JSON-ADR format
    #[command(after_long_help = "\
EXAMPLES:
  adrs import json adrs.json             Import from JSON file
  adrs import json --dry-run adrs.json   Preview without writing files
  adrs import json --overwrite adrs.json Replace existing ADRs
  cat adrs.json | adrs import json -     Import from stdin

MERGING REPOSITORIES:
  adrs import json --renumber external.json
                                         Append ADRs with new numbers
  adrs import json --renumber --dry-run external.json
                                         Preview renumbering

OPTIONS:
  --dry-run     See what would be imported without making changes
  --renumber    Assign new numbers starting after existing ADRs
                (also available as --append)
  --overwrite   Replace existing files instead of skipping
  --ng          Use YAML frontmatter in imported files")]
    Json {
        /// JSON-ADR file to import (use "-" for stdin)
        #[arg(value_name = "FILE")]
        file: PathBuf,

        /// Import to a directory (no repository required)
        #[arg(short, long, value_name = "PATH")]
        dir: Option<PathBuf>,

        /// Overwrite existing files
        #[arg(short, long)]
        overwrite: bool,

        /// Renumber ADRs starting from next available number (append to existing ADRs)
        #[arg(short, long, alias = "append")]
        renumber: bool,

        /// Preview import without writing files
        #[arg(long)]
        dry_run: bool,

        /// Use next-gen mode with YAML frontmatter
        #[arg(long)]
        ng: bool,
    },
}

#[derive(Subcommand)]
enum TemplateCommands {
    /// List available templates
    List,

    /// Show a template's content
    Show {
        /// Template format: nygard, madr
        format: String,

        /// Template variant: full, minimal, bare, bare-minimal
        #[arg(short, long, value_name = "VARIANT")]
        variant: Option<String>,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    let start_dir = cli
        .working_dir
        .clone()
        .unwrap_or_else(|| std::env::current_dir().unwrap_or_default());

    match cli.command {
        Commands::Init { directory } => commands::init(&start_dir, directory, cli.ng),
        Commands::New {
            title,
            supersedes,
            link,
            format,
            variant,
            template,
            status,
            tags,
            no_edit,
        } => {
            let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
            commands::new(
                &discovered.root,
                cli.ng,
                title,
                supersedes,
                link,
                format,
                variant,
                template,
                status,
                tags,
                no_edit,
                &discovered.config,
            )
        }
        Commands::Edit { adr } => {
            let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
            commands::edit(&discovered.root, &adr)
        }
        Commands::List {
            status,
            since,
            until,
            decider,
            tag,
            long,
        } => {
            let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
            commands::list(&discovered.root, status, since, until, decider, tag, long)
        }
        Commands::Search {
            query,
            title,
            status,
            case_sensitive,
        } => {
            let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
            commands::search(&discovered.root, &query, title, status, case_sensitive)
        }
        Commands::Link {
            source,
            link,
            target,
            reverse_link,
        } => {
            let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
            commands::link(
                &discovered.root,
                source,
                &link,
                target,
                reverse_link.as_deref(),
            )
        }
        Commands::Status { adr, status, by } => {
            let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
            commands::status(&discovered.root, adr, &status, by)
        }
        Commands::Config => {
            let discovered = discover(&start_dir).ok();
            commands::config_with_discovery(&start_dir, discovered)
        }
        Commands::Doctor => {
            let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
            commands::doctor(&discovered.root)
        }
        Commands::Generate { command } => {
            let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
            match command {
                GenerateCommands::Toc {
                    ordered,
                    intro,
                    outro,
                    prefix,
                } => commands::generate_toc(&discovered.root, ordered, intro, outro, prefix),
                GenerateCommands::Graph { prefix, extension } => {
                    commands::generate_graph(&discovered.root, prefix, &extension)
                }
                GenerateCommands::Book {
                    output,
                    title,
                    description,
                } => commands::generate_book(&discovered.root, &output, title, description),
            }
        }
        Commands::Export { command } => match command {
            ExportCommands::Json {
                adr,
                dir,
                pretty,
                metadata_only,
                base_url,
            } => {
                if let Some(ref dir_path) = dir {
                    // Export from arbitrary directory - no repo needed
                    commands::export_json(
                        &start_dir,
                        adr,
                        Some(dir_path),
                        pretty,
                        metadata_only,
                        base_url,
                    )
                } else {
                    // Export from repository
                    let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
                    commands::export_json(
                        &discovered.root,
                        adr,
                        None,
                        pretty,
                        metadata_only,
                        base_url,
                    )
                }
            }
        },
        Commands::Import { command } => match command {
            ImportCommands::Json {
                file,
                dir,
                overwrite,
                renumber,
                dry_run,
                ng,
            } => {
                if let Some(ref dir_path) = dir {
                    // Import to arbitrary directory - no repo needed
                    commands::import_json(
                        &start_dir,
                        &file,
                        Some(dir_path),
                        overwrite,
                        renumber,
                        dry_run,
                        ng,
                    )
                } else {
                    // Import to repository
                    let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
                    commands::import_json(
                        &discovered.root,
                        &file,
                        None,
                        overwrite,
                        renumber,
                        dry_run,
                        ng,
                    )
                }
            }
        },
        Commands::Template { command } => match command {
            TemplateCommands::List => commands::template_list(),
            TemplateCommands::Show { format, variant } => {
                commands::template_show(&format, variant.as_deref())
            }
        },
        Commands::Completions { shell } => {
            let shell = match shell {
                ShellArg::Bash => Shell::Bash,
                ShellArg::Zsh => Shell::Zsh,
                ShellArg::Fish => Shell::Fish,
                ShellArg::Powershell => Shell::PowerShell,
                ShellArg::Elvish => Shell::Elvish,
            };
            let mut cmd = Cli::command();
            generate(shell, &mut cmd, "adrs", &mut io::stdout());
            Ok(())
        }
        #[cfg(all(feature = "mcp", not(feature = "mcp-http")))]
        Commands::Mcp { command } => match command {
            McpCommands::Serve {} => {
                let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
                tokio::runtime::Runtime::new()
                    .context("Failed to create tokio runtime")?
                    .block_on(mcp::serve_stdio(discovered.root))
                    .context("MCP server error")
            }
        },
        #[cfg(feature = "mcp-http")]
        Commands::Mcp { command } => match command {
            McpCommands::Serve { http } => {
                let discovered = discover_or_error(&start_dir, cli.working_dir.is_some())?;
                let runtime =
                    tokio::runtime::Runtime::new().context("Failed to create tokio runtime")?;

                if let Some(addr) = http {
                    runtime
                        .block_on(mcp::serve_http(discovered.root, addr))
                        .context("MCP HTTP server error")
                } else {
                    runtime
                        .block_on(mcp::serve_stdio(discovered.root))
                        .context("MCP server error")
                }
            }
        },
        Commands::Cheatsheet => {
            print_cheatsheet();
            Ok(())
        }
    }
}

fn print_cheatsheet() {
    print!(
        r#"ADR CHEATSHEET
==============

GETTING STARTED
  adrs init                              Create ADR repository in doc/adr
  adrs init docs/decisions               Use custom directory
  adrs --ng init                         Enable NextGen mode (YAML frontmatter)

CREATING ADRs
  adrs new "Use PostgreSQL"              Create new ADR (opens editor)
  adrs new --no-edit "Quick Decision"    Create without editor (CI/scripts)
  adrs new --format madr "..."           Use MADR format
  adrs new --status accepted "..."       Start as accepted
  adrs --ng new -t api,db "..."          Add tags (requires --ng)

SUPERSEDING & LINKING
  adrs new --supersedes 2 "Use MySQL"    Create ADR that supersedes #2
  adrs link 3 Supersedes 1               Link: ADR 3 supersedes ADR 1
  adrs link 3 Amends 1                   Link: ADR 3 amends ADR 1
  adrs link 3 "Relates to" 2             Symmetric relationship

MANAGING STATUS
  adrs status 3 accepted                 Mark as accepted
  adrs status 2 deprecated               Mark as deprecated
  adrs status 1 superseded --by 3        Mark as superseded by ADR 3

VIEWING & SEARCHING
  adrs list                              List all ADRs
  adrs list -l                           Detailed view (status, date)
  adrs list --status accepted            Filter by status
  adrs list --since 2024-01-01           Filter by date
  adrs search postgres                   Search content
  adrs search -t database                Search titles only

GENERATING DOCS
  adrs generate toc > doc/adr/README.md  Create table of contents
  adrs generate graph | dot -Tpng > g.png  Create relationship graph
  adrs generate book                     Create mdbook

IMPORT/EXPORT
  adrs export json --pretty > adrs.json  Export to JSON
  adrs import json external.json         Import from JSON
  adrs import json --renumber ext.json   Append with new numbers
  adrs import json --dry-run ext.json    Preview import

CONFIGURATION
  adrs config                            Show current config
  adrs doctor                            Check repository health
  adrs template list                     Show available templates

For detailed help: adrs <command> --help
Documentation: https://joshrotenberg.com/adrs/
"#
    );
}

/// Discover config or return a helpful error.
fn discover_or_error(
    start_dir: &std::path::Path,
    explicit_dir: bool,
) -> Result<adrs_core::DiscoveredConfig> {
    let discovered = discover(start_dir).context(if explicit_dir {
        "No ADR repository found in the specified directory. Run 'adrs init' first."
    } else {
        "No ADR repository found. Run 'adrs init' to create one, or use '-C' to specify a directory."
    })?;

    if matches!(discovered.source, ConfigSource::Default)
        && !start_dir.join("doc/adr").exists()
        && !explicit_dir
    {
        anyhow::bail!(
            "No ADR repository found. Run 'adrs init' to create one, or use '-C' to specify a directory."
        );
    }

    Ok(discovered)
}