mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Make it possible to pass command line options through to scripts.
The feature is documented with the zeek_script_args variable in init-bare.zeek.
This commit is contained in:
parent
6902b645ba
commit
2bdc56dfcd
5 changed files with 69 additions and 2 deletions
|
@ -5,6 +5,7 @@
|
|||
#include "Options.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -186,8 +187,48 @@ Options parse_cmdline(int argc, char** argv)
|
|||
}
|
||||
else
|
||||
{
|
||||
for ( auto i = 0; i < argc; ++i )
|
||||
zeek_args.emplace_back(argv[i]);
|
||||
if ( argc > 1 )
|
||||
{
|
||||
auto endsWith = [](const std::string& str, const std::string& suffix)
|
||||
{
|
||||
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
|
||||
};
|
||||
|
||||
auto i = 0;
|
||||
for ( ; i < argc && ! endsWith(argv[i], "--"); ++i )
|
||||
{
|
||||
zeek_args.emplace_back(argv[i]);
|
||||
}
|
||||
|
||||
// If a script is invoked with Zeek as the interpreter, the arguments provided
|
||||
// directly in the interpreter line of the script won't be broken apart in the
|
||||
// argv on Linux so we split it up here.
|
||||
if ( endsWith(argv[i], "--") && zeek_args.size() == 1 )
|
||||
{
|
||||
std::istringstream iss(argv[i]);
|
||||
for ( std::string s; iss >> s; )
|
||||
{
|
||||
if ( ! endsWith(s, "--") )
|
||||
{
|
||||
zeek_args.emplace_back(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( i < argc )
|
||||
{
|
||||
// There is an additional increment here to skip over the "--" if it was found.
|
||||
if ( endsWith(argv[i], "--") )
|
||||
++i;
|
||||
|
||||
// The first argument after the double hyphens in implicitly a script name.
|
||||
rval.scripts_to_load.emplace_back(argv[i++]);
|
||||
|
||||
// If there are more argument, grab them for script arguments
|
||||
for ( ; i < argc; ++i )
|
||||
rval.script_args.emplace_back(argv[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
constexpr struct option long_opts[] = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue