Skip to content

Commit

Permalink
Use util.parseArgs instead of minimist (#281)
Browse files Browse the repository at this point in the history
* Use util.parseArgs instead of minimist

* Refresh
  • Loading branch information
ralfhandl authored Feb 13, 2024
1 parent 105ab43 commit a4af79e
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 135 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"lightslategray",
"odata",
"openapi",
"positionals",
"qname",
"whitesmoke"
],
Expand Down
7 changes: 3 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
const csdl = require("odata-csdl");
const lib = require("./csdl2openapi");
const fs = require("fs");
const { parseArgs } = require("./cliParts");
const { parseArguments } = require("./cliParts");
const { stringifyStream } = require("@discoveryjs/json-ext");

const args = parseArgs(process.argv.slice(2));
if (args.unknown)
args.unknown.map((arg) => console.error(`Unknown option: ${arg}`));
const args = parseArguments(process.argv.slice(2));
if (args.unknown) console.error(args.unknown);
if (args.usage) console.log(args.usage);
else convert(args);

Expand Down
79 changes: 32 additions & 47 deletions lib/cliParts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* Latest version: https://github.com/oasis-tcs/odata-openapi/blob/main/lib/cliOptions.js
*/

const minimist = require("minimist");
const util = require("node:util");

module.exports = { parseArgs };
module.exports = { parseArguments };

function parseArgs(argv) {
function parseArguments(argv) {
const usage = `Usage: odata-openapi3 <options> <source file>
Options:
--basePath base path (default: /service-root)
Expand All @@ -24,65 +24,50 @@ function parseArgs(argv) {
--skipBatchPath skips the generation of the $batch path, (default: false)
-t, --target target file (default: source file basename + .openapi3.json)
--title default title if none is annotated`;
const unknown = [];

const args = minimist(argv, {
string: [
"basePath",
"description",
"host",
"keep",
"levels",
"openapi-version",
"scheme",
"target",
"title",
],
boolean: ["diagram", "help", "pretty", "skipBatchPath"],
alias: {
d: "diagram",
h: "help",
k: "keep",
o: "openapi-version",
p: "pretty",
t: "target",
},
default: {
pretty: false,
},
unknown: (arg) => {
if (arg.substring(0, 1) == "-") {
unknown.push(arg);
return false;
}
},
});
let parsed;
try {
parsed = util.parseArgs({
args: argv,
options: {
basePath: { type: "string" },
description: { type: "string" },
diagram: { type: "boolean", short: "d" },
help: { type: "boolean", short: "h" },
host: { type: "string" },
keep: { type: "string", short: "k", multiple: true },
levels: { type: "string" },
"openapi-version": { type: "string", short: "o" },
pretty: { type: "boolean", short: "p" },
scheme: { type: "string" },
skipBatchPath: { type: "boolean" },
target: { type: "string", short: "t" },
title: { type: "string" },
},
allowPositionals: true,
});
} catch (e) {
return { unknown: e.message, usage };
}

if (unknown.length > 0 || args.help || args._.length !== 1)
return {
unknown,
usage,
};
if (parsed.values.help || parsed.positionals.length !== 1) return { usage };

const source = args._[0];
const source = parsed.positionals[0];
const target =
args.target ||
parsed.values.target ||
(source.lastIndexOf(".") > 0
? source.substring(0, source.lastIndexOf("."))
: source) + ".openapi3.json";

const options = {};

for (const [name, value] of Object.entries(args)) {
if (name.length === 1) continue;
if (value === false) continue;
for (const [name, value] of Object.entries(parsed.values)) {
switch (name) {
case "description":
options.defaultDescription = value;
break;
case "keep":
if (Array.isArray(value)) options.rootResourcesToKeep = value;
else options.rootResourcesToKeep = [value];
options.rootResourcesToKeep = value;
break;
case "levels": {
const l = Number(value);
Expand Down
137 changes: 85 additions & 52 deletions package-lock.json

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

Loading

0 comments on commit a4af79e

Please sign in to comment.