Repository navigation
Expand file tree
/
Copy pathprogram.ts
More file actions
152 lines (133 loc) · 5.57 KB
/
Copy pathprogram.ts
File metadata and controls
152 lines (133 loc) · 5.57 KB
1
2
3
4
5
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
82
83
84
85
86
87
88
89
90
91
92
93
94
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Commander.js program definition — extracted from index.ts to avoid ESM TLA deadlock.
//
// This module is 100% synchronous (no top-level await), so it can be safely
// imported by both the entry point (index.ts) and the REPL dispatcher (executor.ts).
import { styleText } from "node:util";
import { Command, CommanderError } from "@commander-js/extra-typings";
import { init as initI18n } from "./i18n/index.ts";
import { setUsageErrorExit } from "./cli/exit.ts";
import { healthCommand } from "./commands/health.ts";
import { loginCommand, logoutCommand } from "./commands/auth.ts";
import { secretCommand } from "./commands/secret.ts";
import { translateCommand } from "./commands/translate.ts";
import { evaluateCommand } from "./commands/evaluate.ts";
import { configCommand } from "./commands/config/command.ts";
import { scoreCommand } from "./commands/score.ts";
import { annotateCommand } from "./commands/annotate.ts";
import { usageCommand } from "./commands/usage.ts";
import { contactCommand, feedbackCommand } from "./commands/links.ts";
import { tmCommand } from "./commands/tm/command.ts";
import { getVersion } from "./shared/version.ts";
import { terminal } from "./output/terminal.ts";
import { getSettings } from "./config/index.ts";
import { buildEpilogByCommanderPath } from "./repl/registry.ts";
const settings = getSettings();
initI18n(settings.cli.language);
export const program = new Command()
.name("prompsit")
.description("Translate, evaluate, and process text with the Prompsit API")
.helpCommand(false);
// Help styling: bold titles, cyan commands and options
program.configureHelp({
sortSubcommands: false,
sortOptions: false,
styleTitle: (str) => styleText("bold", str),
styleCommandText: (str) => styleText("cyan", str),
styleOptionText: (str) => styleText("cyan", str),
});
program.helpOption("-h, --help", "Show this help");
program.version(getVersion(), "-V, --version", "Show version number");
program.option("-v, --verbose", "Enable debug logging to stderr");
// --- Grouped commands ---
program.commandsGroup("Basics:");
program.addCommand(loginCommand);
program.addCommand(logoutCommand);
program.addCommand(secretCommand);
program.commandsGroup("Translation:");
program.addCommand(translateCommand);
program.commandsGroup("Translation Memory:");
program.addCommand(tmCommand);
program.commandsGroup("Evaluation:");
program.addCommand(evaluateCommand);
program.commandsGroup("Data:");
program.addCommand(scoreCommand);
program.addCommand(annotateCommand);
program.commandsGroup("Configuration:");
program.addCommand(configCommand);
program.commandsGroup("System:");
program.addCommand(healthCommand);
program.addCommand(usageCommand);
program.addCommand(contactCommand);
program.addCommand(feedbackCommand);
// Copy-paste examples in main help footer
program.addHelpText(
"after",
`
Examples:
$ prompsit login
$ prompsit translate "Hello world" -s en -t es
$ prompsit translate @report.docx -s en -t de
$ prompsit translate --formats
$ prompsit translate --formats --tsv
$ prompsit translate --languages -s en
$ prompsit translate --languages --tsv
$ prompsit eval -s "Hello" -h "Hola" -r "Hola"
$ prompsit eval @scores.csv -m bleu,chrf
$ prompsit eval --formats
$ prompsit eval --formats --tsv
$ prompsit score @corpus.tmx
$ prompsit score --languages --tsv
$ prompsit annotate @corpus.jsonl -l en --metadata "lid,docscorer"
$ prompsit annotate --metadata --tsv
$ prompsit usage
$ prompsit tm show
$ prompsit tm import memory.tmx
$ prompsit tm search "Hello world" -t es
$ prompsit # Interactive REPL
Run 'prompsit <command> --help' for detailed usage and examples.`
);
// exitOverride: catch Commander.js parse errors and map to exit code 2
program.exitOverride((err: CommanderError) => {
if (err.exitCode !== 0) {
setUsageErrorExit();
}
throw err;
});
// Redirect Commander output through terminal adapter used by CLI and REPL.
// Commander's writeOut includes trailing \n; console.log adds its own -> trim to avoid double newline.
program.configureOutput({
writeOut: (str) => {
terminal.line(str.endsWith("\n") ? str.slice(0, -1) : str);
},
writeErr: (str) => {
terminal.warn(str.trimEnd());
},
// Commander calls outputError(str, writeErr) for parse errors (excess args, missing options).
// Override to route through terminal.error (red) instead of writeErr (yellow warning).
outputError: (str) => {
terminal.error("COMMAND", str.trimEnd());
},
});
// addCommand() doesn't copy inherited settings (Commander.js by design).
// Only the fluent .command() API calls copyInheritedSettings() automatically.
// See: https://github.com/tj/commander.js#commands
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Commander generic variance
function propagateSettings(parent: Command<any, any, any>): void {
for (const cmd of parent.commands) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- Commander generic variance
cmd.copyInheritedSettings(parent);
propagateSettings(cmd);
}
}
propagateSettings(program);
// Attach CLI examples as epilogs to leaf commands (from REPL registry)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Commander generic variance
function attachEpilogs(parent: Command<any, any, any>, parentPath: string): void {
for (const cmd of parent.commands) {
const cmdPath = parentPath ? `${parentPath} ${cmd.name()}` : cmd.name();
const epilog = buildEpilogByCommanderPath(cmdPath);
if (epilog) cmd.addHelpText("after", epilog);
attachEpilogs(cmd, cmdPath);
}
}
attachEpilogs(program, "");