Class: Carson::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/carson/cli.rb

Class Method Summary collapse

Class Method Details

.build_parserObject



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
# File 'lib/carson/cli.rb', line 55

def self.build_parser
	OptionParser.new do |parser|
		parser.banner = "Usage: carson <command> [options]"
		parser.separator ""
		parser.separator "Repository governance and workflow automation for coding agents."
		parser.separator ""
		parser.separator "Commands:"
		parser.separator "    status       Show repository state (branch, PRs, worktrees)"
		parser.separator "    setup        Initialise Carson configuration"
		parser.separator "    audit        Run pre-commit health checks"
		parser.separator "    sync         Sync local main with remote"
		parser.separator "    deliver      Push, create PR, and optionally merge"
		parser.separator "    prune        Remove stale local branches"
		parser.separator "    worktree     Manage isolated coding worktrees"
		parser.separator "    housekeep    Sync, reap worktrees, and prune branches"
		parser.separator "    repos        List governed repositories"
		parser.separator "    onboard      Register a repository for governance"
		parser.separator "    offboard     Remove a repository from governance"
		parser.separator "    refresh      Re-install hooks and configuration"
		parser.separator "    template     Manage canonical template files"
		parser.separator "    review       Manage PR review workflow"
		parser.separator "    govern       Portfolio-level PR triage loop"
		parser.separator "    version      Show Carson version"
		parser.separator ""
		parser.separator "Run `carson <command> --help` for details on a specific command."
	end
end

.dispatch(parsed:, runtime:) ⇒ Object

— dispatch —



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
# File 'lib/carson/cli.rb', line 690

def self.dispatch( parsed:, runtime: )
	command = parsed.fetch( :command )
	return Runtime::EXIT_ERROR if command == :invalid

	case command
	when "status"
		runtime.status!( json_output: parsed.fetch( :json, false ) )
	when "setup"
		runtime.setup!( cli_choices: parsed.fetch( :cli_choices, {} ) )
	when "audit"
		runtime.audit!( json_output: parsed.fetch( :json, false ) )
	when "sync"
		runtime.sync!( json_output: parsed.fetch( :json, false ) )
	when "prune"
		runtime.prune!( json_output: parsed.fetch( :json, false ) )
	when "prune:all"
		runtime.prune_all!
	when "worktree:create"
		runtime.worktree_create!( name: parsed.fetch( :worktree_name ), json_output: parsed.fetch( :json, false ) )
	when "worktree:remove"
		runtime.worktree_remove!( worktree_path: parsed.fetch( :worktree_path ), force: parsed.fetch( :force, false ), json_output: parsed.fetch( :json, false ) )
	when "onboard"
		runtime.onboard!
	when "refresh"
		runtime.refresh!
	when "refresh:all"
		runtime.refresh_all!
	when "offboard"
		runtime.offboard!
	when "template:check"
		runtime.template_check!
	when "template:apply"
		runtime.template_apply!( push_prep: parsed.fetch( :push_prep, false ) )
	when "deliver"
		runtime.deliver!(
			merge: parsed.fetch( :merge, false ),
			title: parsed.fetch( :title, nil ),
			body_file: parsed.fetch( :body_file, nil ),
			json_output: parsed.fetch( :json, false )
		)
	when "review:gate"
		runtime.review_gate!
	when "review:sweep"
		runtime.review_sweep!
	when "repos"
		runtime.repos!( json_output: parsed.fetch( :json, false ) )
	when "housekeep"
		runtime.housekeep!( json_output: parsed.fetch( :json, false ) )
	when "housekeep:target"
		runtime.housekeep_target!( target: parsed.fetch( :target ), json_output: parsed.fetch( :json, false ) )
	when "housekeep:all"
		runtime.housekeep_all!( json_output: parsed.fetch( :json, false ) )
	when "govern"
		runtime.govern!(
			dry_run: parsed.fetch( :dry_run, false ),
			json_output: parsed.fetch( :json, false ),
			loop_seconds: parsed.fetch( :loop_seconds, nil )
		)
	when "template:check:all"
		runtime.template_check_all!
	when "audit:all"
		runtime.audit_all!
	when "sync:all"
		runtime.sync_all!
	when "status:all"
		runtime.status_all!( json_output: parsed.fetch( :json, false ) )
	else
		runtime.send( :puts_line, "Unknown command: #{command}" )
		Runtime::EXIT_ERROR
	end
end

.parse_args(arguments:, output:, error:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/carson/cli.rb', line 40

def self.parse_args( arguments:, output:, error: )
	verbose = arguments.delete( "--verbose" ) ? true : false
	parser = build_parser
	preset = parse_preset_command( arguments: arguments, output: output, parser: parser )
	return preset.merge( verbose: verbose ) unless preset.nil?

	command = arguments.shift
	result = parse_command( command: command, arguments: arguments, error: error )
	result.merge( verbose: verbose )
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	error.puts parser
	{ command: :invalid }
end

.parse_audit_command(arguments:, error:) ⇒ Object

— audit —



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
# File 'lib/carson/cli.rb', line 442

def self.parse_audit_command( arguments:, error: )
	options = { json: false, all: false }
	audit_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson audit [--all] [--json]"
		parser.separator ""
		parser.separator "Run pre-commit health checks on the repository."
		parser.separator "Validates hooks, main-branch sync, PR status, and CI baseline."
		parser.separator "Exits with a non-zero status when policy violations are found."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--all", "Audit all governed repositories" ) { options[ :all ] = true }
		parser.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson audit           Check repository health (also the default command)"
		parser.separator "    carson audit --json    Structured output for agent consumption"
		parser.separator "    carson audit --all     Audit all governed repos"
	end
	audit_parser.parse!( arguments )
	unless arguments.empty?
		error.puts "#{BADGE} Unexpected arguments for audit: #{arguments.join( ' ' )}"
		return { command: :invalid }
	end
	return { command: "audit:all" } if options[ :all ]
	{ command: "audit", json: options[ :json ] }
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_command(command:, arguments:, error:) ⇒ Object



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
# File 'lib/carson/cli.rb', line 95

def self.parse_command( command:, arguments:, error: )
	case command
	when "version"
		{ command: "version" }
	when "setup"
		parse_setup_command( arguments: arguments, error: error )
	when "onboard"
		parse_onboard_command( arguments: arguments, error: error )
	when "offboard"
		parse_offboard_command( arguments: arguments, error: error )
	when "refresh"
		parse_refresh_command( arguments: arguments, error: error )
	when "template"
		parse_template_subcommand( arguments: arguments, error: error )
	when "prune"
		parse_prune_command( arguments: arguments, error: error )
	when "worktree"
		parse_worktree_subcommand( arguments: arguments, error: error )
	when "repos"
		parse_repos_command( arguments: arguments, error: error )
	when "housekeep"
		parse_housekeep_command( arguments: arguments, error: error )
	when "review"
		parse_review_subcommand( arguments: arguments, error: error )
	when "audit"
		parse_audit_command( arguments: arguments, error: error )
	when "sync"
		parse_sync_command( arguments: arguments, error: error )
	when "status"
		parse_status_command( arguments: arguments, error: error )
	when "deliver"
		parse_deliver_command( arguments: arguments, error: error )
	when "govern"
		parse_govern_subcommand( arguments: arguments, error: error )
	else
		{ command: command }
	end
end

.parse_deliver_command(arguments:, error:) ⇒ Object

— deliver —



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
# File 'lib/carson/cli.rb', line 536

def self.parse_deliver_command( arguments:, error: )
	options = { merge: false, json: false, title: nil, body_file: nil }
	deliver_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson deliver [--merge] [--json] [--title TITLE] [--body-file PATH]"
		parser.separator ""
		parser.separator "Push the current branch, create a pull request, and optionally merge."
		parser.separator "Collapses the manual push → PR → merge flow into a single command."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--merge", "Also merge the PR if CI passes" ) { options[ :merge ] = true }
		parser.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		parser.on( "--title TITLE", "PR title (defaults to branch name)" ) { |value| options[ :title ] = value }
		parser.on( "--body-file PATH", "File containing PR body text" ) { |value| options[ :body_file ] = value }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson deliver               Push and open a PR"
		parser.separator "    carson deliver --merge       Push, open a PR, and merge if CI passes"
	end
	deliver_parser.parse!( arguments )
	unless arguments.empty?
		error.puts "#{BADGE} Unexpected arguments for deliver: #{arguments.join( ' ' )}"
		error.puts deliver_parser
		return { command: :invalid }
	end
	{
		command: "deliver",
		merge: options.fetch( :merge ),
		json: options.fetch( :json ),
		title: options[ :title ],
		body_file: options[ :body_file ]
	}
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_govern_subcommand(arguments:, error:) ⇒ Object

— govern —



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
# File 'lib/carson/cli.rb', line 644

def self.parse_govern_subcommand( arguments:, error: )
	options = {
		dry_run: false,
		json: false,
		loop_seconds: nil
	}
	govern_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson govern [--dry-run] [--json] [--loop SECONDS]"
		parser.separator ""
		parser.separator "Portfolio-level PR triage loop."
		parser.separator "Scans governed repositories, classifies open PRs, and takes action"
		parser.separator "(merge, request review, or report). Runs once by default."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--dry-run", "Run all checks but do not merge or dispatch" ) { options[ :dry_run ] = true }
		parser.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		parser.on( "--loop SECONDS", Integer, "Run continuously, sleeping SECONDS between cycles" ) do |seconds|
			error.puts( "#{BADGE} Error: --loop must be a positive integer" ) || ( return { command: :invalid } ) if seconds < 1
			options[ :loop_seconds ] = seconds
		end
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson govern                  Triage all governed repos once"
		parser.separator "    carson govern --dry-run        Preview actions without applying them"
		parser.separator "    carson govern --loop 300       Run continuously every 5 minutes"
	end
	govern_parser.parse!( arguments )
	unless arguments.empty?
		error.puts "#{BADGE} Unexpected arguments for govern: #{arguments.join( ' ' )}"
		error.puts govern_parser
		return { command: :invalid }
	end
	{
		command: "govern",
		dry_run: options.fetch( :dry_run ),
		json: options.fetch( :json ),
		loop_seconds: options[ :loop_seconds ]
	}
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	error.puts govern_parser
	{ command: :invalid }
end

.parse_housekeep_command(arguments:, error:) ⇒ Object

— housekeep —



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
# File 'lib/carson/cli.rb', line 602

def self.parse_housekeep_command( arguments:, error: )
	options = { all: false, json: false }
	housekeep_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson housekeep [REPO] [--all] [--json]"
		parser.separator ""
		parser.separator "Run housekeeping: sync main, reap dead worktrees, and prune stale branches."
		parser.separator "Defaults to the current repository."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--all", "Housekeep all governed repositories" ) { options[ :all ] = true }
		parser.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson housekeep           Housekeep the current repository"
		parser.separator "    carson housekeep nexus     Housekeep a named governed repo"
		parser.separator "    carson housekeep --all     Housekeep all governed repos"
	end
	housekeep_parser.parse!( arguments )

	if options[ :all ] && !arguments.empty?
		error.puts "#{BADGE} --all and repo target are mutually exclusive. Use: carson housekeep --all OR carson housekeep [repo]"
		return { command: :invalid }
	end

	return { command: "housekeep:all", json: options[ :json ] } if options[ :all ]

	if arguments.length > 1
		error.puts "#{BADGE} Too many arguments for housekeep. Use: carson housekeep [repo]"
		return { command: :invalid }
	end

	target = arguments.shift
	return { command: "housekeep:target", target: target, json: options[ :json ] } if target

	{ command: "housekeep", json: options[ :json ] }
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_offboard_command(arguments:, error:) ⇒ Object



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
# File 'lib/carson/cli.rb', line 199

def self.parse_offboard_command( arguments:, error: )
	offboard_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson offboard [REPO_PATH]"
		parser.separator ""
		parser.separator "Remove a repository from Carson governance."
		parser.separator "Unregisters the repo from Carson's portfolio and removes hooks."
		parser.separator "Defaults to the current directory if no path is given."
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson offboard            Offboard the current repository"
	end
	offboard_parser.parse!( arguments )
	if arguments.length > 1
		error.puts "#{BADGE} Too many arguments for offboard. Use: carson offboard [repo_path]"
		error.puts offboard_parser
		return { command: :invalid }
	end
	repo_path = arguments.first
	{
		command: "offboard",
		repo_root: repo_path.to_s.strip.empty? ? nil : File.expand_path( repo_path )
	}
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_onboard_command(arguments:, error:) ⇒ Object

— onboard / offboard —



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
# File 'lib/carson/cli.rb', line 171

def self.parse_onboard_command( arguments:, error: )
	onboard_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson onboard [REPO_PATH]"
		parser.separator ""
		parser.separator "Register a repository for Carson governance."
		parser.separator "Detects the remote, installs hooks, applies templates, and runs initial audit."
		parser.separator "Defaults to the current directory if no path is given."
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson onboard             Onboard the current repository"
		parser.separator "    carson onboard ~/Dev/app   Onboard a specific repository"
	end
	onboard_parser.parse!( arguments )
	if arguments.length > 1
		error.puts "#{BADGE} Too many arguments for onboard. Use: carson onboard [repo_path]"
		error.puts onboard_parser
		return { command: :invalid }
	end
	repo_path = arguments.first
	{
		command: "onboard",
		repo_root: repo_path.to_s.strip.empty? ? nil : File.expand_path( repo_path )
	}
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_preset_command(arguments:, output:, parser:) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/carson/cli.rb', line 83

def self.parse_preset_command( arguments:, output:, parser: )
	first = arguments.first
	if [ "--help", "-h" ].include?( first )
		output.puts parser
		return { command: :help }
	end
	return { command: "version" } if [ "--version", "-v" ].include?( first )
	return { command: "audit" } if arguments.empty?

	nil
end

.parse_prune_command(arguments:, error:) ⇒ Object

— prune —



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/carson/cli.rb', line 271

def self.parse_prune_command( arguments:, error: )
	options = { all: false, json: false }
	prune_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson prune [--all] [--json]"
		parser.separator ""
		parser.separator "Remove stale local branches."
		parser.separator "Cleans up branches gone from the remote, orphan branches with merged PRs,"
		parser.separator "and absorbed branches whose content is already on main."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--all", "Prune all governed repositories" ) { options[ :all ] = true }
		parser.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson prune           Clean up stale branches in this repo"
		parser.separator "    carson prune --all     Clean up across all governed repos"
	end
	prune_parser.parse!( arguments )
	return { command: "prune:all", json: options[ :json ] } if options[ :all ]
	{ command: "prune", json: options[ :json ] }
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_refresh_command(arguments:, error:) ⇒ Object

— refresh —



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
# File 'lib/carson/cli.rb', line 228

def self.parse_refresh_command( arguments:, error: )
	options = { all: false }
	refresh_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson refresh [--all] [REPO_PATH]"
		parser.separator ""
		parser.separator "Re-install Carson hooks and configuration for a repository."
		parser.separator "Defaults to the current directory. Use --all to refresh all governed repos."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--all", "Refresh all governed repositories" ) { options[ :all ] = true }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson refresh             Refresh the current repository"
		parser.separator "    carson refresh --all       Refresh all governed repos"
	end
	refresh_parser.parse!( arguments )

	if options[ :all ] && !arguments.empty?
		error.puts "#{BADGE} --all and repo_path are mutually exclusive. Use: carson refresh --all OR carson refresh [repo_path]"
		error.puts refresh_parser
		return { command: :invalid }
	end

	return { command: "refresh:all" } if options[ :all ]

	if arguments.length > 1
		error.puts "#{BADGE} Too many arguments for refresh. Use: carson refresh [repo_path]"
		error.puts refresh_parser
		return { command: :invalid }
	end

	repo_path = arguments.first
	{
		command: "refresh",
		repo_root: repo_path.to_s.strip.empty? ? nil : File.expand_path( repo_path )
	}
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_repos_command(arguments:, error:) ⇒ Object

— repos —



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
# File 'lib/carson/cli.rb', line 574

def self.parse_repos_command( arguments:, error: )
	options = { json: false }
	repos_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson repos [--json]"
		parser.separator ""
		parser.separator "List all repositories governed by Carson."
		parser.separator "Shows the portfolio of repos registered via carson onboard."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson repos           List governed repositories"
		parser.separator "    carson repos --json    Structured output for agent consumption"
	end
	repos_parser.parse!( arguments )
	unless arguments.empty?
		error.puts "#{BADGE} Unexpected arguments for repos: #{arguments.join( ' ' )}"
		return { command: :invalid }
	end
	{ command: "repos", json: options[ :json ] }
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_review_subcommand(arguments:, error:) ⇒ Object

— review —



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
# File 'lib/carson/cli.rb', line 354

def self.parse_review_subcommand( arguments:, error: )
	review_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson review <gate|sweep>"
		parser.separator ""
		parser.separator "Manage PR review workflow."
		parser.separator ""
		parser.separator "Subcommands:"
		parser.separator "    gate     Check if review requirements are met for merge"
		parser.separator "    sweep    Scan and resolve pending review threads"
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson review gate     Check merge readiness"
		parser.separator "    carson review sweep    Resolve pending review threads"
	end
	review_parser.parse!( arguments )

	action = arguments.shift
	if action.to_s.strip.empty?
		error.puts "#{BADGE} Missing subcommand for review. Use: carson review gate|sweep"
		error.puts review_parser
		return { command: :invalid }
	end
	{ command: "review:#{action}" }
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_setup_command(arguments:, error:) ⇒ Object

— setup —



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
# File 'lib/carson/cli.rb', line 136

def self.parse_setup_command( arguments:, error: )
	options = {}
	setup_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson setup [--remote NAME] [--main-branch NAME] [--workflow STYLE] [--merge METHOD] [--canonical PATH]"
		parser.separator ""
		parser.separator "Initialise Carson configuration for the current repository."
		parser.separator "Detects git remote, main branch, and workflow style, then writes .carson.yml."
		parser.separator "Pass flags to override detected values."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--remote NAME", "Git remote name" ) { |value| options[ "git.remote" ] = value }
		parser.on( "--main-branch NAME", "Main branch name" ) { |value| options[ "git.main_branch" ] = value }
		parser.on( "--workflow STYLE", "Workflow style (branch or trunk)" ) { |value| options[ "workflow.style" ] = value }
		parser.on( "--merge METHOD", "Merge method (squash, rebase, or merge)" ) { |value| options[ "govern.merge.method" ] = value }
		parser.on( "--canonical PATH", "Canonical template directory path" ) { |value| options[ "template.canonical" ] = value }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson setup                            Auto-detect and write config"
		parser.separator "    carson setup --remote github            Use 'github' as the git remote"
		parser.separator "    carson setup --merge squash             Set squash as the merge method"
	end
	setup_parser.parse!( arguments )
	unless arguments.empty?
		error.puts "#{BADGE} Unexpected arguments for setup: #{arguments.join( ' ' )}"
		error.puts setup_parser
		return { command: :invalid }
	end
	{ command: "setup", cli_choices: options }
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_status_command(arguments:, error:) ⇒ Object

— status —



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
# File 'lib/carson/cli.rb', line 505

def self.parse_status_command( arguments:, error: )
	options = { json: false, all: false }
	status_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson status [--all] [--json]"
		parser.separator ""
		parser.separator "Show the current state of the repository."
		parser.separator "Reports branch, worktrees, open PRs, stale branches, and version."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--all", "Show status for all governed repositories" ) { options[ :all ] = true }
		parser.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson status           Quick overview of repository state"
		parser.separator "    carson status --json    Structured output for agent consumption"
		parser.separator "    carson status --all     Portfolio-wide status overview"
	end
	status_parser.parse!( arguments )
	unless arguments.empty?
		error.puts "#{BADGE} Unexpected arguments for status: #{arguments.join( ' ' )}"
		return { command: :invalid }
	end
	return { command: "status:all", json: options[ :json ] } if options[ :all ]
	{ command: "status", json: options[ :json ] }
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_sync_command(arguments:, error:) ⇒ Object

— sync —



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
# File 'lib/carson/cli.rb', line 474

def self.parse_sync_command( arguments:, error: )
	options = { json: false, all: false }
	sync_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson sync [--all] [--json]"
		parser.separator ""
		parser.separator "Sync the local main branch with the remote."
		parser.separator "Fetches and fast-forwards main without switching branches."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--all", "Sync all governed repositories" ) { options[ :all ] = true }
		parser.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson sync            Pull latest changes from remote main"
		parser.separator "    carson sync --json     Structured output for agent consumption"
		parser.separator "    carson sync --all      Sync all governed repos"
	end
	sync_parser.parse!( arguments )
	unless arguments.empty?
		error.puts "#{BADGE} Unexpected arguments for sync: #{arguments.join( ' ' )}"
		return { command: :invalid }
	end
	return { command: "sync:all" } if options[ :all ]
	{ command: "sync", json: options[ :json ] }
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_template_subcommand(arguments:, error:) ⇒ Object

— template —



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
# File 'lib/carson/cli.rb', line 384

def self.parse_template_subcommand( arguments:, error: )
	# Handle parent-level help or missing subcommand.
	if arguments.empty? || [ "--help", "-h" ].include?( arguments.first )
		template_parser = OptionParser.new do |parser|
			parser.banner = "Usage: carson template <check|apply> [options]"
			parser.separator ""
			parser.separator "Manage canonical template files (CI workflows, lint configs)."
			parser.separator ""
			parser.separator "Subcommands:"
			parser.separator "    check                  Show template drift without making changes"
			parser.separator "    apply [--push-prep]    Sync templates into the repository"
			parser.separator ""
			parser.separator "Examples:"
			parser.separator "    carson template check    Check for template drift"
			parser.separator "    carson template apply    Apply canonical templates"
		end

		if arguments.empty?
			error.puts "#{BADGE} Missing subcommand for template. Use: carson template check|apply"
			error.puts template_parser
			return { command: :invalid }
		end

		# Let OptionParser handle --help (prints and exits).
		template_parser.parse!( arguments )
		return { command: :help }
	end

	action = arguments.shift
	return { command: "template:check:all" } if action == "check" && arguments.include?( "--all" )
	return { command: "template:#{action}" } unless action == "apply"

	options = { push_prep: false }
	apply_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson template apply [--push-prep]"
		parser.separator ""
		parser.separator "Sync canonical template files (CI workflows, lint configs) into the repository."
		parser.separator "Copies managed files from the configured canonical directory."
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--push-prep", "Apply templates and auto-commit any managed file changes (used by pre-push hook)" ) do
			options[ :push_prep ] = true
		end
	end
	apply_parser.parse!( arguments )
	unless arguments.empty?
		error.puts "#{BADGE} Unexpected arguments for template apply: #{arguments.join( ' ' )}"
		error.puts apply_parser
		return { command: :invalid }
	end
	{ command: "template:apply", push_prep: options.fetch( :push_prep ) }
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.parse_worktree_subcommand(arguments:, error:) ⇒ Object

— worktree —



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
# File 'lib/carson/cli.rb', line 298

def self.parse_worktree_subcommand( arguments:, error: )
	options = { json: false, force: false }
	worktree_parser = OptionParser.new do |parser|
		parser.banner = "Usage: carson worktree <create|remove> <name> [options]"
		parser.separator ""
		parser.separator "Manage isolated worktrees for coding agents."
		parser.separator "Create auto-syncs main before branching. Remove guards against"
		parser.separator "unpushed commits and CWD-inside-worktree by default."
		parser.separator ""
		parser.separator "Subcommands:"
		parser.separator "    create <name>              Create a new worktree with a fresh branch"
		parser.separator "    remove <name> [--force]    Remove a worktree (--force skips safety checks)"
		parser.separator ""
		parser.separator "Options:"
		parser.on( "--json", "Machine-readable JSON output" ) { options[ :json ] = true }
		parser.on( "--force", "Skip safety checks on remove" ) { options[ :force ] = true }
		parser.separator ""
		parser.separator "Examples:"
		parser.separator "    carson worktree create feature-x    Create an isolated worktree"
		parser.separator "    carson worktree remove feature-x    Remove after work is pushed"
	end
	worktree_parser.parse!( arguments )

	action = arguments.shift
	if action.to_s.strip.empty?
		error.puts "#{BADGE} Missing subcommand for worktree. Use: carson worktree create|remove <name>"
		error.puts worktree_parser
		return { command: :invalid }
	end

	case action
	when "create"
		name = arguments.shift
		if name.to_s.strip.empty?
			error.puts "#{BADGE} Missing name for worktree create. Use: carson worktree create <name>"
			return { command: :invalid }
		end
		{ command: "worktree:create", worktree_name: name, json: options[ :json ] }
	when "remove"
		worktree_path = arguments.shift
		if worktree_path.to_s.strip.empty?
			error.puts "#{BADGE} Missing path for worktree remove. Use: carson worktree remove <name-or-path>"
			return { command: :invalid }
		end
		{ command: "worktree:remove", worktree_path: worktree_path, force: options[ :force ], json: options[ :json ] }
	else
		error.puts "#{BADGE} Unknown worktree subcommand: #{action}. Use: carson worktree create|remove <name>"
		{ command: :invalid }
	end
rescue OptionParser::ParseError => exception
	error.puts "#{BADGE} #{exception.message}"
	{ command: :invalid }
end

.start(arguments:, repo_root:, tool_root:, output:, error:) ⇒ Object



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
# File 'lib/carson/cli.rb', line 6

def self.start( arguments:, repo_root:, tool_root:, output:, error: )
	parsed = parse_args( arguments: arguments, output: output, error: error )
	command = parsed.fetch( :command )
	return Runtime::EXIT_OK if command == :help

	if command == "version"
		output.puts "#{BADGE} #{Carson::VERSION}"
		return Runtime::EXIT_OK
	end

	if %w[repos refresh:all prune:all housekeep:all housekeep:target template:check:all audit:all sync:all status:all].include?( command )
		verbose = parsed.fetch( :verbose, false )
		runtime = Runtime.new( repo_root: repo_root, tool_root: tool_root, output: output, error: error, verbose: verbose )
		return dispatch( parsed: parsed, runtime: runtime )
	end

	target_repo_root = parsed.fetch( :repo_root, nil )
	target_repo_root = repo_root if target_repo_root.to_s.strip.empty?
	unless Dir.exist?( target_repo_root )
		error.puts "#{BADGE} ERROR: repository path does not exist: #{target_repo_root}"
		return Runtime::EXIT_ERROR
	end

	verbose = parsed.fetch( :verbose, false )
	runtime = Runtime.new( repo_root: target_repo_root, tool_root: tool_root, output: output, error: error, verbose: verbose )
	dispatch( parsed: parsed, runtime: runtime )
rescue ConfigError => exception
	error.puts "#{BADGE} CONFIG ERROR: #{exception.message}"
	Runtime::EXIT_ERROR
rescue StandardError => exception
	error.puts "#{BADGE} ERROR: #{exception.message}"
	Runtime::EXIT_ERROR
end