Shell: Add support for ARGV (and $*, $#)

This patchset also adds the 'shift' builtin, as well as the usual tests.
closes #2948.
This commit is contained in:
AnotherTest 2020-08-04 09:27:25 +04:30 committed by Andreas Kling
parent 192b2383ac
commit 12af65c1c9
Notes: sideshowbarker 2024-07-19 04:20:03 +09:00
8 changed files with 79 additions and 1 deletions

View file

@ -159,12 +159,14 @@ int main(int argc, char** argv)
const char* command_to_run = nullptr;
const char* file_to_read_from = nullptr;
Vector<const char*> script_args;
bool skip_rc_files = false;
Core::ArgsParser parser;
parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string");
parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
parser.add_option(skip_rc_files, "Skip running shellrc files", "skip-shellrc", 0);
parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
parser.add_positional_argument(script_args, "Extra argumets to pass to the script (via $* and co)", "argument", Core::ArgsParser::Required::No);
parser.parse(argc, argv);
@ -181,6 +183,13 @@ int main(int argc, char** argv)
run_rc_file(Shell::local_init_file_path);
}
{
Vector<String> args;
for (auto* arg : script_args)
args.empend(arg);
shell->set_local_variable("ARGV", *new AST::ListValue(move(args)));
}
if (command_to_run) {
dbgprintf("sh -c '%s'\n", command_to_run);
shell->run_command(command_to_run);