2

Beanshell文档暗示您可以在命令行上使用这种格式运行脚本:

java bsh.Interpreter script.bsh [args]

唯一的问题是我无法让它工作。我知道如何使用 Beanshell 脚本中的 args 调用其他脚本,但我无法获取初始脚本来获取 args。帮助?

例如,像这样的 beanshell 脚本,不会解析 args:

import java.util.*;
for (int i=0; i < args.length; i++) {
  System.out.println("Arg: " + args[i]);
}

此外,这也不起作用:

import bsh.Interpreter;
for( i : bsh.args )
System.out.println( i );
4

2 回答 2

3

命令行参数在bsh.args而不是下可用args。因此,如果您使用 更改args代码中的所有实例bsh.args,您应该一切顺利。参考:特殊变量和值


这对我有用:

for (arg : bsh.args)
    print(arg);

例子:

$ bsh foo.bsh 1 2 3
1
2
3
于 2011-09-09T03:58:51.450 回答
0

感谢Chris Jester-Young,我为此使用 Beanshell 编写了一个解决方案:

import java.util.*;
//debug();
argsList  = new ArrayList();
optsList = new HashMap();
specialOpts = new ArrayList();
int count = 0; // count the number of program args
for (int i=0; i < bsh.args.length ; i++) {
  switch (bsh.args[i].charAt(0)) {
    case '-':
        if (bsh.args[i].charAt(1) == '-') {
          int len = 0;
          String argstring = bsh.args[i].toString();
          len = argstring.length();
          System.out.println("Add special option " + 
                              argstring.substring(2, len) );
          specialOpts.add(argstring.substring(2, len));
      } else if (bsh.args[i].charAt(1) != '-' && bsh.args[i].length() > 2 ) {
            System.out.println("Found extended option: " + bsh.args[i] +
                                " with parameter " + bsh.args[i+1] );
          optsList.put(bsh.args[i], bsh.args[i+1]);
          i= i+1;
      } else if (bsh.args[i].charAt(1) != '-' && bsh.args[i].length() == 2 ) {
          System.out.println("Found regular option: " + bsh.args[i].charAt(1) + 
                             " with value " + bsh.args[i+1] );
          optsList.put(bsh.args[i], bsh.args[i+1]);
          i= i+1;
      } else if (bsh.args[i].length() <= 1) {
            System.out.println("Improperly formed arg found: " + bsh.args[i] );
        }
    break;
    default:
      System.out.println("Add arg to argument list: " + bsh.args[i] );
      argsList.add(bsh.args[i]);
    break;
  }
}
于 2011-09-09T05:29:15.160 回答