python: Update -c to work like normal python

In python, when you use -c it consumes all subsequent parameters and
appends them to argv. Now, gem5 and python behave the same with -c.

Python:
> python -c "import sys; print(sys.argv)" --hello -j
['-c', '--hello', '-j']

gem5:
> gem5.opt -c "import sys; print(sys.argv)" --hello -j
gem5 Simulator System.  https://www.gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version [DEVELOP-FOR-22.1]
gem5 compiled Oct 17 2022 15:47:46
gem5 started Oct 17 2022 15:53:45
gem5 executing on challenger, pid 4021103
command line: build/ALL/gem5.opt -c 'import sys; print(sys.argv)' --hello -j

['-c', '--hello', '-j']

Change-Id: I53e87712be9523e0583149235c9787c92618f884
Signed-off-by: Jason Lowe-Power <jason@lowepower.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/63151
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
This commit is contained in:
Jason Lowe-Power
2022-09-02 12:02:33 -07:00
committed by Jason Lowe-Power
parent 32e7ce3f19
commit b8e6e3aa43

View File

@@ -176,6 +176,23 @@ def parse_options():
"-v", "--verbose", action="count", default=0, help="Increase verbosity"
)
# To make gem5 mimic python better. After `-c` we should consume all other
# arguments and add those to argv.
def collect_args(option, opt_str, value, parser):
extra_args = parser.rargs[:]
del parser.rargs[:]
setattr(parser.values, option.dest, (value, extra_args))
option(
"-c",
type=str,
help="program passed in as string (terminates option list)",
default="",
metavar="cmd",
action="callback",
callback=collect_args,
)
# Statistics options
group("Statistics Options")
option(
@@ -281,14 +298,6 @@ def parse_options():
help="List all built-in SimObjects, their params and default values",
)
option(
"-c",
type=str,
help="program passed in as string (terminates option list)",
default="",
metavar="cmd",
)
arguments = options.parse_args()
return options, arguments
@@ -556,8 +565,9 @@ def main():
sys.argv = arguments
if options.c:
filedata = options.c
filedata = options.c[0]
filecode = compile(filedata, "<string>", "exec")
sys.argv = ["-c"] + options.c[1]
scope = {"__name__": "__m5_main__"}
else:
sys.path = [os.path.dirname(sys.argv[0])] + sys.path