Skip to content

Commit

Permalink
Drop minimist in favor of util.parseArgs
Browse files Browse the repository at this point in the history
Also refactor the cli utility.

**CLI Changes**

- The double dash between args and positionals is no longer required,
  though still recommended.
- Now we handle every positional as input as opposed to only the
  first. This means you can omit quoting the input However repeated
  whitespace is not perserved this way, so quoting is still
  recommended.

**Breaking Changes**

- CLI will now fail with a message on unrecognized option.
  • Loading branch information
runarberg committed Nov 8, 2023
1 parent 95c7001 commit a140d38
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 37 deletions.
123 changes: 92 additions & 31 deletions bin/mathup.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,115 @@
#!/usr/bin/env node

import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";

import minimist from "minimist";
import { createReadStream } from "node:fs";
import path from "node:path";
import { pipeline } from "node:stream/promises";
import { fileURLToPath } from "node:url";
import { parseArgs } from "node:util";

import mathup from "../src/index.js";

const argv = minimist(process.argv.slice(2));
const argOptions = {
bare: {
type: "boolean",
short: "b",
default: false,
},

display: {
type: "boolean",
short: "d",
default: false,
},

help: {
type: "boolean",
short: "h",
default: false,
},

rtl: {
type: "boolean",
default: false,
},

decimalmark: {
type: "string",
short: "m",
},

colsep: {
type: "string",
short: "c",
},

rowsep: {
type: "string",
short: "r",
},
};

const DIRNAME = path.dirname(fileURLToPath(import.meta.url));

/**
* @param {import("node:stream").Writable} stream
* @returns {Promise<void>}
*/
async function printHelp(stream) {
await pipeline(createReadStream(path.join(DIRNAME, "usage.txt")), stream);
}

/**
* @returns {void}
*/
function main() {
let input;

if (argv.h || argv.help) {
fs.createReadStream(path.join(DIRNAME, "usage.txt"))
.pipe(process.stdout)
.on("close", () => {
process.exit(1);
});
async function main() {
let argv;
try {
argv = parseArgs({
options: argOptions,
strict: true,
allowPositionals: true,
});
} catch (error) {
process.stderr.write(error.message);
process.stderr.write("\n");

if (error.code === "ERR_PARSE_ARGS_UNKNOWN_OPTION") {
await printHelp(process.stderr);
}

process.exit(1);
}

if (argv.values.help) {
await printHelp(process.stdin);

return;
}

/** @type {import("../src/index.js").Options} */
const options = {
bare: argv.b || argv.bare,
display: argv.d || argv.display ? "block" : undefined,
dir: argv.rtl ? "rtl" : undefined,
decimalMark: argv.m || argv.decimalmark,
colSep: argv.c || argv.colsep,
rowSep: argv.r || argv.rowsep,
bare: argv.values.bare,
display: argv.values.display ? "block" : undefined,
dir: argv.values.rtl ? "rtl" : undefined,
decimalMark: argv.values.decimalmark,
colSep: argv.values.colsep,
rowSep: argv.values.rowsep,
};

if (typeof argv._[0] === "string") {
[input] = argv._;
if (argv.positionals.length > 0) {
const input = argv.positionals.join(" ");

process.stdout.write(mathup(String(input), options).toString());
process.stdout.write("\n");

return;
}

const input = (await process.stdin.toArray()).join("").trim();

if (input.length > 0) {
process.stdout.write(mathup(String(input), options).toString());
process.stdout.write("\n");
} else {
process.stdin.on("readable", () => {
input = process.stdin.read();
if (input !== null) {
process.stdout.write(mathup(String(input), options).toString());
process.stdout.write("\n");
}
});
}
}

Expand Down
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,5 @@
"prettier": "3.0.3",
"rollup": "^4.3.0",
"typescript": "^5.2.2"
},
"dependencies": {
"minimist": "^1.2.8"
}
}

0 comments on commit a140d38

Please sign in to comment.