gpt4 book ai didi

Scala scopt : argument required() based on one or more other arguments

转载 作者:行者123 更新时间:2023-12-04 07:40:39 33 4
gpt4 key购买 nike

我习惯使用Scala scopt用于命令行选项解析。您可以选择参数是否为 .required()通过调用刚刚显示的函数。
如何定义仅在定义了另一个参数时才需要的参数?
例如,我有一个标志 --writeToSnowflake通过 scopt 定义如下:

opt[Unit]("writeToSnowflake")
.action((_, config) => config.copy(writeToSnowflake = true))
如果我选择将作业写入雪花,则一组其他参数将成为强制性的。例如, --snowflake_table , --snowflake_database , 等等。
我怎样才能实现它?

最佳答案

我发现 .children()可以在 .cmd() 之外使用s 来实现我的要求。这是一个例子:
如果指定了父级,在这种情况下,如果 --snowflake 被“通过”,因此计算结果为 True ,然后是 .required() 的 child 如果它们是 null 将抛出错误(但仅当指定了父项时,就像我的情况一样)。

opt[Unit]("snowflake")
.action((_, config) => config.copy(writeToSnowflake = true))
.text("optional flag for writing to Snowflake")
.children(
opt[Unit]("snowflake_incremental_writing")
.action((_, config) => config.copy(snowflakeIncrementalWriting = true))
.text("optional flag for enabling incremental writing"),
opt[Map[String, String]]("snowflake_options")
.required()
.action((snowflakeOptions, config) => config.copy(snowflakeOptions = snowflakeOptions))
.text("options for writing to snowflake: user, privateKey, warehouse, database, schema, and table")
)

关于Scala scopt : argument required() based on one or more other arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67489830/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com