From 7f7c006007c42376d711d71f42e5577d431c6b64 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 23 Mar 2015 23:25:25 -0300 Subject: Add support for space in long option assignments. Support assignments in long option in two tokens, so that ``` luarocks --only-from /foo/bar build luasocket ``` is accepted as a synonym to: ``` luarocks --only-from=/foo/bar build luasocket ``` (This is consistent with the GNU getopt handling of "--" long options.) Closes #65. --- src/luarocks/command_line.lua | 15 ++---- src/luarocks/util.lua | 103 +++++++++++++++++++++++++++++++++++++--- src/luarocks/write_rockspec.lua | 4 -- 3 files changed, 100 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua index 96d7cb11..9cc34c42 100644 --- a/src/luarocks/command_line.lua +++ b/src/luarocks/command_line.lua @@ -64,6 +64,9 @@ function command_line.run_command(...) end local nonflags = { util.parse_flags(unpack(args)) } local flags = table.remove(nonflags, 1) + if flags.ERROR then + die(flags.ERROR.." See --help.") + end if flags["from"] then flags["server"] = flags["from"] end if flags["only-from"] then flags["only-server"] = flags["only-from"] end @@ -126,16 +129,10 @@ function command_line.run_command(...) end if flags["branch"] then - if flags["branch"] == true or flags["branch"] == "" then - die("Argument error: use --branch=") - end cfg.branch = flags["branch"] end if flags["tree"] then - if flags["tree"] == true or flags["tree"] == "" then - die("Argument error: use --tree=") - end local named = false for _, tree in ipairs(cfg.rocks_trees) do if type(tree) == "table" and flags["tree"] == tree.name then @@ -178,17 +175,11 @@ function command_line.run_command(...) cfg.variables.SCRIPTS_DIR = cfg.deploy_bin_dir if flags["server"] then - if flags["server"] == true then - die("Argument error: use --server=") - end local protocol, path = dir.split_url(flags["server"]) table.insert(cfg.rocks_servers, 1, protocol.."://"..path) end if flags["only-server"] then - if flags["only-server"] == true then - die("Argument error: use --only-server=") - end cfg.rocks_servers = { flags["only-server"] } end diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 40af35bd..6319ae14 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -63,6 +63,63 @@ function util.matchquote(s) return (s:gsub("[?%-+*%[%].%%()$^]","%%%1")) end +local supported_flags = { + ["all"] = true, + ["api-key"] = "", + ["append"] = true, + ["arch"] = "", + ["bin"] = true, + ["binary"] = true, + ["branch"] = "", + ["debug"] = true, + ["deps"] = true, + ["deps-mode"] = "", + ["detailed"] = "\"\"", + ["extensions"] = true, + ["force"] = true, + ["from"] = "", + ["help"] = true, + ["home"] = true, + ["homepage"] = true, + ["keep"] = true, + ["lib"] = true, + ["license"] = "\"\"", + ["list"] = true, + ["local"] = true, + ["local-tree"] = true, + ["lr-bin"] = true, + ["lr-cpath"] = true, + ["lr-path"] = true, + ["lua-version"] = "", + ["modules"] = true, + ["mversion"] = true, + ["no-refresh"] = true, + ["nodeps"] = true, + ["old-versions"] = true, + ["only-from"] = "", + ["only-server"] = "", + ["only-sources"] = "", + ["only-sources-from"] = "", + ["outdated"] = true, + ["output"] = "", + ["pack-binary-rock"] = true, + ["porcelain"] = true, + ["quick"] = true, + ["rock-dir"] = true, + ["rock-tree"] = true, + ["rockspec"] = true, + ["server"] = "", + ["skip-pack"] = true, + ["source"] = true, + ["summary"] = "\"\"", + ["tag"] = "", + ["timeout"] = "", + ["to"] = "", + ["tree"] = "", + ["verbose"] = true, + ["version"] = true, +} + --- Extract flags from an arguments list. -- Given string arguments, extract flag arguments into a flags set. -- For example, given "foo", "--tux=beep", "--bla", "bar", "--baz", @@ -71,19 +128,53 @@ end function util.parse_flags(...) local args = {...} local flags = {} - for i = #args, 1, -1 do + local i = 1 + local out = {} + local ignore_flags = false + while i <= #args do local flag = args[i]:match("^%-%-(.*)") - if flag then + if flag == "--" then + ignore_flags = true + end + if flag and not ignore_flags then local var,val = flag:match("([a-z_%-]*)=(.*)") if val then - flags[var] = val + local vartype = supported_flags[var] + if type(vartype) == "string" then + flags[var] = val + else + if vartype then + return { ERROR = "Invalid argument: flag --"..var.." does not take an parameter." } + else + return { ERROR = "Invalid argument: unknown flag --"..var.."." } + end + end else - flags[flag] = true + local var = flag + local vartype = supported_flags[var] + if type(vartype) == "string" then + i = i + 1 + local val = args[i] + if not val then + return { ERROR = "Invalid argument: flag --"..var.."="..vartype.." expects a parameter." } + end + if val:match("^%-%-.*") then + return { ERROR = "Invalid argument: flag --"..var.."="..vartype.." expects a parameter (if you really want to pass "..val.." as an argument to --"..var..", use --"..var.."="..val..")." } + else + flags[var] = val + end + elseif vartype == true then + flags[var] = true + else + return { ERROR = "Invalid argument: unknown flag --"..var.."." } + end end - table.remove(args, i) + else + table.insert(out, args[i]) end + i = i + 1 end - return flags, unpack(args) + return flags, unpack(out) end --- Build a sequence of flags for forwarding from one command to diff --git a/src/luarocks/write_rockspec.lua b/src/luarocks/write_rockspec.lua index 5b129ab0..fb3a120e 100644 --- a/src/luarocks/write_rockspec.lua +++ b/src/luarocks/write_rockspec.lua @@ -209,10 +209,6 @@ function write_rockspec.run(...) elseif not url_or_dir then url_or_dir = version end - - if flags["tag"] == true then - return nil, "Incorrect usage: --tag requires an argument. "..util.see_help("write_rockspec") - end if flags["tag"] then if not version then -- cgit v1.2.3-55-g6feb