cli.v

2.9 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	text += '\n  ${fmt.dim('Asha Software · asha.software')}\n'39	return text40}4142pub fn run(argv []string) int {43	if argv.len < 2 {44		eprintln(usage_text())45		return 246	}47	hand_over(argv)48	rest := argv[2..]49	match argv[1] {50		'--version', '-v' {51			println('koh ${version}')52			return 053		}54		'--help', '-h' {55			println(usage_text())56			return 057		}58		'init' {59			return cmd_init(rest)60		}61		'save' {62			return cmd_save(rest, false)63		}64		'checkpoint' {65			return cmd_save(rest, true)66		}67		'status' {68			return cmd_status(rest)69		}70		'view' {71			return cmd_view(rest)72		}73		'diff' {74			return cmd_diff(rest)75		}76		'undo' {77			return cmd_undo(rest)78		}79		'revert' {80			return cmd_revert(rest)81		}82		'load' {83			return cmd_load(rest)84		}85		'flag' {86			return cmd_flag(rest)87		}88		'pluck' {89			return cmd_pluck(rest)90		}91		'peek' {92			return cmd_peek(rest)93		}94		'doctor' {95			return cmd_doctor(rest)96		}97		'strip' {98			return cmd_strip(rest)99		}100		'remote' {101			return cmd_remote(rest)102		}103		'offer' {104			return cmd_offer(rest)105		}106		'steal' {107			return cmd_steal(rest)108		}109		'build' {110			return cmd_build(rest)111		}112		'update' {113			return cmd_update(rest)114		}115		'migrate' {116			return cmd_migrate(rest)117		}118		'man', 'help' {119			return cmd_man(rest)120		}121		else {122			return usage_error('unknown command "${argv[1]}" — run "koh --help" for the command list')123		}124	}125}