Playing around with auto-completion (- broken)

yargs
gardenapple 4 years ago
parent cbcc1e9a68
commit 8c22aac58d
No known key found for this signature in database
GPG Key ID: CAF17E9ABE789268

@ -56,6 +56,12 @@ const Properties = {
textContent: "text-content" textContent: "text-content"
}; };
const LowConfidenceMode = {
noOp: "no-op",
force: "force",
exit: "exit"
};
const yargs = require("yargs"); const yargs = require("yargs");
//backwards compat with old, comma-separated values //backwards compat with old, comma-separated values
@ -63,7 +69,6 @@ function yargsCompatProperties(args) {
if (args["properties"]) { if (args["properties"]) {
for (var i = 0; i < args["properties"].length; i++) { for (var i = 0; i < args["properties"].length; i++) {
const property = args["properties"][i]; const property = args["properties"][i];
console.error(property);
if (property.indexOf(',') > -1) { if (property.indexOf(',') > -1) {
const split = args["properties"][i].split(','); const split = args["properties"][i].split(',');
args["properties"].splice(i, 1, ...split); args["properties"].splice(i, 1, ...split);
@ -99,17 +104,40 @@ function yargsFixPositional(args) {
} }
} }
let args = yargs let args = yargs
.version(false) .version(false)
.parserConfiguration({
"camel-case-expansion": false
})
.command("* [source]", "Process HTML input", (yargs) => { .command("* [source]", "Process HTML input", (yargs) => {
yargs.positional("source", { yargs.positional("source", {
desc: "A file, an http(s) URL, or '-' for standard input", desc: "A file, an http(s) URL, or '-' for standard input",
type: "string" type: "string"
}); });
}) })
.completion('completion', false, function(current, args) {
if (args["properties"] !== undefined) {
const properties = args["properties"];
let possibleProperties = [];
for (var possibleProperty of Object.values(Properties)) {
if (possibleProperty.startsWith(properties[properties.length - 1])
&& !properties.includes(possibleProperty))
possibleProperties.push(possibleProperty);
}
if (possibleProperties.length > 0)
return possibleProperties;
}
if (args["low-confidence"] !== undefined) {
const currentMode = args["low-confidence"];
let possibleModes = [];
for (var possibleMode of Object.values(LowConfidenceMode)) {
if (possibleMode.startsWith(currentMode)
&& possibleMode != currentMode)
possibleModes.push(possibleMode);
}
if (possibleModes.length > 0)
return possibleModes;
}
})
.middleware([ yargsCompatProperties, yargsFixPositional ], true) //middleware seems to be buggy .middleware([ yargsCompatProperties, yargsFixPositional ], true) //middleware seems to be buggy
.option('c', { .option('c', {
alias: "completion", alias: "completion",
@ -134,8 +162,8 @@ let args = yargs
alias: "low-confidence", alias: "low-confidence",
type: "string", type: "string",
desc: "What to do if Readability.js is uncertain about what the core content actually is", desc: "What to do if Readability.js is uncertain about what the core content actually is",
choices: ["no-op", "force", "exit"], //default: "no-op", //don't set default because completion won't work
default: "no-op" choices: ["no-op", "force", "exit"]
}) })
.option('p', { .option('p', {
alias: "properties", alias: "properties",
@ -147,7 +175,7 @@ let args = yargs
alias: "quiet", alias: "quiet",
type: "boolean", type: "boolean",
desc: "Don't output extra information to stderr", desc: "Don't output extra information to stderr",
default: false default: false
}) })
.option('u', { .option('u', {
alias: "url", alias: "url",
@ -181,9 +209,14 @@ Text-content and Html-content are mutually exclusive, and are always printed las
Default value is "html-title,html-content".`) Default value is "html-title,html-content".`)
.wrap(Math.min(yargs.terminalWidth(), 100)) .wrap(Math.min(yargs.terminalWidth(), 100))
.strict() .strict()
//.wrap(yargs.terminalWidth())
.parse(); .parse();
if (!args["low-confidence"]) {
args["low-confidence"] = LowConfidenceMode.noOp;
args['l'] = LowConfidenceMode.noOp;
}
function printUsage() { function printUsage() {
yargs.showHelp(); yargs.showHelp();
@ -239,14 +272,7 @@ let wantedProperties = [];
let justOutputHtml = false; let justOutputHtml = false;
if (args["properties"]) { if (args["properties"]) {
for (var property of args["properties"].split(',')) { wantedProperties = args["properties"];
if (Object.values(Properties).includes(property)) {
wantedProperties.push(property);
} else {
console.error(`Invalid property: ${property}`);
setErrored(ExitCodes.badUsageCLI);
}
}
} else { } else {
wantedProperties = [ Properties.htmlTitle, Properties.htmlContent ]; wantedProperties = [ Properties.htmlTitle, Properties.htmlContent ];
justOutputHtml = true; justOutputHtml = true;
@ -254,17 +280,6 @@ if (args["properties"]) {
const LowConfidenceMode = {
noOp: "no-op",
force: "force",
exit: "exit"
};
if (!Object.values(LowConfidenceMode).includes(args["low-confidence"])) {
console.error(`Invalid mode: ${args["low-confidence"]}`);
setErrored(ExitCodes.badUsageCLI);
}
if (errored) { if (errored) {
printUsage(); printUsage();

Loading…
Cancel
Save