微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 picocli 从属性中读取值并使用它连接 postgres

如何解决如何使用 picocli 从属性中读取值并使用它连接 postgres

我使用 picocli 并且我想构建 cli 从数据库获取数据并将其发送到另一个服务。所以我配置看起来像

object Student : Table("student") {
    val id = integer("student_id").autoIncrement().primaryKey()
    val name = varchar("name",50)
    val grade = integer("grade")
}

class App() : Callable<Integer> {
    @CommandLine.Option(names = ["-ho","--host"],description = arrayOf("Host database"),defaultValue = "localhost")
    var host: String? = null

    @CommandLine.Option(names = ["-p","--port"],description = arrayOf("port database"),defaultValue = "5432")
    var port: String? = null

    @CommandLine.Option(names = ["-d","--database"],description = arrayOf("database"),defaultValue = "postgres")
    var database: String? = null

    @CommandLine.Option(names = ["-u","--username"],description = arrayOf("username of database"),defaultValue = "postgres")
    var username: String? = null

    @CommandLine.Option(names = ["-w","--password"],description = arrayOf("password off database"),defaultValue = "password")
    var password: String? = null

    override fun call(): Integer {
       connectDB()
       createStudent()
       return Integer(0)
    }

    fun createStudent() {
        println("Begin create student")
        transaction {
            create(Student)
        }
        println("End create student")
    }

    fun connectDB() {
        try {
            Database.connect("jdbc:postgresql://${this.host}:${this.port}/${this.database}","org.postgresql.Driver",this.username.toString(),this.password.toString())
            print("Connect db")
        } catch (e : Exception) {
            println("Connect db fail with error ${e.message}")
        }
    }

}

fun main(args: Array<String>) {
    val existCode = CommandLine(App()).execute(*args)
    System.exit(existCode);
}

当我执行它时连接成功。但我不想从 args 解析值。我想从我的文件属性示例中读取:db.properties 或 db.yml。怎么办呢?我搜索 doccument picocli 但我找不到任何东西。所以现在我使用 org.jetbrains.exposed:exposed crud 到我的 postgres.It 与 picoli 一起工作?如果从数据库获取数据并将其发送到另一个 api 时有什么建议框架吗?非常感谢你

解决方法

您提到的要求是一种常见的 CLI 设计模式:用户可以在命令行或配置文件(或两者,在这种情况下命令行覆盖配置文件)中指定值。

在 picocli 中,实现这一点的最简单方法是使用 default provider:如果未在命令行中指定该值,则从该默认提供程序获取该值。

您的默认提供程序实现可以从属性文件或 yaml 文件中读取,这完全取决于您。

例如:

@Command(name = "db",defaultValueProvider = MyDefaultProvider.class)
class App() : Callable<Integer> {
   // ...
}

class MyDefaultProvider implements picocli.CommandLine.IDefaultValueProvider {
    public String defaultValue(ArgSpec argSpec) throws Exception {
        // read db.properties (or db.yml or something) from some location
        Properties defaultValues = readProperties();

        // return the default value for the argument
        if (argSpec.isOption()) {
            OptionSpec option = (OptionSpec) argSpec;
            return defaultValues.getProperty(option.longestName());
        }
    }

    private Properties readProperties() throws IOException {
        //...
    }
}

Picocli 还提供了一个内置的 PropertiesDefaultProvider 实现,它会在用户主目录中查找名为 .${COMMAND-NAME}.properties 的文件,其中 ${COMMAND-NAME} 是命令的名称。 (在您的示例中,App 类没有 @Command(name = "xxx") 注释,如果您选择使用内置 PropertiesDefaultProvider,您可能需要添加此注释。)

,

我有一个问题。现在我可以使用默认连接到数据库,但我必须命令:getStudent 和 showStudent。我如何使用配置 connectDb 应用于命令

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。