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

Replace argparse with minimist #327

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 7 additions & 9 deletions bin/remarkable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


var fs = require('fs');
var argparse = require('argparse');
var argparse = require('../lib/arg_parser');

var Remarkable = require('..');

Expand All @@ -14,14 +14,12 @@ var Remarkable = require('..');
var cli = new argparse.ArgumentParser({
prog: 'remarkable',
version: require('../package.json').version,
addHelp: true
});

cli.addArgument([ 'file' ], {
help: 'File to read',
nargs: '?',
defaultValue: '-'
});
usage: '[file]'
},
[
{name: 'file', optional: true, default: '-', help: 'File to read'}
]
);

var options = cli.parseArgs();

Expand Down
111 changes: 111 additions & 0 deletions lib/arg_parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"use strict";
var argparser = require("minimist");

/**
* Alternative argument parser based on minimist.
*/
function nameForArg(arg) {
var res =
(arg.optional ? "{" : "") +
(arg.values ? arg.values.join(",") : arg.name) +
(arg.optional ? "}" : "");
return res;
}

function helpForArg(arg, spaces) {
var name = nameForArg(arg).padEnd(spaces);
return name + " " + arg.help
}

function ArgumentParser(info, args) {
var commonArgs = [
{name: '-h, --help', help: 'Show this help message and exit.'},
{name: '-v, --version', help: "Show program's version number and exit."}
];
var spaces = args.concat(commonArgs).reduce((accum, arg) => Math.max(nameForArg(arg).length, accum),0);
var helpString =
"Positional arguments:\n" +
args.map(arg => " " + helpForArg(arg, spaces)).join(" \n") + "\n\n" +
"Optional arguments:\n" +
commonArgs.map(arg => " " + helpForArg(arg, spaces)).join(" \n") + "\n";

function message(message, stream) {
var out = stream || process.stdout;
out.write("" + message);
}

function showHelp(includeArgs = false) {
var str = "usage: " + info.prog + " [-h] [-v] " + info.usage + "\n";

if (includeArgs) {
str += "\n" + helpString;
}
message(str);
}

function showVersion() {
message(info.version + "\n");
}

function parseError(msg, errNo = info.errNo) {
showHelp();
message(info.prog + ": error: " + msg + "\n");
process.exit(errNo);
}

this.parseArgs = function() {
var options = args.reduce((accum, arg) => {
accum[arg.name] = arg.default | null; return accum
},{});

var opts = argparser(process.argv.slice(2), {
boolean: ["help", "version"],
alias: { v: "version", h: "help" },
stopEarly: function() {
showHelp();
process.exit(1);
}
});
if (opts.help) {
showHelp(true);
process.exit(0);
}
if (opts.version) {
showVersion();
process.exit(0);
}

var len = opts._.length;
var copyLen = 0;
// If we've got fewer args than total, kill optional args from the back
// until we've got the amount we're expecting.
var argsCopy = args.reverse().filter(arg => {
var res = !arg.optional || copyLen < len;
copyLen += res ? 1 : 0;
return res;
}).reverse();

if (len < argsCopy.length) {
parseError("too few arguments");
}

argsCopy.forEach((v, i) => {
var optVal =opts._[i];
if (v.values && !v.values.includes(optVal)) {
parseError(
'argument "' + v.name + '": Invalid choice: ' +
optVal + " (choose from [" + v.values.join(", ") + "])"
);
}
options[v.name] = optVal;
});

if (len > argsCopy.length) {
parseError("Unrecognized arguments: " + opts._.slice(argsCopy.length).join(" ") + ".");
}

return options;
};
}

exports.ArgumentParser = ArgumentParser;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
"test": "make test"
},
"dependencies": {
"argparse": "~0.1.15",
"autolinker": "~0.28.0"
"autolinker": "~0.28.0",
"minimist": "^1.2.0"
},
"devDependencies": {
"ansi": "^0.3.0",
Expand Down
31 changes: 12 additions & 19 deletions support/specsplit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,23 @@

'use strict';


var fs = require('fs');
var util = require('util');
var argparse = require('argparse');
var argparse = require('../lib/arg_parser');

var Remarkable = require('..');


var cli = new argparse.ArgumentParser({
prog: 'specsplit',
version: require('../package.json').version,
addHelp: true
});

cli.addArgument([ 'type' ], {
help: 'type of examples to filter',
nargs: '?',
choices: [ 'good', 'bad' ]
});

cli.addArgument([ 'spec' ], {
help: 'spec file to read'
});
var cli = new argparse.ArgumentParser(
{
prog: 'specsplit',
version: require('../package.json').version,
usage: '[{good,bad}] spec'
},
[
{name: 'type', optional: true, values: ['good', 'bad'], help: 'type of examples to filter' },
{name: 'spec', 'help': 'spec file to read'}
]
);

var options = cli.parseArgs();

Expand Down Expand Up @@ -72,7 +66,6 @@ readFile(options.spec, 'utf8', function (error, input) {
input = input.replace(/→/g, '\t');

input.replace(/^\.\n([\s\S]*?)^\.\n([\s\S]*?)^\.$/gm, function(__, md, html, offset, orig) {

var result = {
md: md,
html: html,
Expand Down