aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Ouellette <oue.paul18@gmail.com>2019-08-16 12:43:17 -0400
committerPaul Ouellette <oue.paul18@gmail.com>2019-08-17 00:56:37 -0400
commit6a751c541814534015e4b8b651f71332d293ace1 (patch)
treebc16caa164ff526210fdb4389a94a286d8beb8ee
parentcc559584eac8c0c20037085d2fed6dd8cfb93ae4 (diff)
downloadluarocks-6a751c541814534015e4b8b651f71332d293ace1.tar.gz
luarocks-6a751c541814534015e4b8b651f71332d293ace1.tar.bz2
luarocks-6a751c541814534015e4b8b651f71332d293ace1.zip
Update to argparse 0.7.0
-rw-r--r--src/luarocks/argparse.lua165
1 files changed, 115 insertions, 50 deletions
diff --git a/src/luarocks/argparse.lua b/src/luarocks/argparse.lua
index a91da957..dc6cdb0d 100644
--- a/src/luarocks/argparse.lua
+++ b/src/luarocks/argparse.lua
@@ -584,12 +584,17 @@ function Option:_is_vararg()
584 return self._maxargs ~= self._minargs 584 return self._maxargs ~= self._minargs
585end 585end
586 586
587function Parser:_get_fullname() 587function Parser:_get_fullname(exclude_root)
588 local parent = self._parent 588 local parent = self._parent
589 if exclude_root and not parent then
590 return ""
591 end
589 local buf = {self._name} 592 local buf = {self._name}
590 593
591 while parent do 594 while parent do
592 table.insert(buf, 1, parent._name) 595 if not exclude_root or parent._parent then
596 table.insert(buf, 1, parent._name)
597 end
593 parent = parent._parent 598 parent = parent._parent
594 end 599 end
595 600
@@ -1234,45 +1239,54 @@ function Parser:_bash_option_args(buf, indent)
1234 end 1239 end
1235end 1240end
1236 1241
1237function Parser:_bash_get_cmd(buf) 1242function Parser:_bash_get_cmd(buf, indent)
1238 local cmds = {} 1243 if #self._commands == 0 then
1244 return
1245 end
1246
1247 table.insert(buf, (" "):rep(indent) .. 'args=("${args[@]:1}")')
1248 table.insert(buf, (" "):rep(indent) .. 'for arg in "${args[@]}"; do')
1249 table.insert(buf, (" "):rep(indent + 4) .. 'case "$arg" in')
1250
1239 for _, command in ipairs(self._commands) do 1251 for _, command in ipairs(self._commands) do
1240 if not command._is_help_command then 1252 table.insert(buf, (" "):rep(indent + 8) .. table.concat(command._aliases, "|") .. ")")
1241 table.insert(cmds, (" "):rep(12) .. table.concat(command._aliases, "|") .. ")") 1253 if self._parent then
1242 table.insert(cmds, (" "):rep(16) .. 'cmd="' .. command._name .. '"') 1254 table.insert(buf, (" "):rep(indent + 12) .. 'cmd="$cmd ' .. command._name .. '"')
1243 table.insert(cmds, (" "):rep(16) .. "break") 1255 else
1244 table.insert(cmds, (" "):rep(16) .. ";;") 1256 table.insert(buf, (" "):rep(indent + 12) .. 'cmd="' .. command._name .. '"')
1245 end 1257 end
1258 table.insert(buf, (" "):rep(indent + 12) .. 'opts="$opts ' .. command:_get_options() .. '"')
1259 command:_bash_get_cmd(buf, indent + 12)
1260 table.insert(buf, (" "):rep(indent + 12) .. "break")
1261 table.insert(buf, (" "):rep(indent + 12) .. ";;")
1246 end 1262 end
1247 1263
1248 if #cmds > 0 then 1264 table.insert(buf, (" "):rep(indent + 4) .. "esac")
1249 table.insert(buf, (" "):rep(4) .. "for arg in ${COMP_WORDS[@]:1}; do") 1265 table.insert(buf, (" "):rep(indent) .. "done")
1250 table.insert(buf, (" "):rep(8) .. 'case "$arg" in')
1251 table.insert(buf, table.concat(cmds, "\n"))
1252 table.insert(buf, (" "):rep(8) .. "esac")
1253 table.insert(buf, (" "):rep(4) .. "done\n")
1254 end
1255end 1266end
1256 1267
1257function Parser:_bash_cmd_completions(buf) 1268function Parser:_bash_cmd_completions(buf)
1258 local subcmds = {} 1269 local cmd_buf = {}
1259 for _, command in ipairs(self._commands) do 1270 if self._parent then
1260 if #command._options > 0 and not command._is_help_command then 1271 self:_bash_option_args(cmd_buf, 12)
1261 table.insert(subcmds, (" "):rep(8) .. command._name .. ")") 1272 end
1262 command:_bash_option_args(subcmds, 12) 1273 if #self._commands > 0 then
1263 table.insert(subcmds, (" "):rep(12) .. 'opts="$opts ' .. command:_get_options() .. '"') 1274 table.insert(cmd_buf, (" "):rep(12) .. 'COMPREPLY=($(compgen -W "' .. self:_get_commands() .. '" -- "$cur"))')
1264 table.insert(subcmds, (" "):rep(12) .. ";;") 1275 elseif self._is_help_command then
1265 end 1276 table.insert(cmd_buf, (" "):rep(12)
1277 .. 'COMPREPLY=($(compgen -W "'
1278 .. self._parent:_get_commands()
1279 .. '" -- "$cur"))')
1280 end
1281 if #cmd_buf > 0 then
1282 table.insert(buf, (" "):rep(8) .. "'" .. self:_get_fullname(true) .. "')")
1283 table.insert(buf, table.concat(cmd_buf, "\n"))
1284 table.insert(buf, (" "):rep(12) .. ";;")
1266 end 1285 end
1267 1286
1268 table.insert(buf, (" "):rep(4) .. 'case "$cmd" in') 1287 for _, command in ipairs(self._commands) do
1269 table.insert(buf, (" "):rep(8) .. self._basename .. ")") 1288 command:_bash_cmd_completions(buf)
1270 table.insert(buf, (" "):rep(12) .. 'COMPREPLY=($(compgen -W "' .. self:_get_commands() .. '" -- "$cur"))')
1271 table.insert(buf, (" "):rep(12) .. ";;")
1272 if #subcmds > 0 then
1273 table.insert(buf, table.concat(subcmds, "\n"))
1274 end 1289 end
1275 table.insert(buf, (" "):rep(4) .. "esac\n")
1276end 1290end
1277 1291
1278function Parser:get_bash_complete() 1292function Parser:get_bash_complete()
@@ -1281,17 +1295,20 @@ function Parser:get_bash_complete()
1281 local buf = {([[ 1295 local buf = {([[
1282_%s() { 1296_%s() {
1283 local IFS=$' \t\n' 1297 local IFS=$' \t\n'
1284 local cur prev cmd opts arg 1298 local args cur prev cmd opts arg
1299 args=("${COMP_WORDS[@]}")
1285 cur="${COMP_WORDS[COMP_CWORD]}" 1300 cur="${COMP_WORDS[COMP_CWORD]}"
1286 prev="${COMP_WORDS[COMP_CWORD-1]}" 1301 prev="${COMP_WORDS[COMP_CWORD-1]}"
1287 cmd="%s"
1288 opts="%s" 1302 opts="%s"
1289]]):format(self._basename, self._basename, self:_get_options())} 1303]]):format(self._basename, self:_get_options())}
1290 1304
1291 self:_bash_option_args(buf, 4) 1305 self:_bash_option_args(buf, 4)
1292 self:_bash_get_cmd(buf) 1306 self:_bash_get_cmd(buf, 4)
1293 if #self._commands > 0 then 1307 if #self._commands > 0 then
1308 table.insert(buf, "")
1309 table.insert(buf, (" "):rep(4) .. 'case "$cmd" in')
1294 self:_bash_cmd_completions(buf) 1310 self:_bash_cmd_completions(buf)
1311 table.insert(buf, (" "):rep(4) .. "esac\n")
1295 end 1312 end
1296 1313
1297 table.insert(buf, ([=[ 1314 table.insert(buf, ([=[
@@ -1441,23 +1458,48 @@ local function fish_escape(string)
1441 return string:gsub("[\\']", "\\%0") 1458 return string:gsub("[\\']", "\\%0")
1442end 1459end
1443 1460
1444function Parser:_fish_complete_help(buf, prefix) 1461function Parser:_fish_get_cmd(buf, indent)
1462 if #self._commands == 0 then
1463 return
1464 end
1465
1466 table.insert(buf, (" "):rep(indent) .. "set -e cmdline[1]")
1467 table.insert(buf, (" "):rep(indent) .. "for arg in $cmdline")
1468 table.insert(buf, (" "):rep(indent + 4) .. "switch $arg")
1469
1470 for _, command in ipairs(self._commands) do
1471 table.insert(buf, (" "):rep(indent + 8) .. "case " .. table.concat(command._aliases, " "))
1472 table.insert(buf, (" "):rep(indent + 12) .. "set cmd $cmd " .. command._name)
1473 command:_fish_get_cmd(buf, indent + 12)
1474 table.insert(buf, (" "):rep(indent + 12) .. "break")
1475 end
1476
1477 table.insert(buf, (" "):rep(indent + 4) .. "end")
1478 table.insert(buf, (" "):rep(indent) .. "end")
1479end
1480
1481function Parser:_fish_complete_help(buf, basename)
1482 local prefix = "complete -c " .. basename
1445 table.insert(buf, "") 1483 table.insert(buf, "")
1446 1484
1447 for _, command in ipairs(self._commands) do 1485 for _, command in ipairs(self._commands) do
1448 for _, alias in ipairs(command._aliases) do 1486 local aliases = table.concat(command._aliases, " ")
1449 local line = ("%s -n '__fish_use_subcommand' -xa '%s'"):format(prefix, alias) 1487 local line
1450 if command._description then 1488 if self._parent then
1451 line = ("%s -d '%s'"):format(line, fish_escape(get_short_description(command))) 1489 line = ("%s -n '__fish_%s_using_command %s' -xa '%s'")
1452 end 1490 :format(prefix, basename, self:_get_fullname(true), aliases)
1453 table.insert(buf, line) 1491 else
1492 line = ("%s -n '__fish_%s_using_command' -xa '%s'"):format(prefix, basename, aliases)
1454 end 1493 end
1455 1494 if command._description then
1495 line = ("%s -d '%s'"):format(line, fish_escape(get_short_description(command)))
1496 end
1497 table.insert(buf, line)
1456 end 1498 end
1457 1499
1458 if self._is_help_command then 1500 if self._is_help_command then
1459 local line = ("%s -n '__fish_seen_subcommand_from %s' -xa '%s'") 1501 local line = ("%s -n '__fish_%s_using_command %s' -xa '%s'")
1460 :format(prefix, table.concat(self._aliases, " "), self._parent:_get_commands()) 1502 :format(prefix, basename, self:_get_fullname(true), self._parent:_get_commands())
1461 table.insert(buf, line) 1503 table.insert(buf, line)
1462 end 1504 end
1463 1505
@@ -1465,7 +1507,7 @@ function Parser:_fish_complete_help(buf, prefix)
1465 local parts = {prefix} 1507 local parts = {prefix}
1466 1508
1467 if self._parent then 1509 if self._parent then
1468 table.insert(parts, "-n '__fish_seen_subcommand_from " .. table.concat(self._aliases, " ") .. "'") 1510 table.insert(parts, "-n '__fish_" .. basename .. "_seen_command " .. self:_get_fullname(true) .. "'")
1469 end 1511 end
1470 1512
1471 for _, alias in ipairs(option._aliases) do 1513 for _, alias in ipairs(option._aliases) do
@@ -1490,7 +1532,7 @@ function Parser:_fish_complete_help(buf, prefix)
1490 end 1532 end
1491 1533
1492 for _, command in ipairs(self._commands) do 1534 for _, command in ipairs(self._commands) do
1493 command:_fish_complete_help(buf, prefix) 1535 command:_fish_complete_help(buf, basename)
1494 end 1536 end
1495end 1537end
1496 1538
@@ -1498,8 +1540,31 @@ function Parser:get_fish_complete()
1498 self._basename = base_name(self._name) 1540 self._basename = base_name(self._name)
1499 assert(self:_is_shell_safe()) 1541 assert(self:_is_shell_safe())
1500 local buf = {} 1542 local buf = {}
1501 local prefix = "complete -c " .. self._basename 1543
1502 self:_fish_complete_help(buf, prefix) 1544 if #self._commands > 0 then
1545 table.insert(buf, ([[
1546function __fish_%s_print_command
1547 set -l cmdline (commandline -poc)
1548 set -l cmd]]):format(self._basename))
1549 self:_fish_get_cmd(buf, 4)
1550 table.insert(buf, ([[
1551 echo "$cmd"
1552end
1553
1554function __fish_%s_using_command
1555 test (__fish_%s_print_command) = "$argv"
1556 and return 0
1557 or return 1
1558end
1559
1560function __fish_%s_seen_command
1561 string match -q "$argv*" (__fish_%s_print_command)
1562 and return 0
1563 or return 1
1564end]]):format(self._basename, self._basename, self._basename, self._basename))
1565 end
1566
1567 self:_fish_complete_help(buf, self._basename)
1503 return table.concat(buf, "\n") .. "\n" 1568 return table.concat(buf, "\n") .. "\n"
1504end 1569end
1505 1570
@@ -2004,7 +2069,7 @@ end
2004 2069
2005local argparse = {} 2070local argparse = {}
2006 2071
2007argparse.version = "0.6.0" 2072argparse.version = "0.7.0"
2008 2073
2009setmetatable(argparse, {__call = function(_, ...) 2074setmetatable(argparse, {__call = function(_, ...)
2010 return Parser(default_cmdline[0]):add_help(true)(...) 2075 return Parser(default_cmdline[0]):add_help(true)(...)