cli.v

3.0 KB · raw

1//! Command line entry: dispatch table, usage text and exit codes.2//!3//! Exit codes are fixed across every command: 0 success, 1 refusal or error,4//! 2 bad usage. Each command returns its code; nothing else calls `exit`.56module cli78import src.fmt910// Read from VERSION at build time, so the binary and the manifest cannot drift.11pub const version = $embed_file('../../VERSION').to_string().trim_space()1213// The help screen. It is built rather than embedded in a constant, because the14// colour follows NO_COLOR, KOH_NO_COLOR and whether the output is a terminal.15// Flags are not shown here: `koh man <command>` documents every one.16pub fn usage_text() string {17	groups := [18		[19			['save', 'snapshot your work'],20			['checkpoint', 'mark a place to come back to'],21			['status', 'what changed'],22			['log', 'history'],23			['view', 'browse saves and checkpoints'],24			['diff', 'show changes'],25		],26		[27			['undo', 'step back'],28			['revert', 'throw away unsaved changes'],29			['load', 'go to any save'],30		],31		[32			['offer', 'send to your remote'],33			['steal', 'bring in from your remote'],34		],35	]36	mut text := '${fmt.light('koh ${version} — version control for the solo developer')}\n'37	for group in groups {38		text += '\n'39		for row in group {40			text += '  ${fmt.gold(row[0])}${' '.repeat(12 - row[0].len)}${fmt.muted(row[1])}\n'41		}42	}43	// The list grows as commands land, and the screen keeps to 80 columns, so it44	// wraps rather than running off the edge.45	text += '\n  ${fmt.dim('also: remote · flag · pluck · peek · build · update')}\n'46	text += '        ${fmt.dim('init · doctor · strip · migrate')}\n'47	text += '\n  ${fmt.dim('koh man <command>  for options and examples')}\n'48	return text49}5051pub fn run(argv []string) int {52	if argv.len < 2 {53		eprintln(usage_text())54		return 255	}56	hand_over(argv)57	rest := argv[2..]58	match argv[1] {59		'--version', '-v' {60			println('koh ${version}')61			return 062		}63		'--help', '-h' {64			println(usage_text())65			return 066		}67		'init' {68			return cmd_init(rest)69		}70		'save' {71			return cmd_save(rest, false)72		}73		'checkpoint' {74			return cmd_save(rest, true)75		}76		'status' {77			return cmd_status(rest)78		}79		'log' {80			return cmd_log(rest)81		}82		'view' {83			return cmd_view(rest)84		}85		'diff' {86			return cmd_diff(rest)87		}88		'undo' {89			return cmd_undo(rest)90		}91		'revert' {92			return cmd_revert(rest)93		}94		'load' {95			return cmd_load(rest)96		}97		'flag' {98			return cmd_flag(rest)99		}100		'pluck' {101			return cmd_pluck(rest)102		}103		'peek' {104			return cmd_peek(rest)105		}106		'doctor' {107			return cmd_doctor(rest)108		}109		'strip' {110			return cmd_strip(rest)111		}112		'remote' {113			return cmd_remote(rest)114		}115		'offer' {116			return cmd_offer(rest)117		}118		'steal' {119			return cmd_steal(rest)120		}121		'build' {122			return cmd_build(rest)123		}124		'update' {125			return cmd_update(rest)126		}127		'migrate' {128			return cmd_migrate(rest)129		}130		'man', 'help' {131			return cmd_man(rest)132		}133		else {134			return usage_error('unknown command "${argv[1]}" — run "koh --help" for the command list')135		}136	}137}