From c166c473525e7f16e2c2f2914e22cb58bfbdb5fe Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Mon, 29 Jul 2024 18:17:17 +0200 Subject: [PATCH] fix: Add help messages to the cli and clarify some options (#27) --- README.md | 8 +++---- src/clientApp.ts | 58 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c49f690..e23314e 100644 --- a/README.md +++ b/README.md @@ -42,12 +42,12 @@ Steam OpenId support is required on your local server. Enable it with [screepsmo All of the command line arguments are optional. -- `--package` — Used to set the path to the Screeps package.nw file. Use this if the path isn't automatically detected. +- `--package` — Path to the Screeps package.nw file. Use this if the path isn't automatically detected. - `--host` — Changes the host address. (default: localhost) - `--port` — Changes the port. (default: 8080) -- `--backend` — Used to configure a backend url. If provided, the client app proxies this endpoint and the server list page is disabled. -- `--internal_backend` — Used to configure an internal backend url. If provided, the client app uses this address to reference the internal server endpoint. -- `--server_list` — Used to set the path to a custom server list json config file. +- `--backend` — Set the backend url. When provided, the app will directly proxy this server and disable the server list page. +- `--internal_backend` — Set the backend's internal url. Requires --backend to be set. When provided, the app will use this url to connect to the server while still using its --backend name externally. +- `--server_list` — Path to a custom server list json config file. - `--beautify` — Formats .js files loaded in the client for debugging. - `--debug` — Display verbose errors for development. - `-v` , `--version` — Display the version number. diff --git a/src/clientApp.ts b/src/clientApp.ts index 987827a..1956dd1 100644 --- a/src/clientApp.ts +++ b/src/clientApp.ts @@ -38,18 +38,55 @@ const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8')); const version = packageJson.version || '1.0.0'; const arrow = '\u2192'; +const DEFAULT_HOST = 'localhost'; +const DEFAULT_PORT = 8080; + // Parse program arguments const argv = (() => { - const parser = new ArgumentParser(); + const parser = new ArgumentParser({ description: 'Web proxy for the Screeps World game client.' }); parser.add_argument('-v', '--version', { action: 'version', version: `v${version}` }); - parser.add_argument('--package', { nargs: '?', type: 'str' }); - parser.add_argument('--host', { nargs: '?', type: 'str' }); - parser.add_argument('--port', { nargs: '?', type: 'int' }); - parser.add_argument('--backend', { nargs: '?', type: 'str' }); - parser.add_argument('--internal_backend', { nargs: '?', type: 'str' }); - parser.add_argument('--server_list', { nargs: '?', type: 'str' }); - parser.add_argument('--beautify', { action: 'store_true', default: false }); - parser.add_argument('--debug', { action: 'store_true', default: false }); + parser.add_argument('--package', { + nargs: '?', + type: 'str', + help: "Path to the Screeps package.nw file. Use this if the path isn't automatically detected.", + }); + parser.add_argument('--host', { + nargs: '?', + type: 'str', + default: DEFAULT_HOST, + help: `Changes the host address. (default: ${DEFAULT_HOST})`, + }); + parser.add_argument('--port', { + nargs: '?', + type: 'int', + default: DEFAULT_PORT, + help: `Changes the port. (default: ${DEFAULT_PORT})`, + }); + parser.add_argument('--backend', { + nargs: '?', + type: 'str', + help: 'Set the backend url. When provided, the app will directly proxy this server and disable the server list page.', + }); + parser.add_argument('--internal_backend', { + nargs: '?', + type: 'str', + help: "Set the backend's internal url. Requires --backend to be set. When provided, the app will use this url to connect to the server while still using its --backend name externally.", + }); + parser.add_argument('--server_list', { + nargs: '?', + type: 'str', + help: 'Path to a custom server list json config file.', + }); + parser.add_argument('--beautify', { + action: 'store_true', + default: false, + help: 'Formats .js files loaded in the client for debugging.', + }); + parser.add_argument('--debug', { + action: 'store_true', + default: false, + help: 'Display verbose errors for development.', + }); return parser.parse_args(); })(); @@ -88,8 +125,7 @@ const lastModified = stat.mtime; // Set up web server const koa = new Koa(); -const port = argv.port ?? 8080; -const host = argv.host ?? 'localhost'; +const { host, port } = argv; const server = koa.listen(port, host); server.on('error', (err) => handleServerError(err, argv.debug)); server.on('listening', () => console.log('🌐', chalk.dim('Ready', arrow), chalk.white(`http://${host}:${port}/`)));