Enhance CLI command mode for ritmex-bot (#23)

- Introduced a new command mode for `ritmex-bot`, allowing agent-friendly structured trading operations without entering the Ink interactive menu.
- Updated `package.json` to include versioning and set the project as public with a new CLI entry point.
- Enhanced documentation in both English and Chinese to provide comprehensive usage instructions for the new command mode.
- Added a new executable script for `ritmex-bot` to facilitate command execution.
- Improved error handling and command parsing for better user experience and clarity in command execution.
This commit is contained in:
Disney
2026-02-27 12:42:20 +08:00
committed by GitHub
parent d6399b92aa
commit 4014a86223
22 changed files with 3319 additions and 53 deletions
+56 -21
View File
@@ -5,29 +5,64 @@ import { setupGlobalErrorHandlers } from "./runtime-errors";
import { parseCliArgs, printCliHelp } from "./cli/args";
import { startStrategy } from "./cli/strategy-runner";
import { resolveExchangeId } from "./exchanges/create-adapter";
import { CommandParseError, parseCommandArgv, printCommandHelp } from "./cli/command-parser";
import { executeCliCommand, renderCommandPayload } from "./cli/command-executor";
setupGlobalErrorHandlers();
const options = parseCliArgs();
// If user specifies --exchange, override environment-based resolution for this process
if (options.exchange) {
// Ensure downstream calls to resolveExchangeId() pick the CLI value.
// We set both common env keys respected by resolveExchangeId.
process.env.EXCHANGE = options.exchange;
process.env.TRADE_EXCHANGE = options.exchange;
}
void run();
if (options.help) {
printCliHelp();
process.exit(0);
}
async function run(): Promise<void> {
const rawArgv = process.argv.slice(2);
if (options.strategy) {
startStrategy(options.strategy, { silent: options.silent })
.catch((error) => {
const message = error instanceof Error ? error.message : String(error);
console.error(`[Strategy] Failed to start: ${message}`);
process.exit(1);
});
} else {
render(<App />);
try {
const parsedCommand = parseCommandArgv(rawArgv);
if (parsedCommand) {
if (parsedCommand.kind === "help") {
printCommandHelp(parsedCommand.topic);
process.exit(0);
}
const result = await executeCliCommand(parsedCommand);
const output = renderCommandPayload(result.payload, parsedCommand.json);
if (result.payload.success) {
console.log(output);
} else {
console.error(output);
}
if (result.forceExit) {
process.exit(result.exitCode);
}
return;
}
} catch (error) {
if (error instanceof CommandParseError) {
console.error(`[CLI] ${error.message}`);
process.exit(2);
}
throw error;
}
const options = parseCliArgs(rawArgv);
// If user specifies --exchange, override environment-based resolution for this process
if (options.exchange) {
// Ensure downstream calls to resolveExchangeId() pick the CLI value.
// We set both common env keys respected by resolveExchangeId.
process.env.EXCHANGE = options.exchange;
process.env.TRADE_EXCHANGE = options.exchange;
}
if (options.help) {
printCliHelp();
process.exit(0);
}
if (options.strategy) {
startStrategy(options.strategy, { silent: options.silent })
.catch((error) => {
const message = error instanceof Error ? error.message : String(error);
console.error(`[Strategy] Failed to start: ${message}`);
process.exit(1);
});
} else {
render(<App />);
}
}