Use argparse for parsing command-line options
This commit is contained in:
parent
53e36cba0c
commit
0511343f09
|
|
@ -16,10 +16,12 @@
|
||||||
"format:js": "prettier --write src/"
|
"format:js": "prettier --write src/"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"argparse": "^2.0.1",
|
||||||
"ramda": "^0.27.1",
|
"ramda": "^0.27.1",
|
||||||
"xml": "^1.0.1"
|
"xml": "^1.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/argparse": "^2.0.0",
|
||||||
"@types/jest": "^26.0.14",
|
"@types/jest": "^26.0.14",
|
||||||
"@types/node": "^14.10.3",
|
"@types/node": "^14.10.3",
|
||||||
"@types/ramda": "types/npm-ramda#dist",
|
"@types/ramda": "types/npm-ramda#dist",
|
||||||
|
|
|
||||||
11
src/index.ts
11
src/index.ts
|
|
@ -2,16 +2,11 @@ import * as fs from "fs";
|
||||||
import { generateZwo } from "./generateZwo";
|
import { generateZwo } from "./generateZwo";
|
||||||
import { parse } from "./parser";
|
import { parse } from "./parser";
|
||||||
import { stats } from "./stats";
|
import { stats } from "./stats";
|
||||||
|
import { parseCliOptions } from "./parseCliOptions";
|
||||||
|
|
||||||
const opts = { stats: false, filename: "" };
|
const opts = parseCliOptions();
|
||||||
if (process.argv[2] === "--stats") {
|
|
||||||
opts.stats = true;
|
|
||||||
opts.filename = process.argv[3];
|
|
||||||
} else {
|
|
||||||
opts.filename = process.argv[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
const workout = parse(fs.readFileSync(opts.filename, "utf8"));
|
const workout = parse(fs.readFileSync(opts.file, "utf8"));
|
||||||
|
|
||||||
if (opts.stats) {
|
if (opts.stats) {
|
||||||
console.log(stats(workout));
|
console.log(stats(workout));
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
import { ArgumentParser } from "argparse";
|
||||||
|
|
||||||
|
export type CliOptions = {
|
||||||
|
file: string;
|
||||||
|
stats: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const parseCliOptions = (): CliOptions => {
|
||||||
|
const argParser = new ArgumentParser({
|
||||||
|
description: "Zwift workout generator",
|
||||||
|
add_help: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
argParser.add_argument("--stats", {
|
||||||
|
help: "output aggregate statistics instead of ZWO file",
|
||||||
|
action: "store_true",
|
||||||
|
default: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
argParser.add_argument("file", { nargs: 1 });
|
||||||
|
|
||||||
|
// As we only allow one file as input,
|
||||||
|
// convert filenames array to just a single string.
|
||||||
|
const { file, ...rest } = argParser.parse_args();
|
||||||
|
return {
|
||||||
|
file: file[0],
|
||||||
|
...rest,
|
||||||
|
};
|
||||||
|
};
|
||||||
10
yarn.lock
10
yarn.lock
|
|
@ -519,6 +519,11 @@
|
||||||
dependencies:
|
dependencies:
|
||||||
"@sinonjs/commons" "^1.7.0"
|
"@sinonjs/commons" "^1.7.0"
|
||||||
|
|
||||||
|
"@types/argparse@^2.0.0":
|
||||||
|
version "2.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/argparse/-/argparse-2.0.0.tgz#df1bb55f4c3bbbb3c744a6b35f78a401d3283a89"
|
||||||
|
integrity sha512-FFW98gJUgUWGq7ZMd0gJmNRoOPqAaq9oiNqfc/RM6+C5BZKHHuHTwvlU5hrOULIXEfZz3rhw9jakwYe6cqNv3Q==
|
||||||
|
|
||||||
"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.7":
|
"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.7":
|
||||||
version "7.1.9"
|
version "7.1.9"
|
||||||
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.9.tgz#77e59d438522a6fb898fa43dc3455c6e72f3963d"
|
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.9.tgz#77e59d438522a6fb898fa43dc3455c6e72f3963d"
|
||||||
|
|
@ -838,6 +843,11 @@ argparse@^1.0.7:
|
||||||
dependencies:
|
dependencies:
|
||||||
sprintf-js "~1.0.2"
|
sprintf-js "~1.0.2"
|
||||||
|
|
||||||
|
argparse@^2.0.1:
|
||||||
|
version "2.0.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
|
||||||
|
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==
|
||||||
|
|
||||||
arr-diff@^4.0.0:
|
arr-diff@^4.0.0:
|
||||||
version "4.0.0"
|
version "4.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-4.0.0.tgz#d6461074febfec71e7e15235761a329a5dc7c520"
|
resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-4.0.0.tgz#d6461074febfec71e7e15235761a329a5dc7c520"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue