Skip to content

Commit

Permalink
util: fix arg parsing for gen_data (quisquous#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
wexxlee authored Jan 5, 2024
1 parent 2c7b245 commit cafcf25
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/PatchUpdateChecklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ check the `#important` channel in the [XIVAPI Discord server](https://discord.gg

Cactbot uses a number of resources files that are sourced from game data.
Once XIVAPI is updated, you can refresh those files data
by running `npm run util generate` from your cacbot root repo directory.
by running `npm run generate` from your cacbot root repo directory.
From the interactive menu, you can choose to run one at a time,
or you can run all of them by selecting `* Generate All Data Files`.
The scripts that will be run (and the resources files they genereate) are:
Expand Down
3 changes: 3 additions & 0 deletions util/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ in the various project [docs](https://github.com/OverlayPlugin/cactbot/tree/main

You can run the most common scripts by running `npm run util`
from inside your cactbot root directory.
If you want to pass CLI arguments to the scripts,
use '--' so the args are treated as script args and not node args.
(e.g. `npm run generate -- -f all -ll debug`.
2 changes: 1 addition & 1 deletion util/console_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ConsoleLogger {
if (logLevel === undefined)
return;
if (!Object.keys(this.logLevelMap).includes(logLevel))
this.fatalError('Invalid log level is set.');
this.fatalError(`Invalid log level (${logLevel}) is set.`);
this.myLogLevel = this.logLevelMap[logLevel];
this.debug(`Log level set: ${logLevel}`);
}
Expand Down
24 changes: 14 additions & 10 deletions util/generate_data_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ type GenerateDataFilesInquirerType = {

const fileDefault: FileKeyAll = 'all';

// TODO: argparse isn't handling CLI options past the initial 'action'
// need to look into this more
const generateDataFilesFunc = async (args: Namespace): Promise<void> => {
if (!(args instanceof GenerateDataFilesNamespace))
throw new UnreachableCode();
Expand All @@ -88,22 +86,28 @@ const generateDataFilesFunc = async (args: Namespace): Promise<void> => {
message: 'Which data file do you want to generate?',
choices: fileChoices,
default: args.file,
when: () => typeof args.file !== 'string',
when: () => args.file === null || args.file === undefined,
},
{
type: 'list',
name: 'loglevel',
message: 'What level of console logging do you want?',
choices: logLevelChoices,
default: args.loglevel,
when: () => typeof args.loglevel !== 'string',
when: () => args.loglevel === null || args.loglevel === undefined,
},
] as const;
return inquirer.prompt<GenerateDataFilesInquirerType>(questions)
.then((answers) => {
const myChoice: FileKeyAll = answers.file ?? args.file ?? fileDefault;
const myLogLevel = answers.loglevel ?? args.loglevel ?? ConsoleLogger.logLevelDefault;
return fileKeyToFuncAll[myChoice](myLogLevel);
// args.file and args.loglevel return as objects, rather than string primitives. /shrug
// when myLogLevel is passed to the gen_data function, it cannot recognize the log level,
// so force it here to a string and then re-apply the type with an assertion.
// parsearg already limits user-input values to the respective type literal.
const myFile: FileKeyAll = answers.file ?? args.file ?? fileDefault;
const myLogLevel: LogLevelKey = answers.loglevel ??
args.loglevel ??
ConsoleLogger.logLevelDefault;
return fileKeyToFuncAll[myFile](myLogLevel.toString() as LogLevelKey);
}).catch(console.error);
};

Expand All @@ -120,14 +124,14 @@ export const registerGenerateDataFiles = (
description: actionChoices.generate.name,
});

generateParser.addArgument('--file', {
generateParser.addArgument(['-f', '--file'], {
nargs: 1,
type: 'string',
choices: Object.keys(fileKeyToFuncAll),
help: 'The name of the file to be generated (incl. \'all\')',
help: 'The data file to be generated (or \'all\')',
});

generateParser.addArgument('--loglevel', {
generateParser.addArgument(['-ll', '--loglevel'], {
nargs: 1,
type: 'string',
choices: logLevels.map((ll) => ll[0]),
Expand Down

0 comments on commit cafcf25

Please sign in to comment.