cli.v

2.8 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	commands := [18		['save', 'snapshot your work'],19		['checkpoint', 'mark a place to come back to'],20		['status', 'where things stand'],21		['view', 'browse saves and checkpoints'],22		['diff', 'what changed'],23		['undo', 'step back'],24		['revert', 'throw away unsaved changes'],25		['load', 'go to any save'],26		['offer', 'send to your remote'],27		['steal', 'bring in from your remote'],28	]29	mut text := '${fmt.light('koh ${version} — version control for the solo developer')}\n'30	// A blank line between commands, so the eye lands on one line at a time, and31	// the list grows as commands land.32	for row in commands {33		text += '\n  ${fmt.gold(row[0])}${' '.repeat(12 - row[0].len)}${fmt.muted(row[1])}\n'34	}35	text += '\n  ${fmt.dim('also: remote · flag · pluck · peek · build · update')}\n'36	text += '        ${fmt.dim('init · doctor · strip · migrate')}\n'37	text += '\n  ${fmt.dim('koh man <command>  for options and examples')}\n'38	return text39}4041pub fn run(argv []string) int {42	if argv.len < 2 {43		eprintln(usage_text())44		return 245	}46	hand_over(argv)47	rest := argv[2..]48	match argv[1] {49		'--version', '-v' {50			println('koh ${version}')51			return 052		}53		'--help', '-h' {54			println(usage_text())55			return 056		}57		'init' {58			return cmd_init(rest)59		}60		'save' {61			return cmd_save(rest, false)62		}63		'checkpoint' {64			return cmd_save(rest, true)65		}66		'status' {67			return cmd_status(rest)68		}69		'view' {70			return cmd_view(rest)71		}72		'diff' {73			return cmd_diff(rest)74		}75		'undo' {76			return cmd_undo(rest)77		}78		'revert' {79			return cmd_revert(rest)80		}81		'load' {82			return cmd_load(rest)83		}84		'flag' {85			return cmd_flag(rest)86		}87		'pluck' {88			return cmd_pluck(rest)89		}90		'peek' {91			return cmd_peek(rest)92		}93		'doctor' {94			return cmd_doctor(rest)95		}96		'strip' {97			return cmd_strip(rest)98		}99		'remote' {100			return cmd_remote(rest)101		}102		'offer' {103			return cmd_offer(rest)104		}105		'steal' {106			return cmd_steal(rest)107		}108		'build' {109			return cmd_build(rest)110		}111		'update' {112			return cmd_update(rest)113		}114		'migrate' {115			return cmd_migrate(rest)116		}117		'man', 'help' {118			return cmd_man(rest)119		}120		else {121			return usage_error('unknown command "${argv[1]}" — run "koh --help" for the command list')122		}123	}124}