Skip to content

Commit

Permalink
swing
Browse files Browse the repository at this point in the history
  • Loading branch information
abathur committed Sep 20, 2023
1 parent adec64d commit b6d7e36
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 49 deletions.
84 changes: 43 additions & 41 deletions resholve
Original file line number Diff line number Diff line change
Expand Up @@ -1395,10 +1395,8 @@ class CommandParser(theirparse.ArgumentParser):
- version, variant, description of how they differ, platform
"""

def __init__(self, name, variant="generic"): # , **kwargs):
theirparse.ArgumentParser.__init__(
self, prog="{:} ({:})".format(name, variant), add_help=False # , **kwargs
)
def __init__(self, **kwargs):
theirparse.ArgumentParser.__init__(self, **kwargs)
self.register("action", "invocations", InvocationsAction)
# self.register("action", "invocation", InvocationAction)

Expand Down Expand Up @@ -1428,6 +1426,10 @@ class CommandParser(theirparse.ArgumentParser):
return ret


def make_command_parser(name, variant="generic", cls=CommandParser):
return cls(prog="{:} ({:})".format(name, variant), add_help=False)


class FindParser(CommandParser):
"""
find's exec options (-exec -execdir -ok -okdir)
Expand Down Expand Up @@ -1544,7 +1546,7 @@ TODO:
class BuiltinCommandParsers(object):
@staticmethod
def _alias():
generic = CommandParser("alias", "builtin")
generic = make_command_parser("alias", "builtin")
generic.add_argument("-p", action="store_true")
generic.add_argument(
"commands", nargs=theirparse.REMAINDER, action="invocations"
Expand All @@ -1553,23 +1555,23 @@ class BuiltinCommandParsers(object):

@staticmethod
def _builtin():
generic = CommandParser("builtin", "builtin")
generic = make_command_parser("builtin", "builtin")
generic.add_argument(
"commands", nargs=theirparse.REMAINDER, action="invocations"
)
return (generic,)

@staticmethod
def _coproc():
generic = CommandParser("coproc", "builtin")
generic = make_command_parser("coproc", "builtin")
generic.add_argument(
"commands", nargs=theirparse.REMAINDER, action="invocations"
)
return (generic,)

@staticmethod
def _command():
generic = CommandParser("command", "builtin")
generic = make_command_parser("command", "builtin")
generic.add_argument("-p", action="store_true")
generic.add_argument("-v", action="store_true")
generic.add_argument("-V", action="store_true")
Expand All @@ -1580,7 +1582,7 @@ class BuiltinCommandParsers(object):

@staticmethod
def _eval():
generic = CommandParser("eval", "builtin")
generic = make_command_parser("eval", "builtin")
generic.add_argument(
"commands", nargs=theirparse.REMAINDER, action="invocations"
)
Expand All @@ -1601,7 +1603,7 @@ class BuiltinCommandParsers(object):
-c execute COMMAND with an empty environment
-l place a dash in the zeroth argument to COMMAND
"""
generic = CommandParser("exec", "builtin")
generic = make_command_parser("exec", "builtin")
generic.add_argument("-a")
generic.add_argument("-c", action="store_true")
generic.add_argument("-l", action="store_true")
Expand All @@ -1612,15 +1614,15 @@ class BuiltinCommandParsers(object):

@staticmethod
def _source():
generic = CommandParser("source", "builtin")
generic = make_command_parser("source", "builtin")
generic.add_argument(
"commands", nargs=theirparse.REMAINDER, action="invocations"
)
return (generic,)

@staticmethod
def _type():
generic = CommandParser("type", "builtin")
generic = make_command_parser("type", "builtin")
generic.add_argument("-p", action="store_true")
generic.add_argument("-v", action="store_true")
generic.add_argument("-V", action="store_true")
Expand All @@ -1646,7 +1648,7 @@ class ExternalCommandParsers(object):
--help display this help and exit
--version output version information and exit
"""
generic = CommandParser("chroot")
generic = make_command_parser("chroot")
# bsd
generic.add_argument("-g")
generic.add_argument("-G")
Expand Down Expand Up @@ -1676,7 +1678,7 @@ class ExternalCommandParsers(object):
assignment-alikes should be stripped out already
"""
generic = CommandParser("env")
generic = make_command_parser("env")
generic.add_argument("-P") # bsd altpath
# gnu
generic.add_argument(
Expand Down Expand Up @@ -1705,7 +1707,7 @@ class ExternalCommandParsers(object):
"""
coreutils src/install.c:execlp
"""
generic = CommandParser("install")
generic = make_command_parser("install")
generic.add_argument(
"--strip-program",
dest="commands",
Expand All @@ -1720,7 +1722,7 @@ class ExternalCommandParsers(object):
coreutils src/nice.c:execvp
Usage: %s [OPTION] [COMMAND [ARG]...]
"""
generic = CommandParser("nice")
generic = make_command_parser("nice")
generic.add_argument("-n")
generic.add_argument("--adjustment")
generic.add_argument(
Expand All @@ -1734,7 +1736,7 @@ class ExternalCommandParsers(object):
coreutils src/nohup.c:execvp
Usage: %s [OPTION] [COMMAND [ARG]...]
"""
generic = CommandParser("nohup")
generic = make_command_parser("nohup")
generic.add_argument(
"commands", nargs=theirparse.REMAINDER, action="invocations"
)
Expand All @@ -1749,7 +1751,7 @@ class ExternalCommandParsers(object):
two mutually-exclusive forms; if opts are present, the command
is the first arg--if not, there's a combined context first.
"""
opt_context = CommandParser("runcon", "opt context")
opt_context = make_command_parser("runcon", "opt context")
context = opt_context.add_mutually_exclusive_group(required=True)
context.add_argument("-c", "--compute", action="store_true")
context.add_argument("-t", "--type")
Expand All @@ -1761,7 +1763,7 @@ class ExternalCommandParsers(object):
)

# these two forms are mutua
combined_context = CommandParser("runcon", "combined context")
combined_context = make_command_parser("runcon", "combined context")
combined_context.add_argument("context")
combined_context.add_argument(
"commands", nargs=theirparse.REMAINDER, action="invocations"
Expand All @@ -1773,7 +1775,7 @@ class ExternalCommandParsers(object):
"""
coreutils src/sort.c:execlp
"""
generic = CommandParser("sort")
generic = make_command_parser("sort")
generic.add_argument(
"--compress-program",
dest="commands",
Expand All @@ -1788,7 +1790,7 @@ class ExternalCommandParsers(object):
"""
coreutils src/split.c:execl
"""
generic = CommandParser("split")
generic = make_command_parser("split")
generic.add_argument(
"--filter",
dest="commands",
Expand All @@ -1804,7 +1806,7 @@ class ExternalCommandParsers(object):
coreutils src/stdbuf.c:?
Usage: %s [OPTION] [COMMAND [ARG]...]
"""
generic = CommandParser("stdbuf")
generic = make_command_parser("stdbuf")
generic.add_argument("-i", "--input")
generic.add_argument("-o", "--output")
generic.add_argument("-e", "--error")
Expand All @@ -1819,7 +1821,7 @@ class ExternalCommandParsers(object):
coreutils src/timeout.c:execvp
Usage: %s [OPTION] [COMMAND [ARG]...]
"""
generic = CommandParser("timeout")
generic = make_command_parser("timeout")
generic.add_argument("-k", "--kill-after")
generic.add_argument("-s", "--signal")
generic.add_argument("-v", "--verbose", action="store_true")
Expand All @@ -1834,7 +1836,7 @@ class ExternalCommandParsers(object):
@staticmethod
def _sqlite3():
""" """
generic = CommandParser("sqlite3")
generic = make_command_parser("sqlite3")
generic.add_argument(
"-cmd",
dest="commands",
Expand All @@ -1846,7 +1848,7 @@ class ExternalCommandParsers(object):

@staticmethod
def _script():
linux = CommandParser("script", "linux")
linux = make_command_parser("script", "linux")
linux.add_argument("-a", "--append", action="store_true")
linux.add_argument("-E", "--echo")
linux.add_argument("-e", "--return", action="store_true")
Expand All @@ -1871,7 +1873,7 @@ class ExternalCommandParsers(object):
)
linux.add_argument("file", nargs="?")

bsd = CommandParser("script", "bsd")
bsd = make_command_parser("script", "bsd")
bsd.add_argument("-d", action="store_true")
bsd.add_argument("-k", action="store_true")
bsd.add_argument("-p", action="store_true")
Expand All @@ -1896,7 +1898,7 @@ class ExternalCommandParsers(object):
requires the GnuSedParser hack-around, I'll
split them to be a little clearer...
"""
gnu = GnuSedParser("sed")
gnu = make_command_parser("sed", cls=GnuSedParser)
# very unsure about this group part
gscript = gnu.add_mutually_exclusive_group()
gnu.add_argument("-a", action="store_true") # BSD-only
Expand All @@ -1918,7 +1920,7 @@ class ExternalCommandParsers(object):
gnu.add_argument("--version", action="store_true")
gnu.add_argument("input_files", nargs="*")

bsd = CommandParser("sed")
bsd = make_command_parser("sed")
# very unsure about this group part
bscript = bsd.add_mutually_exclusive_group()
bsd.add_argument("-a", action="store_true") # BSD-only
Expand Down Expand Up @@ -1965,8 +1967,8 @@ class ExternalCommandParsers(object):
-V --version
"""
generic = CommandParser("awk")
assign_fallback = CommandParser("awk")
generic = make_command_parser("awk")
assign_fallback = make_command_parser("awk")

def base(parser):
# very unsure about this group part
Expand Down Expand Up @@ -2029,7 +2031,7 @@ class ExternalCommandParsers(object):
YES: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command]
YES: sudo [-AbEHknPS] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] [VAR=value] [-i|-s] [<command>]
"""
generic = CommandParser("sudo", "generic")
generic = make_command_parser("sudo", "generic")
# 1. weed out sudo forms that don't need resolving
# note: some of these long forms aren't known correct
# because sudo re-uses a lot of flags
Expand Down Expand Up @@ -2096,7 +2098,7 @@ class ExternalCommandParsers(object):
if this doesn't work, we'll probably need N parsers...
"""
generic = CommandParser("xargs")
generic = make_command_parser("xargs")
# gnu
generic.add_argument("-0", "--null", action="store_true")
generic.add_argument("-a", "--arg-file")
Expand Down Expand Up @@ -2145,7 +2147,7 @@ class ExternalCommandParsers(object):
if this doesn't work, we'll probably need N parsers...
"""
generic = CommandParser("rlwrap")
generic = make_command_parser("rlwrap")
generic.add_argument("-a", "--always-readline", nargs="?")
generic.add_argument("-A", "--ansi-colour-aware", action="store_true")
generic.add_argument("-b", "--break-chars")
Expand Down Expand Up @@ -2195,7 +2197,7 @@ class ExternalCommandParsers(object):
-ok command ;
-okdir command ;
"""
generic = FindParser("find")
generic = make_command_parser("find", cls=FindParser)
generic.add_argument("start")
# boolean predicates, not quite like normal flags...
# just capturing to ~ignore
Expand Down Expand Up @@ -2226,7 +2228,7 @@ class ExternalCommandParsers(object):

@staticmethod
def _generic_shell():
generic = CommandParser("generic_shell")
generic = make_command_parser("generic_shell")
generic.add_argument(
"-c",
# rest are nonstandard; leaving breadcrumbs;
Expand Down Expand Up @@ -2294,7 +2296,7 @@ class ExternalCommandParsers(object):
-I, --use-compress-program=COMMAND
Filter data through COMMAND. It must accept the -d option, for decompression. The argument can contain command line options.
"""
generic = CommandParser("generic tar")
generic = make_command_parser("generic tar")
"""
TODO: I'm technically missing one here, but it is a
bit harder than the rest.
Expand Down Expand Up @@ -2387,7 +2389,7 @@ class ExternalCommandParsers(object):
"""
We need to sub-search expressions for ! commands
"""
generic = CommandParser("dc")
generic = make_command_parser("dc")
generic.add_argument("-e", "--expression", action="append")
generic.add_argument("-f", "--file")
generic.add_argument("-V", "--version", action="store_true")
Expand All @@ -2400,7 +2402,7 @@ class ExternalCommandParsers(object):
"""
Based on Flatpak v1.14.4.
"""
generic = CommandParser("flatpak")
generic = make_command_parser("flatpak")

# flatpak --help
generic.add_argument(
Expand All @@ -2420,7 +2422,7 @@ class ExternalCommandParsers(object):
)
subparsers = generic.add_subparsers()
# flatpak update --help
update = subparsers.add_parser("update")
update = subparsers.add_parser("update", add_help=False)
update.add_argument(
"-h", "--help", "-u", "--user", "--system", action="store_true"
)
Expand Down Expand Up @@ -3890,8 +3892,8 @@ class RecordCommandlike(object):
return True

def handle_external_generic(self, parsed, invocation):
# if not hasattr(parsed, "commands"):
# return True
if not hasattr(parsed, "commands"):
return True

# TODO: shimming shells in here may be a bigger crime than
# duplicating the logic in two functions would be?
Expand Down
15 changes: 13 additions & 2 deletions tests/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ require() {

parsers() {
cat parse_*.sh > parsed.sh
resholve --interpreter none --path "${PKG_PARSED}:${PKG_COREUTILS}" < parsed.sh > resolved.sh
bash -xe resolved.sh
case "$(uname -s)" in
Linux)
cat linux_parse_*.sh >> parsed.sh
;;
Darwin)
: pass for now
;;
*)
: pass for now
;;
esac
resholve --interpreter none --path "${PKG_PARSED}:${PKG_COREUTILS}" parsed.sh
bash -xe parsed.sh.resolved
}
1 change: 1 addition & 0 deletions tests/linux_parse_flatpak.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flatpak update --help
6 changes: 0 additions & 6 deletions tests/parse_flatpak.sh

This file was deleted.

0 comments on commit b6d7e36

Please sign in to comment.