diff --git a/scripts/rimpl.ts b/scripts/rimpl.ts index d40676352..eadf066be 100644 --- a/scripts/rimpl.ts +++ b/scripts/rimpl.ts @@ -13,29 +13,33 @@ import {openYamlFileAsObject, saveYamlFileAs} from '../src/util/yaml'; */ const rimplScript = async () => { try { - const {inputPath, outputPath} = parseProcessArgument(); - const impl = await openYamlFileAsObject(inputPath); + const processParams = parseProcessArgument(); - if (!('graph' in impl)) { - throw new Error('No graph data found.'); - } + if (processParams) { + const {inputPath, outputPath} = processParams; + const impl = await openYamlFileAsObject(inputPath); - // Lifecycle Initialize Models - const modelsHandbook = new ModelsUniverse(); - impl.initialize.models.forEach((model: any) => - modelsHandbook.writeDown(model) - ); + if (!('graph' in impl)) { + throw new Error('No graph data found.'); + } - // Lifecycle Computing - const ateruiComputer = new Supercomputer(impl, modelsHandbook); - const ompl = await ateruiComputer.compute(); + // Lifecycle Initialize Models + const modelsHandbook = new ModelsUniverse(); + impl.initialize.models.forEach((model: any) => + modelsHandbook.writeDown(model) + ); - if (!outputPath) { - console.log(JSON.stringify(ompl)); - return; - } + // Lifecycle Computing + const ateruiComputer = new Supercomputer(impl, modelsHandbook); + const ompl = await ateruiComputer.compute(); - await saveYamlFileAs(ompl, outputPath); + if (!outputPath) { + console.log(JSON.stringify(ompl)); + return; + } + + await saveYamlFileAs(ompl, outputPath); + } } catch (error) { console.error(error); } diff --git a/src/config/config.ts b/src/config/config.ts index 5206ea59a..840ffc755 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -1,3 +1,7 @@ +import {ArgumentConfig} from 'ts-command-line-args'; + +import {RimplProcessArgs} from '../types/process-args'; + export const CONFIG = { MODEL_IDS: { BOAVIZTA_CPU: 'org.boavizta.cpu.sci', @@ -19,13 +23,51 @@ export const CONFIG = { TEADS_CURVE: 'teads.curve', WATT_TIME: 'org.wattime.grid', }, - VALIDATION: { - IMPL_CLI: { - impl: String, + RIMPL: { + ARGS: { + impl: { + type: String, + optional: true, + alias: 'i', + description: 'Path to an input IMPL file.', + }, ompl: { type: String, optional: true, + description: + 'Path to the output IMPL file where the results as saved, if none is provided it prints to stdout.', + }, + format: { + type: String, + optional: true, + description: + 'The output file format. default to yaml but if csv is specified then it formats the impacts as a csv file for loading into another program.', + defaultValue: 'yaml', + }, + verbose: { + type: Boolean, + optional: true, + description: + 'How much information to output about the calculation to aid investigation and debugging.', + }, + help: { + type: Boolean, + optional: true, + alias: 'h', + description: 'Prints this usage guide.', }, - }, + } as ArgumentConfig, + HELP: `rimpl + -impl [path to the input impl file] + -ompl [path to the output impl file] + -format [yaml|csv] + -verbose + -help + impl: path to an input IMPL file + ompl: path to the output IMPL file where the results as saved, if none is provided it prints to stdout. + format: the output file format. default to yaml but if csv is specified then it formats the impacts as a csv file for loading into another program. + verbose: how much information to output about the calculation to aid investigation and debugging. + help: prints out the above help instruction. + `, }, }; diff --git a/src/types/process-args.ts b/src/types/process-args.ts index 67ccc70b8..ed40b8633 100644 --- a/src/types/process-args.ts +++ b/src/types/process-args.ts @@ -1,4 +1,7 @@ -export interface ImplProcessArgs { - impl: string; - ompl: string; +export interface RimplProcessArgs { + impl?: string; + ompl?: string; + format?: string; + verbose?: boolean; + help?: boolean; } diff --git a/src/util/args.ts b/src/util/args.ts index 6272b76e4..de19e774e 100644 --- a/src/util/args.ts +++ b/src/util/args.ts @@ -5,10 +5,10 @@ import {checkIfFileIsYaml} from './yaml'; import {CONFIG, STRINGS} from '../config'; -import {ImplProcessArgs} from '../types/process-args'; +import {RimplProcessArgs} from '../types/process-args'; -const {VALIDATION} = CONFIG; -const {IMPL_CLI} = VALIDATION; +const {RIMPL} = CONFIG; +const {ARGS, HELP} = RIMPL; const {WRONG_CLI_ARGUMENT} = STRINGS; @@ -16,7 +16,7 @@ const {WRONG_CLI_ARGUMENT} = STRINGS; * Validates process arguments * @private */ -const validateAndParseProcessArgs = () => parse(IMPL_CLI); +const validateAndParseProcessArgs = () => parse(ARGS); /** * Prepends process path to fiven `filePath`. @@ -33,13 +33,20 @@ const prependFullFilePath = (filePath: string) => { * Otherwise throws error. */ export const parseProcessArgument = () => { - const {impl, ompl} = validateAndParseProcessArgs(); + const {impl, ompl, help} = validateAndParseProcessArgs(); - if (checkIfFileIsYaml(impl)) { - return { - inputPath: prependFullFilePath(impl), - ...(ompl && {outputPath: prependFullFilePath(ompl)}), - }; + if (help) { + console.log(HELP); + return; + } + + if (impl) { + if (checkIfFileIsYaml(impl)) { + return { + inputPath: prependFullFilePath(impl), + ...(ompl && {outputPath: prependFullFilePath(ompl)}), + }; + } } throw Error(WRONG_CLI_ARGUMENT);