Пример #1
0
    public static String _c1(c1options x) {

      assertEquals(true, x.flag());
      assertEquals(33, x.a());
      assertEquals("bb", x.bb(), "bb");
      assertEquals(Arrays.asList(new File("f1.txt"), new File("f2.txt")), x.input());
      assertEquals(false, x.notset());
      assertEquals(Arrays.asList("-a", "--a", "a"), x._arguments());

      return "a";
    }
Пример #2
0
 private String doCommand(Object target, String method, String[] args) {
   if (target == null) ; // System.err.println("Huh? Target should never be null " +
   // domain);
   else {
     String cname = "_" + method.replaceAll("-", "_");
     try {
       Method m = target.getClass().getMethod(cname, new Class[] {String[].class});
       return (String) m.invoke(target, new Object[] {args});
     } catch (NoSuchMethodException e) {
       // Ignore
     } catch (InvocationTargetException e) {
       if (e.getCause() instanceof IllegalArgumentException) {
         domain.error(
             "%s, for cmd: %s, arguments; %s",
             e.getCause().getMessage(), method, Arrays.toString(args));
       } else {
         domain.warning("Exception in replace: %s", e.getCause());
         e.getCause().printStackTrace();
       }
     } catch (Exception e) {
       domain.warning("Exception in replace: " + e + " method=" + method);
       e.printStackTrace();
     }
   }
   return null;
 }
Пример #3
0
 public static Properties getParent(Properties p) {
   try {
     Field f = Properties.class.getDeclaredField("defaults");
     f.setAccessible(true);
     return (Properties) f.get(p);
   } catch (Exception e) {
     Field[] fields = Properties.class.getFields();
     System.err.println(Arrays.toString(fields));
     return null;
   }
 }
Пример #4
0
 /**
  * Get the contents of a file.
  *
  * @param in
  * @return
  * @throws IOException
  */
 public String _cat(String args[]) throws IOException {
   verifyCommand(args, "${cat;<in>}, get the content of a file", null, 2, 2);
   File f = domain.getFile(args[1]);
   if (f.isFile()) {
     return IO.collect(f);
   } else if (f.isDirectory()) {
     return Arrays.toString(f.list());
   } else {
     try {
       URL url = new URL(args[1]);
       return IO.collect(url, "UTF-8");
     } catch (MalformedURLException mfue) {
       // Ignore here
     }
     return null;
   }
 }
Пример #5
0
  /**
   * replace ; <list> ; regex ; replace
   *
   * @param args
   * @return
   */
  public String _replace(String args[]) {
    if (args.length != 4) {
      domain.warning("Invalid nr of arguments to replace " + Arrays.asList(args));
      return null;
    }

    String list[] = args[1].split("\\s*,\\s*");
    StringBuilder sb = new StringBuilder();
    String del = "";
    for (int i = 0; i < list.length; i++) {
      String element = list[i].trim();
      if (!element.equals("")) {
        sb.append(del);
        sb.append(element.replaceAll(args[2], args[3]));
        del = ", ";
      }
    }

    return sb.toString();
  }
Пример #6
0
  public String _tstamp(String args[]) {
    String format = "yyyyMMddHHmm";
    long now = System.currentTimeMillis();
    TimeZone tz = TimeZone.getTimeZone("UTC");

    if (args.length > 1) {
      format = args[1];
    }
    if (args.length > 2) {
      tz = TimeZone.getTimeZone(args[2]);
    }
    if (args.length > 3) {
      now = Long.parseLong(args[3]);
    }
    if (args.length > 4) {
      domain.warning("Too many arguments for tstamp: " + Arrays.toString(args));
    }

    SimpleDateFormat sdf = new SimpleDateFormat(format);
    sdf.setTimeZone(tz);

    return sdf.format(new Date(now));
  }
Пример #7
0
  public void testCommand() throws Exception {
    CommandLine getopt = new CommandLine(rp);
    assertEquals("[cmda, cmdb]", getopt.getCommands(new X()).keySet().toString());

    getopt.execute(new X(), "cmda", Arrays.asList("-e", "help"));
  }