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	groups := [18		[19			['save', 'snapshot your work'],20			['checkpoint', 'mark a place to come back to'],21			['status', 'what changed'],22			['log', 'history'],23			['diff', 'show changes'],24		],25		[26			['undo', 'step back'],27			['revert', 'throw away unsaved changes'],28			['load', 'go to any save'],29		],30		[31			['offer', 'send to your remote'],32			['steal', 'bring in from your remote'],33		],34	]35	mut text := '${fmt.light('koh ${version} — version control for the solo developer')}\n'36	for group in groups {37		text += '\n'38		for row in group {39			text += '  ${fmt.gold(row[0])}${' '.repeat(12 - row[0].len)}${fmt.muted(row[1])}\n'40		}41	}42	text += '\n  ${fmt.dim('also: remote · flag · pluck · peek · build · init · doctor · strip · migrate')}\n'43	text += '\n  ${fmt.dim('koh man <command>  for options and examples')}\n'44	return text45}4647pub fn run(argv []string) int {48	if argv.len < 2 {49		eprintln(usage_text())50		return 251	}52	hand_over(argv)53	rest := argv[2..]54	match argv[1] {55		'--version', '-v' {56			println('koh ${version}')57			return 058		}59		'--help', '-h' {60			println(usage_text())61			return 062		}63		'init' {64			return cmd_init(rest)65		}66		'save' {67			return cmd_save(rest, false)68		}69		'checkpoint' {70			return cmd_save(rest, true)71		}72		'status' {73			return cmd_status(rest)74		}75		'log' {76			return cmd_log(rest)77		}78		'saves' {79			return cmd_saves(rest)80		}81		'checkpoints' {82			return cmd_checkpoints(rest)83		}84		'diff' {85			return cmd_diff(rest)86		}87		'undo' {88			return cmd_undo(rest)89		}90		'revert' {91			return cmd_revert(rest)92		}93		'load' {94			return cmd_load(rest)95		}96		'flag' {97			return cmd_flag(rest)98		}99		'pluck' {100			return cmd_pluck(rest)101		}102		'peek' {103			return cmd_peek(rest)104		}105		'doctor' {106			return cmd_doctor(rest)107		}108		'strip' {109			return cmd_strip(rest)110		}111		'remote' {112			return cmd_remote(rest)113		}114		'offer' {115			return cmd_offer(rest)116		}117		'steal' {118			return cmd_steal(rest)119		}120		'build' {121			return cmd_build(rest)122		}123		'migrate' {124			return cmd_migrate(rest)125		}126		'man', 'help' {127			return cmd_man(rest)128		}129		else {130			return usage_error('unknown command "${argv[1]}" — run "koh --help" for the command list')131		}132	}133}