Parsing arguments to a Java command line program

后端 未结 9 1040
孤街浪徒
孤街浪徒 2020-12-07 20:37

What if I wanted to parse this:

java MyProgram -r opt1 -S opt2 arg1 arg2 arg3 arg4 --test -A opt3

And the result I want in my program is:

9条回答
  •  没有蜡笔的小新
    2020-12-07 21:28

    Use the Apache Commons CLI library commandline.getArgs() to get arg1, arg2, arg3, and arg4. Here is some code:

    
    
        import org.apache.commons.cli.CommandLine;
        import org.apache.commons.cli.Option;
        import org.apache.commons.cli.Options;
        import org.apache.commons.cli.Option.Builder;
        import org.apache.commons.cli.CommandLineParser;
        import org.apache.commons.cli.DefaultParser;
        import org.apache.commons.cli.ParseException;
    
        public static void main(String[] parameters)
        {
            CommandLine commandLine;
            Option option_A = Option.builder("A")
                .required(true)
                .desc("The A option")
                .longOpt("opt3")
                .build();
            Option option_r = Option.builder("r")
                .required(true)
                .desc("The r option")
                .longOpt("opt1")
                .build();
            Option option_S = Option.builder("S")
                .required(true)
                .desc("The S option")
                .longOpt("opt2")
                .build();
            Option option_test = Option.builder()
                .required(true)
                .desc("The test option")
                .longOpt("test")
                .build();
            Options options = new Options();
            CommandLineParser parser = new DefaultParser();
    
            String[] testArgs =
            { "-r", "opt1", "-S", "opt2", "arg1", "arg2",
              "arg3", "arg4", "--test", "-A", "opt3", };
    
            options.addOption(option_A);
            options.addOption(option_r);
            options.addOption(option_S);
            options.addOption(option_test);
    
            try
            {
                commandLine = parser.parse(options, testArgs);
    
                if (commandLine.hasOption("A"))
                {
                    System.out.print("Option A is present.  The value is: ");
                    System.out.println(commandLine.getOptionValue("A"));
                }
    
                if (commandLine.hasOption("r"))
                {
                    System.out.print("Option r is present.  The value is: ");
                    System.out.println(commandLine.getOptionValue("r"));
                }
    
                if (commandLine.hasOption("S"))
                {
                    System.out.print("Option S is present.  The value is: ");
                    System.out.println(commandLine.getOptionValue("S"));
                }
    
                if (commandLine.hasOption("test"))
                {
                    System.out.println("Option test is present.  This is a flag option.");
                }
    
                {
                    String[] remainder = commandLine.getArgs();
                    System.out.print("Remaining arguments: ");
                    for (String argument : remainder)
                    {
                        System.out.print(argument);
                        System.out.print(" ");
                    }
    
                    System.out.println();
                }
    
            }
            catch (ParseException exception)
            {
                System.out.print("Parse error: ");
                System.out.println(exception.getMessage());
            }
        }
    
    

提交回复
热议问题