Java 【命令行接收参数】

commons-cli

依赖:

1      <!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
2         <dependency>
3             <groupId>commons-cli</groupId>
4             <artifactId>commons-cli</artifactId>
5             <version>1.2</version>
6         </dependency>

代码

复制代码
 1 public static void main(String[] args) {
 2         args = new String[]{"-t", "command_test", "-f", "/User/xxx/command.txt", "-b", "-s50", "-Dkey1=value1", "-Dkey2=value2" };
 3 
 4         try {
 5             // create Options object
 6             Options options = new Options();
 7             options.addOption(new Option("t", "text", true, "use given information(String)"));
 8             options.addOption(new Option("b", false, "display current time(boolean)"));
 9             options.addOption(new Option("s", "size", true, "use given size(Integer)"));
10             options.addOption(new Option("f", "file", true, "use given file(File)"));
11 
12             @SuppressWarnings("static-access")
13             Option property = OptionBuilder.withArgName("property=value")
14                     .hasArgs(2)
15                     .withValueSeparator()
16                     .withDescription("use value for given property(property=value)")
17                     .create("D");
18             property.setRequired(true);
19             options.addOption(property);
20 
21             // print usage
22             HelpFormatter formatter = new HelpFormatter();
23             formatter.printHelp( "AntOptsCommonsCLI", options );
24             System.out.println();
25 
26             // create the command line parser
27             CommandLineParser parser = new PosixParser();
28             CommandLine cmd = parser.parse(options, args);
29 
30             // check the options have been set correctly
31             System.out.println(cmd.getOptionValue("t"));
32             System.out.println(cmd.getOptionValue("f"));
33             if (cmd.hasOption("b")) {
34                 System.out.println(new Date());
35             }
36             System.out.println(cmd.getOptionValue( "s" ));
37             System.out.println(cmd.getOptionProperties("D").getProperty("key1") );
38             System.out.println(cmd.getOptionProperties("D").getProperty("key2") );
39         } catch (Exception ex) {
40             System.out.println( "Unexpected exception:" + ex.getMessage() );
41         }
42     }
复制代码

 

jcommander

依赖

1       <!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
2         <dependency>
3             <groupId>com.beust</groupId>
4             <artifactId>jcommander</artifactId>
5             <version>1.78</version>
6         </dependency>

代码

复制代码
public class JCommanderBean {

    @Parameter
    public List<String> parameters = Lists.newArrayList();

    @Parameter(names = {"-log", "-verbose"}, description = "Level of verbosity")
    public Integer verbose = 1;

    @Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
    public String groups;

    @Parameter(names = "-debug", description = "Debug mode")
    public boolean debug = false;

    @DynamicParameter(names = "-D", description = "Dynamic parameters go here")
    public Map<String, String> dynamicParams = new HashMap<String, String>();
}
复制代码
复制代码
 1 public static void main(String[] args) {
 2 
 3         String[] argv = {"-log", "2", "-groups", "unit1,unit2,unit3",
 4                 "-debug", "-Doption=value", "a", "b", "c"};
 5         JCommanderBean jct = new JCommanderBean();
 6 
 7         JCommander.newBuilder()
 8                 .addObject(jct)
 9                 .build()
10                 .parse(argv);
11 
12         Assert.assertEquals(2, jct.verbose.intValue());
13         Assert.assertEquals("unit1,unit2,unit3", jct.groups);
14         Assert.assertTrue(jct.debug);
15         Assert.assertEquals("value", jct.dynamicParams.get("option"));
16         Assert.assertEquals(Arrays.asList("a", "b", "c"), jct.parameters);
17     }
复制代码

 

posted @   为你编程  阅读(794)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示