diff --git a/barg.js b/barg.js index de31449..5ffa56c 100644 --- a/barg.js +++ b/barg.js @@ -58,7 +58,7 @@ function parse_input(spec, input) { let position = 0; let level = 1; - let commands = ["root"]; + let commands = ["_"]; function ref(...rest) { return commands.slice(0, level).concat(rest).join("."); @@ -86,6 +86,7 @@ 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) { @@ -110,17 +111,18 @@ function parse_input(spec, input) { const entry = { 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 === "default") entry.default_value = 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 = entry.default_value; + if (entry.value === undefined) entry.value = default_value; } // Alias @@ -144,6 +146,10 @@ function parse_input(spec, input) { for (const [key, value] of Object.entries(entry)) { schema[ref(name, key)] = value; } + + // Register arg to command + schema[ref("args")] += `${name},`; + break; } default: @@ -151,7 +157,7 @@ function parse_input(spec, input) { } } - commands = ["root"]; + commands = ["_"]; level = 1; position = 0; } @@ -166,22 +172,28 @@ function parse_input(spec, input) { } generate_schema(); + log({ schema, alias_map }); while (tokens.length > 0) { const token = take_one(tokens); 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 let found = false; for (level = commands.length; level >= 1; level--) { 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") { + // Positional arguments are valid only for the latest command entry_ref = get_entry_ref(`pos::${position}`); if (entry_ref) { position += 1; @@ -199,15 +211,16 @@ function parse_input(spec, input) { } else if (entry_type === "argument") { const kind = get_entry_item(entry_ref, "kind"); 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 - - if (kind === "flag") values[dest] = true; - else if (kind === "option") values[dest] = next_token_value(); - else if (kind === "positional") values[dest] = token_key; - else if (kind === "rest") values[dest] = token_key; + let value; + if (kind === "flag") value = true; + else if (kind === "option") value = next_token_value(); + else if (kind === "positional") value = token_key; + else if (kind === "rest") value = token_key; else throw new Error(`Invalid attribute type: '${kind}'`); + + values[dest] = value; } found = true; @@ -219,23 +232,26 @@ function parse_input(spec, input) { } } + commands.shift(); log({ commands, values }); return values; } const SPEC = [ "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;image,i;type:option;required;map:image_name;help:Base image", - "argument;include,I;type:option;repeatable;map:include_paths;help:Include paths", + "argument;name;type:option;required;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, ...)", "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", "command;container;Container building", "argument;dev;type:flag;help:Is dev or not", "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", @@ -248,6 +264,7 @@ 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 stop mycontainer --kill", "dev build --from alpine --image node:20 mybox -- echo hi", ];