tianyun 2 anos atrás
pai
commit
6ea8a74515

+ 1 - 0
clap/README.md

@@ -0,0 +1 @@
+alias mycommand='java -cp "/path/to/picocli-4.6.1.jar:/path/to/myapp.jar" com.alvin.Main'

+ 46 - 0
clap/pom.xml

@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>java-base-maven</artifactId>
+        <groupId>com.alvin</groupId>
+        <version>1.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>clap</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>info.picocli</groupId>
+            <artifactId>picocli</artifactId>
+            <version>4.6.3</version>
+        </dependency>
+
+    </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>2.4.3</version>
+                <configuration>
+                    <!-- put your configurations here -->
+                </configuration>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 11 - 0
clap/src/main/java/com/alvin/CmdParam.java

@@ -0,0 +1,11 @@
+package com.alvin;
+
+import picocli.CommandLine;
+
+@CommandLine.Command(
+        name = "java -jar app.jar",
+        subcommands = {ExportCmd.class, ImportCmd.class},
+        mixinStandardHelpOptions = true,
+        helpCommand = true)
+public class CmdParam {
+}

+ 17 - 0
clap/src/main/java/com/alvin/ExportCmd.java

@@ -0,0 +1,17 @@
+package com.alvin;
+
+import picocli.CommandLine;
+
+@CommandLine.Command(name = "export", mixinStandardHelpOptions = true, helpCommand = true)
+    public class ExportCmd {
+        @CommandLine.Option(names = {"-r", "--jdbc-url"}, required = true, description = "数据库地址")
+        public String jdbcUrl;
+        @CommandLine.Option(names = {"-u", "--username"}, required = true, description = "用户名")
+        public String username;
+        @CommandLine.Option(names = {"-p", "--secret-key"}, required = true, description = "秘钥")
+        public String secretKey;
+        @CommandLine.Option(names = {"-d", "--database"}, defaultValue = "default", description = "库名,默认为default")
+        public String database;
+        @CommandLine.Option(names = {"-t", "--table"}, description = "表名,多个表用逗号分开")
+        public String tables;
+    }

+ 17 - 0
clap/src/main/java/com/alvin/ImportCmd.java

@@ -0,0 +1,17 @@
+package com.alvin;
+
+import picocli.CommandLine;
+
+@CommandLine.Command(name = "import", mixinStandardHelpOptions = true, helpCommand = true)
+    public class ImportCmd {
+        @CommandLine.Option(names = {"-r", "--jdbc-url"}, required = true, description = "数据库地址")
+        public String jdbcUrl;
+        @CommandLine.Option(names = {"-u", "--username"}, required = true, description = "用户名")
+        public String username;
+        @CommandLine.Option(names = {"-p", "--secret-key"}, required = true, description = "秘钥")
+        public String secretKey;
+        @CommandLine.Option(names = {"-f", "--from"}, required = true, description = "来源")
+        public String from;
+        @CommandLine.Option(names = {"-t", "--to"}, required = true, description = "目标")
+        public String to;
+    }

+ 40 - 0
clap/src/main/java/com/alvin/Main.java

@@ -0,0 +1,40 @@
+package com.alvin;
+
+import picocli.CommandLine;
+
+public class Main {
+    public static void main(String[] args) {
+        // 解析参数
+        final CmdParam cmdParam = new CmdParam();
+        final CommandLine commandLine = new CommandLine(cmdParam);
+        try {
+            final CommandLine.ParseResult parseResult = commandLine.parseArgs(args);
+            checkParamHelp(args.length == 0, commandLine, parseResult);
+            if (parseResult.hasSubcommand()) {
+                for (CommandLine.ParseResult subCommand : parseResult.subcommands()) {
+                    final CommandLine c = subCommand.commandSpec().commandLine();
+                    checkParamHelp(args.length == 1, c, subCommand);
+                }
+            }
+        } catch (CommandLine.ParameterException e) {
+            commandLine.usage(System.out);
+            for (CommandLine c : commandLine.getSubcommands().values()) {
+                c.usage(System.out);
+            }
+            System.exit(1);
+        }
+        System.out.println("cmd params: " + cmdParam);
+    }
+
+    private static void checkParamHelp(boolean empty, CommandLine commandLine, CommandLine.ParseResult parseResult) {
+        if (empty || parseResult.isUsageHelpRequested()) {
+            commandLine.usage(System.out);
+            System.exit(0);
+        }
+        if (parseResult.isVersionHelpRequested()) {
+            commandLine.printVersionHelp(System.out);
+            System.exit(0);
+        }
+    }
+
+}

+ 1 - 0
pom.xml

@@ -12,6 +12,7 @@
         <module>test1</module>
         <module>springboot-main</module>
         <module>leetcode</module>
+        <module>clap</module>
     </modules>
 
     <properties>