【问题标题】:Java read command line parameters and allow pipingJava读取命令行参数并允许管道
【发布时间】:2016-10-25 14:52:43
【问题描述】:

1.我想知道如何在控制台中获取我的程序执行时给出的参数,并对其进行一些操作。所以例如我执行命令myProg -i someFile -o someOutput -v where :

  • -i [] 是我阅读的输入文件
  • -o [] 是我写入的文件位置
  • -v 设置详细模式以用于调试目的

2. 我还想做的是在我的程序中允许管道。因此,例如当我调用someProg | myProg > result.whatever 时,它将获取someProg 的结果,处理它,并将其写入result.whatever

【问题讨论】:

  • 如果这不是生产产品,那么以下答案非常好。但是,我会使用 Apache CLI 作为参数的解析器和处理程序。
  • @DejaVuSansMono 好吧,如果你有时间浪费,请告诉我们如何去做!
  • 好像有人抢先了我。这应该为您想要的东西提供基础。 github.com/Macilias/Utils/blob/master/ShellUtils.java

标签: java parameter-passing pipeline


【解决方案1】:

@1:很简单,下面是一个sn-p,告诉你怎么做

public static void main(String[] args) {
    // variables we will need
    String fileIn = "";
    String fileOut = "";
    int i = 0;

    for(String s : args) {
        if (s.equals("-v")) { // we check for if it is equal to -v
            // depends on your logger
            // for example for simpleLogger and slf4j
            System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "debug");
        } else if(s.equals("-i") && !args[i+1].equals("-o") && args[i+1] != null) { // in an else if to have no input = -v
            fileIn = args[i+1];
        } else if(s.equals("-o") && args[i+1] != null) { // in an else if to have no input = -v or -i
            fileOut = args[i+1];
        } else {
            System.err.println(s + "is an invalid parameter");
            System.exit(-1);
        }
    }

    // then you can do whatever you want with your input and output

}

@2:你可以这样做,但是如果用户只是在没有参数或管道myprog的情况下调用你的程序,你可能会遇到一些问题,这就是你必须超时的原因。

private Document doc;
public void loadFile(String fileLocation) { 
    final Thread parent = Thread.currentThread(); // our current thread
    Thread timeout = new Thread() { // to have a timeout
        public void run() {
            try {
            sleep(5000); //waits for 5 seconds
            parent.interrupt();

            //say user that no input has been given or found
            System.exit(-1); // exit the program
            } catch (InterruptedException e) {
                //Timeout was interrupted
            }
        }
    };
    timeout.start(); // starts the timeout thingie that will wait for 5 seconds and shut down the main thread.
    if(fileLocation.equals("")) { // in case no input, so System.in
        File f = null;
        try {
            f = java.io.File.createTempFile("buf", ".whatever", null);
        } catch (IOException e) {
            e2.printStackTrace();
            System.exit(-1);
        }

        PrintWriter writer = null;
        try {
            writer = new PrintWriter(f);
        } catch (FileNotFoundException e) {
            e1.printStackTrace();
            System.exit(-1);
        }

        Scanner sc = new Scanner(System.in);

        while (sc.hasNextLine()) { // we have to do the thread thing because of this line
// hasNextLine() always return true for System.in since it could have a next line
            String b = sc.nextLine();
            writer.println(b);
        }

        timeout.interrupt(); // since it worked fine we shut timeout down
        sc.close();
        writer.close();

        // now you can have your document in f and handle it
        // do things with f

        // don't forget
        f.delete();
    } else {
        // load the file from the path
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2020-01-16
    • 2019-12-31
    相关资源
    最近更新 更多