v1-minimal
This commit is contained in:
parent
f5a64b51ff
commit
5231e7375b
284
barg.js
284
barg.js
@ -1,22 +1,87 @@
|
|||||||
const ELEMENTS_SEP = ";";
|
function log(text) {
|
||||||
const ALIAS_SEP = ",";
|
console.log(JSON.stringify(text, null, 2));
|
||||||
const ATTR_SEP = ":";
|
}
|
||||||
|
|
||||||
let var_values = {};
|
function join(...rest) {
|
||||||
let required_values = [];
|
return rest.filter((v) => !!v).join(".");
|
||||||
|
}
|
||||||
|
|
||||||
let schema = {};
|
function take_one(list) {
|
||||||
let alias_map = {};
|
return list.shift();
|
||||||
|
}
|
||||||
|
|
||||||
let position = 0;
|
function take_all(list) {
|
||||||
let level = 1;
|
const all = [...list];
|
||||||
let commands = ["root"];
|
list.length = 0;
|
||||||
|
return all;
|
||||||
|
}
|
||||||
|
|
||||||
function register_command(raw_entry) {
|
function tokenize(input) {
|
||||||
const header = raw_entry.split(ELEMENTS_SEP).slice(1);
|
const raw_tokens = input.trim().split(/\s+/).slice(1); // skip command
|
||||||
const aliases = header.shift().split(ALIAS_SEP);
|
const tokens = [];
|
||||||
|
|
||||||
|
while (raw_tokens.length) {
|
||||||
|
const raw_token = take_one(raw_tokens);
|
||||||
|
|
||||||
|
if (raw_token === "--") {
|
||||||
|
tokens.push(`rest::${take_all(raw_tokens).join(" ")}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let key;
|
||||||
|
let value;
|
||||||
|
|
||||||
|
if (raw_token.startsWith("--")) {
|
||||||
|
[key, value] = raw_token.slice(2).split("=");
|
||||||
|
tokens.push(`narg::${key}`);
|
||||||
|
} else if (raw_token.startsWith("-")) {
|
||||||
|
[key, value] = raw_token.slice(1).split("=");
|
||||||
|
tokens.push(...key.split("").map((k) => `narg::${k}`));
|
||||||
|
} else {
|
||||||
|
tokens.push(`unk::${raw_token}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value !== undefined) {
|
||||||
|
tokens.push(`value::${value}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse_input(spec, input) {
|
||||||
|
const tokens = tokenize(input);
|
||||||
|
const values = {};
|
||||||
|
|
||||||
|
const schema = {};
|
||||||
|
const alias_map = {};
|
||||||
|
|
||||||
|
let position = 0;
|
||||||
|
let level = 1;
|
||||||
|
let commands = ["root"];
|
||||||
|
|
||||||
|
function ref(...rest) {
|
||||||
|
return commands.slice(0, level).concat(rest).join(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_entry_ref(key) {
|
||||||
|
return alias_map[ref(key)];
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_entry_item(...keys) {
|
||||||
|
return schema[join(...keys)];
|
||||||
|
}
|
||||||
|
|
||||||
|
function generate_schema() {
|
||||||
|
for (const entry of spec) {
|
||||||
|
const elements = entry.split(";");
|
||||||
|
const type = elements.shift();
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case "command": {
|
||||||
|
const aliases = elements.shift().split(",");
|
||||||
const name = aliases[0];
|
const name = aliases[0];
|
||||||
const help = header.shift();
|
const help = elements.shift();
|
||||||
|
|
||||||
// Schema
|
// Schema
|
||||||
schema[ref(name, "type")] = "command";
|
schema[ref(name, "type")] = "command";
|
||||||
@ -31,23 +96,20 @@ function register_command(raw_entry) {
|
|||||||
commands.push(name);
|
commands.push(name);
|
||||||
level += 1;
|
level += 1;
|
||||||
position = 0;
|
position = 0;
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
function end_command() {
|
case "end": {
|
||||||
level -= 1;
|
level -= 1;
|
||||||
commands.pop();
|
commands.pop();
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
function register_argument(raw_entry) {
|
case "argument": {
|
||||||
const elements = raw_entry.split(ELEMENTS_SEP).slice(1);
|
let aliases = elements.shift().split(",");
|
||||||
let aliases = elements.shift().split(ALIAS_SEP);
|
|
||||||
let name = aliases[0];
|
let name = aliases[0];
|
||||||
const entry = {
|
|
||||||
required: false,
|
const entry = { required: false, kind: "positional" };
|
||||||
kind: "positional",
|
|
||||||
};
|
|
||||||
for (const element of elements) {
|
for (const element of elements) {
|
||||||
const [attribute, attr_value] = element.split(ATTR_SEP);
|
const [attribute, attr_value] = element.split(":");
|
||||||
|
|
||||||
if (attribute === "required") entry.required = true;
|
if (attribute === "required") entry.required = true;
|
||||||
else if (attribute === "repeatable") entry.repeatable = true;
|
else if (attribute === "repeatable") entry.repeatable = true;
|
||||||
@ -70,9 +132,11 @@ function register_argument(raw_entry) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const alias of aliases) {
|
for (const alias of aliases) {
|
||||||
if (entry.kind === "option" || entry.kind === "flag")
|
if (entry.kind === "option" || entry.kind === "flag") {
|
||||||
alias_map[ref(`narg::${alias}`)] = ref(name);
|
alias_map[ref(`narg::${alias}`)] = ref(name);
|
||||||
else alias_map[ref(alias)] = ref(name);
|
} else {
|
||||||
|
alias_map[ref(alias)] = ref(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Schema
|
// Schema
|
||||||
@ -80,103 +144,70 @@ function register_argument(raw_entry) {
|
|||||||
for (const [key, value] of Object.entries(entry)) {
|
for (const [key, value] of Object.entries(entry)) {
|
||||||
schema[ref(name, key)] = value;
|
schema[ref(name, key)] = value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function generate_schema(spec) {
|
|
||||||
for (const entry of spec) {
|
|
||||||
const elements = entry.split(ELEMENTS_SEP);
|
|
||||||
const type = elements.shift();
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case "command":
|
|
||||||
register_command(entry);
|
|
||||||
break;
|
|
||||||
case "end":
|
|
||||||
end_command();
|
|
||||||
break;
|
|
||||||
case "argument":
|
|
||||||
register_argument(entry);
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
throw new Error(`Invalid entry type: '${type}'`);
|
throw new Error(`Invalid entry type: '${type}'`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
position = 0;
|
|
||||||
level = 1;
|
|
||||||
commands = ["root"];
|
commands = ["root"];
|
||||||
}
|
|
||||||
|
|
||||||
function parse_input(spec, input) {
|
|
||||||
// rest
|
|
||||||
var_values = {};
|
|
||||||
required_values = [];
|
|
||||||
position = 0;
|
|
||||||
level = 1;
|
level = 1;
|
||||||
commands = ["root"];
|
position = 0;
|
||||||
schema = {};
|
|
||||||
alias_map = {};
|
|
||||||
|
|
||||||
// code
|
|
||||||
generate_schema(spec);
|
|
||||||
const tokens = tokenize(input);
|
|
||||||
|
|
||||||
// log({ schema, alias_map });
|
|
||||||
|
|
||||||
function next_token() {
|
|
||||||
const tagged_token = tokens.shift();
|
|
||||||
const [type, token] = tagged_token.split("::");
|
|
||||||
return { original: tagged_token, type, token };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function next_value() {
|
function next_token_value() {
|
||||||
const tagged_token = next_token();
|
const token = take_one(tokens);
|
||||||
if (tagged_token.type !== "unk") {
|
const [token_type, token_key] = token.split("::");
|
||||||
throw new Error(`Expected option value, but got: ${tagged_token.token}`);
|
if (token_type !== "unk") {
|
||||||
|
throw new Error(`Expected option value, but got: ${token_type}. Token: '${token_key}'`);
|
||||||
}
|
}
|
||||||
return tagged_token.token;
|
return token_key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
generate_schema();
|
||||||
|
|
||||||
while (tokens.length > 0) {
|
while (tokens.length > 0) {
|
||||||
const {
|
const token = take_one(tokens);
|
||||||
original: token,
|
const [token_type, token_key] = token.split("::");
|
||||||
type: token_type,
|
|
||||||
token: token_value,
|
|
||||||
} = next_token();
|
|
||||||
if (token_type === "rest") {
|
if (token_type === "rest") {
|
||||||
var_values["__rest"] = token_value;
|
values["__rest"] = token_key;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bubble up arguments -> Loop over all command levels starting by the latest
|
||||||
let found = false;
|
let found = false;
|
||||||
for (let i = commands.length; i >= 1; i--) {
|
for (level = commands.length; level >= 1; level--) {
|
||||||
level = i;
|
let entry_ref = get_entry_ref(token);
|
||||||
let entry_ref = alias_map[ref(token)];
|
|
||||||
|
|
||||||
|
// Positional arguments are valid only for the latest command
|
||||||
if (!entry_ref && level === commands.length && token_type === "unk") {
|
if (!entry_ref && level === commands.length && token_type === "unk") {
|
||||||
entry_ref = alias_map[ref(`pos::${position++}`)];
|
entry_ref = get_entry_ref(`pos::${position}`);
|
||||||
|
if (entry_ref) {
|
||||||
|
position += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try with parent command
|
||||||
if (!entry_ref) {
|
if (!entry_ref) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [_token_type, token_value] = token.split("::");
|
const entry_type = get_entry_item(entry_ref, "type");
|
||||||
const type = schema[join(entry_ref, "type")];
|
if (entry_type === "command") {
|
||||||
if (type === "command") {
|
commands.push(token_key);
|
||||||
commands.push(token_value);
|
} else if (entry_type === "argument") {
|
||||||
level += commands.length;
|
const kind = get_entry_item(entry_ref, "kind");
|
||||||
} else if (type === "argument") {
|
const dest = get_entry_item(entry_ref, "dest");
|
||||||
const kind = schema[join(entry_ref, "kind")];
|
const value = get_entry_item(entry_ref, "value");
|
||||||
const dest = schema[join(entry_ref, "dest")];
|
|
||||||
const value = schema[join(entry_ref, "value")];
|
|
||||||
|
|
||||||
var_values[dest] = value; // default one?
|
values[dest] = value; // default one
|
||||||
|
|
||||||
if (kind === "flag") var_values[dest] = true;
|
if (kind === "flag") values[dest] = true;
|
||||||
else if (kind === "option") var_values[dest] = next_value();
|
else if (kind === "option") values[dest] = next_token_value();
|
||||||
else if (kind === "positional") var_values[dest] = token_value;
|
else if (kind === "positional") values[dest] = token_key;
|
||||||
else if (kind === "rest") var_values[dest] = token_value;
|
else if (kind === "rest") values[dest] = token_key;
|
||||||
|
else throw new Error(`Invalid attribute type: '${kind}'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
found = true;
|
found = true;
|
||||||
@ -184,67 +215,14 @@ function parse_input(spec, input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
console.log(`(Skipping invalid argument: '${token_value}'`);
|
console.log(`(Skipping invalid argument: '${token_key}'`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log({ commands, var_values });
|
log({ commands, values });
|
||||||
return var_values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
function tokenize(input) {
|
|
||||||
const raw_tokens = input.split(/\s+/);
|
|
||||||
raw_tokens.shift(); // drop command
|
|
||||||
|
|
||||||
const tokens = [];
|
|
||||||
while (raw_tokens.length > 0) {
|
|
||||||
let token = take_token(raw_tokens);
|
|
||||||
let value;
|
|
||||||
|
|
||||||
if (token === "--") {
|
|
||||||
tokens.push(`rest::${take_all(raw_tokens).join(" ")}`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (token.startsWith("--")) {
|
|
||||||
[token, value] = token.slice(2).split("=");
|
|
||||||
tokens.push(`narg::${token}`);
|
|
||||||
} else if (token.startsWith("-")) {
|
|
||||||
[token, value] = token.slice(1).split("=");
|
|
||||||
tokens.push(...token.split("").map((t) => `narg::${t}`));
|
|
||||||
} else {
|
|
||||||
tokens.push(`unk::${token}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value) {
|
|
||||||
tokens.push(`value::${value}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return tokens;
|
|
||||||
}
|
|
||||||
|
|
||||||
function take_token(tokens) {
|
|
||||||
return tokens.shift();
|
|
||||||
}
|
|
||||||
|
|
||||||
function take_all(tokens) {
|
|
||||||
const all = [...tokens];
|
|
||||||
tokens.length = 0;
|
|
||||||
return all;
|
|
||||||
}
|
|
||||||
|
|
||||||
function ref(...rest) {
|
|
||||||
return commands.slice(0, level).concat(rest).join(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
function join(...rest) {
|
|
||||||
return rest.filter((v) => !!v).join(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
function log(text) {
|
|
||||||
console.log(JSON.stringify(text, null, 2));
|
|
||||||
}
|
|
||||||
const SPEC = [
|
const SPEC = [
|
||||||
"command;build;Build a dev container",
|
"command;build;Build a dev container",
|
||||||
"argument;from;type:option;map:from_name;help:Name of the base container",
|
"argument;from;type:option;map:from_name;help:Name of the base container",
|
||||||
|
|||||||
@ -1,3 +1,6 @@
|
|||||||
{
|
{
|
||||||
"type": "module"
|
"type": "module",
|
||||||
|
"prettier": {
|
||||||
|
"printWidth": 100
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user