Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace commander with parseArgs #67

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"test": "mocha"
},
"dependencies": {
"commander": "^9.4.0"
},
"devDependencies": {
"chai": "^4.3.6",
Expand Down
73 changes: 73 additions & 0 deletions src/argsParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"use strict";
const { parseArgs } = require("util");

const mainOptions = {
help: { type: 'string', short: "h" },
"macho-segment-name": { type: "string" },
"output-api-header": { type: "boolean" },
overwrite: { type: "boolean" },
};

function helper(command) {
const helpOptionsDescriptions = {
help: "display help for command",
"macho-segment-name": "--macho-segment-name <segment_name> Name for the Mach-O segment (default: \"__POSTJECT\")",
"output-api-header": "--output-api-header Output the API header to stdout",
"overwrite": "--overwrite Overwrite the resource if it already exists"
};
if (command) {
console.log(helpOptionsDescriptions[command]);
}
else {
console.log("Usage: postject [options] <filename> <resource_name> <resource>");
console.log("Inject arbitrary read-only resources into an executable for use at runtime");
console.log("");
console.log("Arguments:");
console.log(" filename The executable to inject the resource into");
console.log(" resource_name The resource name to use (section name on Mach-O and ELF, resource name for PE)");
console.log(" resource The resource file to inject");
console.log("Options:");
for (const [key, value] of Object.entries(helpOptionsDescriptions)) {
console.log(` ${value}`);
}
}
}

function argumentsParser(clb) {

const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
const { values } = parseArgs({ args, options: mainOptions, strict: false, tokens: true });
if (typeof values.help === 'string') {
helper(values.help);
}
else {
helper();
}
process.exit();
}
if (args.length < 3) {
console.log("Missing adequate arguments");
helper();
process.exit(1);
}
const positionalArgs = args.slice(args.length - 3);
const subCommandArgs = args.slice(0, args.length - 3);

const optionValuesObj = parseArgs({
args: subCommandArgs,
options: mainOptions,
strict: false,
tokens: true,
}).values;
const options = Object.keys(optionValuesObj).reduce((acc, key) => {
const camelCaseKey = key.replace(/-([a-z])/g, (g) =>
g[1].toUpperCase()
);
acc[camelCaseKey] = optionValuesObj[key];
return acc;
}, {});
clb(...positionalArgs, options);
}

module.exports = { argumentsParser };
23 changes: 2 additions & 21 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node

const program = require("commander");
const { constants, promises: fs } = require("fs");
const path = require("path");
const { inject } = require("./api.js");
const { argumentParser } = require("./argsParser.js");

async function main(filename, resourceName, resource, options) {
if (options.outputApiHeader) {
Expand Down Expand Up @@ -36,24 +36,5 @@ async function main(filename, resourceName, resource, options) {
}

if (require.main === module) {
program
.name("postject")
.description(
"Inject arbitrary read-only resources into an executable for use at runtime"
)
.argument("<filename>", "The executable to inject into")
.argument(
"<resource_name>",
"The resource name to use (section name on Mach-O and ELF, resource name for PE)"
)
.argument("<resource>", "The resource to inject")
.option(
"--macho-segment-name <segment_name>",
"Name for the Mach-O segment",
"__POSTJECT"
)
.option("--output-api-header", "Output the API header to stdout")
.option("--overwrite", "Overwrite the resource if it already exists")
.action(main)
.parse(process.argv);
argumentParser(main);
}