use dest for rest arg too

This commit is contained in:
Tomas Mirchev 2025-10-30 03:02:08 +02:00
parent 5231e7375b
commit 4ee97e16d0

57
barg.js
View File

@ -58,7 +58,7 @@ function parse_input(spec, input) {
let position = 0; let position = 0;
let level = 1; let level = 1;
let commands = ["root"]; let commands = ["_"];
function ref(...rest) { function ref(...rest) {
return commands.slice(0, level).concat(rest).join("."); return commands.slice(0, level).concat(rest).join(".");
@ -86,6 +86,7 @@ function parse_input(spec, input) {
// Schema // Schema
schema[ref(name, "type")] = "command"; schema[ref(name, "type")] = "command";
schema[ref(name, "help")] = help; schema[ref(name, "help")] = help;
schema[ref(name, "args")] = "";
// Alias // Alias
for (const alias of aliases) { for (const alias of aliases) {
@ -110,17 +111,18 @@ function parse_input(spec, input) {
const entry = { required: false, kind: "positional" }; const entry = { required: false, kind: "positional" };
for (const element of elements) { for (const element of elements) {
const [attribute, attr_value] = element.split(":"); const [attribute, attr_value] = element.split(":");
let default_value;
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;
else if (attribute === "help") entry.help = attr_value; else if (attribute === "help") entry.help = attr_value;
else if (attribute === "default") entry.default_value = attr_value;
else if (attribute === "dest") entry.dest = attr_value; else if (attribute === "dest") entry.dest = attr_value;
else if (attribute === "type") entry.kind = attr_value; else if (attribute === "type") entry.kind = attr_value;
else if (attribute === "default") default_value = attr_value;
// Defaults // Defaults
if (entry.dest === undefined) entry.dest = name; if (entry.dest === undefined) entry.dest = name;
if (entry.value === undefined) entry.value = entry.default_value; if (entry.value === undefined) entry.value = default_value;
} }
// Alias // Alias
@ -144,6 +146,10 @@ function parse_input(spec, input) {
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;
} }
// Register arg to command
schema[ref("args")] += `${name},`;
break; break;
} }
default: default:
@ -151,7 +157,7 @@ function parse_input(spec, input) {
} }
} }
commands = ["root"]; commands = ["_"];
level = 1; level = 1;
position = 0; position = 0;
} }
@ -166,22 +172,28 @@ function parse_input(spec, input) {
} }
generate_schema(); generate_schema();
log({ schema, alias_map });
while (tokens.length > 0) { while (tokens.length > 0) {
const token = take_one(tokens); const token = take_one(tokens);
const [token_type, token_key] = token.split("::"); const [token_type, token_key] = token.split("::");
if (token_type === "rest") {
values["__rest"] = token_key;
continue;
}
// Bubble up arguments -> Loop over all command levels starting by the latest // Bubble up arguments -> Loop over all command levels starting by the latest
let found = false; let found = false;
for (level = commands.length; level >= 1; level--) { for (level = commands.length; level >= 1; level--) {
let entry_ref = get_entry_ref(token); let entry_ref = get_entry_ref(token);
// Positional arguments are valid only for the latest command // Check for rest argument
if (token_type === "rest") {
entry_ref = get_entry_ref("__rest");
}
if (token_type === "narg") {
entry_ref = get_entry_ref(token);
}
if (!entry_ref && level === commands.length && token_type === "unk") { 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}`);
if (entry_ref) { if (entry_ref) {
position += 1; position += 1;
@ -199,15 +211,16 @@ function parse_input(spec, input) {
} else if (entry_type === "argument") { } else if (entry_type === "argument") {
const kind = get_entry_item(entry_ref, "kind"); const kind = get_entry_item(entry_ref, "kind");
const dest = get_entry_item(entry_ref, "dest"); const dest = get_entry_item(entry_ref, "dest");
const value = get_entry_item(entry_ref, "value"); const default_value = get_entry_item(entry_ref, "value");
values[dest] = value; // default one let value;
if (kind === "flag") value = true;
if (kind === "flag") values[dest] = true; else if (kind === "option") value = next_token_value();
else if (kind === "option") values[dest] = next_token_value(); else if (kind === "positional") value = token_key;
else if (kind === "positional") values[dest] = token_key; else if (kind === "rest") value = token_key;
else if (kind === "rest") values[dest] = token_key;
else throw new Error(`Invalid attribute type: '${kind}'`); else throw new Error(`Invalid attribute type: '${kind}'`);
values[dest] = value;
} }
found = true; found = true;
@ -219,23 +232,26 @@ function parse_input(spec, input) {
} }
} }
commands.shift();
log({ commands, values }); log({ commands, values });
return values; return values;
} }
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;dest:from_name;help:Name of the base container",
"argument;name;type:positional;required;help:Name of the container", "argument;name;type:positional;required;help:Name of the container",
"argument;image,i;type:option;required;map:image_name;help:Base image", "argument;name;type:option;required;help:Name of the container",
"argument;include,I;type:option;repeatable;map:include_paths;help:Include paths", "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, ...)", "argument;verbose,v;type:flag;repeatable;default:0;help:Increase verbosity (-v, -vv, ...)",
"argument;quiet,q;type:flag;default:false;help:Suppress all output", "argument;quiet,q;type:flag;default:false;help:Suppress all output",
"argument;cmd;type:rest;map:cmd_rest;help:Command and args to run inside the container", "argument;cmd;type:rest;help:Command and args to run inside the container",
"argument;name;dest:name_opt;type:option;help:Building container name", "argument;name;dest:name_opt;type:option;help:Building container name",
"command;container;Container building", "command;container;Container building",
"argument;dev;type:flag;help:Is dev or not", "argument;dev;type:flag;help:Is dev or not",
"argument;name;required;help:Building container name", "argument;name;required;help:Building container name",
"argument;cmd;type:rest;dest:cmd_rest;help:Command and args to run inside the container",
"end", "end",
"argument;external;required;help:Name of the external container", "argument;external;required;help:Name of the external container",
"end", "end",
@ -248,6 +264,7 @@ const SPEC = [
const INPUTS = [ const INPUTS = [
"dev build --from base-dev --image node:18 --include src --include test -v -v -q mycontainer -- npm run start", "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",
"dev build container --dev --name webapp externalContainer -- echo hi",
"dev stop mycontainer --kill", "dev stop mycontainer --kill",
"dev build --from alpine --image node:20 mybox -- echo hi", "dev build --from alpine --image node:20 mybox -- echo hi",
]; ];