validating required, use required values, better errors
This commit is contained in:
parent
4ee97e16d0
commit
a3caa0150b
87
barg.js
87
barg.js
@ -51,12 +51,11 @@ function tokenize(input) {
|
||||
|
||||
function parse_input(spec, input) {
|
||||
const tokens = tokenize(input);
|
||||
const values = {};
|
||||
|
||||
const schema = {};
|
||||
const alias_map = {};
|
||||
|
||||
let position = 0;
|
||||
let position = [null, 0];
|
||||
let level = 1;
|
||||
let commands = ["_"];
|
||||
|
||||
@ -86,7 +85,6 @@ function parse_input(spec, input) {
|
||||
// Schema
|
||||
schema[ref(name, "type")] = "command";
|
||||
schema[ref(name, "help")] = help;
|
||||
schema[ref(name, "args")] = "";
|
||||
|
||||
// Alias
|
||||
for (const alias of aliases) {
|
||||
@ -96,7 +94,7 @@ function parse_input(spec, input) {
|
||||
// Control
|
||||
commands.push(name);
|
||||
level += 1;
|
||||
position = 0;
|
||||
position[level] = 0;
|
||||
break;
|
||||
}
|
||||
case "end": {
|
||||
@ -108,26 +106,21 @@ function parse_input(spec, input) {
|
||||
let aliases = elements.shift().split(",");
|
||||
let name = aliases[0];
|
||||
|
||||
const entry = { required: false, kind: "positional" };
|
||||
const entry = { name, dest: name, required: false, kind: "positional" };
|
||||
for (const element of elements) {
|
||||
const [attribute, attr_value] = element.split(":");
|
||||
let default_value;
|
||||
|
||||
if (attribute === "required") entry.required = true;
|
||||
else if (attribute === "repeatable") entry.repeatable = true;
|
||||
else if (attribute === "help") entry.help = attr_value;
|
||||
else if (attribute === "dest") entry.dest = attr_value;
|
||||
else if (attribute === "type") entry.kind = attr_value;
|
||||
else if (attribute === "default") default_value = attr_value;
|
||||
|
||||
// Defaults
|
||||
if (entry.dest === undefined) entry.dest = name;
|
||||
if (entry.value === undefined) entry.value = default_value;
|
||||
else if (attribute === "default") entry.value = attr_value;
|
||||
}
|
||||
|
||||
// Alias
|
||||
if (entry.kind === "positional") {
|
||||
name = String(position++);
|
||||
name = String(position[level]++);
|
||||
aliases = [`pos::${name}`];
|
||||
} else if (entry.kind === "rest") {
|
||||
aliases = ["__rest"];
|
||||
@ -148,7 +141,11 @@ function parse_input(spec, input) {
|
||||
}
|
||||
|
||||
// Register arg to command
|
||||
schema[ref("args")] += `${name},`;
|
||||
if (schema[ref("args")]) {
|
||||
schema[ref("args")] += `,${name}`;
|
||||
} else {
|
||||
schema[ref("args")] = `${name}`;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
@ -159,7 +156,7 @@ function parse_input(spec, input) {
|
||||
|
||||
commands = ["_"];
|
||||
level = 1;
|
||||
position = 0;
|
||||
position = [null, 0];
|
||||
}
|
||||
|
||||
function next_token_value() {
|
||||
@ -172,7 +169,7 @@ function parse_input(spec, input) {
|
||||
}
|
||||
|
||||
generate_schema();
|
||||
log({ schema, alias_map });
|
||||
// log({ schema, alias_map });
|
||||
|
||||
while (tokens.length > 0) {
|
||||
const token = take_one(tokens);
|
||||
@ -194,9 +191,9 @@ function parse_input(spec, input) {
|
||||
|
||||
if (!entry_ref && level === commands.length && token_type === "unk") {
|
||||
// Positional arguments are valid only for the latest command
|
||||
entry_ref = get_entry_ref(`pos::${position}`);
|
||||
entry_ref = get_entry_ref(`pos::${position[level]}`);
|
||||
if (entry_ref) {
|
||||
position += 1;
|
||||
position[level] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -208,10 +205,9 @@ function parse_input(spec, input) {
|
||||
const entry_type = get_entry_item(entry_ref, "type");
|
||||
if (entry_type === "command") {
|
||||
commands.push(token_key);
|
||||
position.push(0);
|
||||
} else if (entry_type === "argument") {
|
||||
const kind = get_entry_item(entry_ref, "kind");
|
||||
const dest = get_entry_item(entry_ref, "dest");
|
||||
const default_value = get_entry_item(entry_ref, "value");
|
||||
|
||||
let value;
|
||||
if (kind === "flag") value = true;
|
||||
@ -220,7 +216,7 @@ function parse_input(spec, input) {
|
||||
else if (kind === "rest") value = token_key;
|
||||
else throw new Error(`Invalid attribute type: '${kind}'`);
|
||||
|
||||
values[dest] = value;
|
||||
schema[join(entry_ref, "value")] = value;
|
||||
}
|
||||
|
||||
found = true;
|
||||
@ -228,7 +224,35 @@ function parse_input(spec, input) {
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
console.log(`(Skipping invalid argument: '${token_key}'`);
|
||||
console.log(`> Skipping invalid argument: (${token_type}) '${token_key}'`);
|
||||
}
|
||||
}
|
||||
|
||||
// Check required
|
||||
const values = {};
|
||||
let id = "";
|
||||
for (const command of commands) {
|
||||
id = join(id, command);
|
||||
let command_args = schema[join(id, "args")];
|
||||
if (!command_args) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const arg of command_args.split(",")) {
|
||||
const name = schema[join(id, arg, "name")];
|
||||
const dest = schema[join(id, arg, "dest")];
|
||||
const value = schema[join(id, arg, "value")];
|
||||
const required = schema[join(id, arg, "required")];
|
||||
|
||||
if (value === undefined && required) {
|
||||
throw new Error(`Argument '${name}' is required in command '${command}'`);
|
||||
}
|
||||
|
||||
if (values[dest] !== undefined) {
|
||||
values[`${command}_${dest}`] = value;
|
||||
} else {
|
||||
values[dest] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -237,11 +261,19 @@ function parse_input(spec, input) {
|
||||
return values;
|
||||
}
|
||||
|
||||
function parse_input_wrap(spec, input) {
|
||||
try {
|
||||
parse_input(spec, input);
|
||||
} catch (error) {
|
||||
console.error(`Error: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
const SPEC = [
|
||||
"command;build;Build a dev container",
|
||||
"argument;from;type:option;dest:from_name;help:Name of the base container",
|
||||
"argument;name;type:positional;required;help:Name of the container",
|
||||
"argument;name;type:option;required;help:Name of the container",
|
||||
"argument;nametwo;help:Name of the container",
|
||||
"argument;image,i;type:option;required;dest:image_name;help:Base image",
|
||||
"argument;include,I;type:option;repeatable;dest:include_paths;help:Include paths",
|
||||
"argument;verbose,v;type:flag;repeatable;default:0;help:Increase verbosity (-v, -vv, ...)",
|
||||
@ -253,7 +285,6 @@ const SPEC = [
|
||||
"argument;name;required;help:Building container name",
|
||||
"argument;cmd;type:rest;dest:cmd_rest;help:Command and args to run inside the container",
|
||||
"end",
|
||||
"argument;external;required;help:Name of the external container",
|
||||
"end",
|
||||
"command;stop;Stop a dev container",
|
||||
"argument;name;type:positional;required;help:Name of the container",
|
||||
@ -262,15 +293,17 @@ const SPEC = [
|
||||
];
|
||||
|
||||
const INPUTS = [
|
||||
"dev build --from base-dev --image node:18 --include src --include test -v -v -q mycontainer -- npm run start",
|
||||
"dev build container --dev --name webapp externalContainer",
|
||||
"dev build container --dev --name webapp externalContainer -- echo hi",
|
||||
"dev build -i myimage buildtwo --from base-dev --image node:18 --include src --include test -v -v -q mycontainer -- npm run start",
|
||||
"dev build buildname container -i myimage --dev --name webapp externalContainer",
|
||||
"dev build --image debian mybox container --dev --name webapp externalContainer -- echo hi",
|
||||
"dev stop mycontainer --kill",
|
||||
"dev stop mycontainer --kill -- not specified one",
|
||||
"dev build --from alpine --image node:20 mybox -- echo hi",
|
||||
"dev build --aaaaa --from alpine --image node:20 --bbbbb orb mybox ccccc -- echo hi",
|
||||
];
|
||||
|
||||
INPUTS.forEach((input) => {
|
||||
console.log("\n\n------------------------\n");
|
||||
console.log("> ", input);
|
||||
parse_input(SPEC, input);
|
||||
parse_input_wrap(SPEC, input);
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user