Quellcode durchsuchen

add is_config and has_config apis

ruki vor 7 Jahren
Ursprung
Commit
985a057cfd
2 geänderte Dateien mit 193 neuen und 31 gelöschten Zeilen
  1. 91 11
      manual.md
  2. 102 20
      zh/manual.md

+ 91 - 11
manual.md

@@ -12,7 +12,7 @@ It's according to the following rules:
 
 | Interfaces            | Description                                                       |
 | --------------------- | ----------------------------------------------------------------- |
-| `is_` + xxx           | Condition interfaces                                              |
+| `is_`/`has_` + xxx    | Condition interfaces                                              |
 | `set_` + xxx          | Set and override the previous settings                            |
 | `add_` + xxx          | Set and append settings                                           |
 | `s` + xxx             | Support multi-parameters, .e.g:`add_files("*.c", "test.cpp")`    |
@@ -28,15 +28,17 @@ It's according to the following rules:
 
 Conditions are generally used to handle some special compilation platforms. 
 
-| Interfaces                | Description                               | Support version |
-| ------------------------- | ----------------------------------------  | --------------- |
-| [is_os](#is_os)           | Is the current compilation target system? | >= 2.0.1        |
-| [is_arch](#is_arch)       | Is the current compilation architecture?  | >= 2.0.1        |
-| [is_plat](#is_plat)       | Is the current compilation platform?      | >= 2.0.1        |
-| [is_host](#is_host)       | Is the current compilation host system?   | >= 2.1.4        |
-| [is_mode](#is_mode)       | Is the current compilation mode?          | >= 2.0.1        |
-| [is_kind](#is_kind)       | Is the current target kind?               | >= 2.0.1        |
-| [is_option](#is_option)   | Is the given options enabled?             | >= 2.0.1        |
+| Interfaces                | Description                               | Support version             |
+| ------------------------- | ----------------------------------------  | --------------------------- |
+| [is_os](#is_os)           | Is the current compilation target system? | >= 2.0.1                    |
+| [is_arch](#is_arch)       | Is the current compilation architecture?  | >= 2.0.1                    |
+| [is_plat](#is_plat)       | Is the current compilation platform?      | >= 2.0.1                    |
+| [is_host](#is_host)       | Is the current compilation host system?   | >= 2.1.4                    |
+| [is_mode](#is_mode)       | Is the current compilation mode?          | >= 2.0.1                    |
+| [is_kind](#is_kind)       | Is the current target kind?               | >= 2.0.1                    |
+| [is_option](#is_option)   | Is the given options enabled?             | >= 2.0.1 < 2.2.2 deprecated |
+| [is_config](#is_config)   | Is the given config values?               | >= 2.2.2                    |
+| [has_config](#has_config) | Is the given configs enabled?             | >= 2.2.2                    |
 
 ##### is_os 
 
@@ -155,7 +157,7 @@ if is_mode("debug") then
     -- enable debug symbols
     set_symbols("debug")
 
-    -- 禁用优化
+    -- disable optimization
     set_optimize("none")
 
 end
@@ -224,6 +226,10 @@ $ xmake
 
 ###### Is the given options enabled
 
+<p class="tips">
+This interface has been deprecated after v2.2.2, please use [has_config](#has_config) instead of it.
+</p>
+
 You can this api to check the custom option configuration command:`xmake f --xxxx=y`
 
 For example, We want to enable the custom option: `xmake f --demo=y` and check it from `xmake.lua`.
@@ -234,6 +240,80 @@ if is_option("demo") then
 end
 ```
 
+##### has_config
+
+###### Is the given configs enabled?
+
+This interface is introduced from version 2.2.2 to detect whether a custom or built-in option/configuration exists or is enabled.
+
+For example, the following configuration will be true:
+
+```console
+# enable the given config or option (if be boolean type)
+$ xmake f --test1=y
+$ xmake f --test1=yes
+$ xmake f --test1=true
+
+# set the config value
+$ xmake f --test2=value
+```
+
+```lua
+if has_config("test1", "test2") then
+    add_defines("TEST")
+end
+```
+
+And the following configuration will be false:
+
+```console
+# disable config/option(if be boolean type)
+$ xmake f --test1=n
+$ xmake f --test1=no
+$ xmake f --test1=false
+```
+
+<p class="tips">
+This interface can determine not only the built-in global and local configs, 
+but also the custom options defined through the [option](#option).
+</p>
+
+##### is_config
+
+###### Is the given config values?
+
+This interface is introduced from version 2.2.2 to determine whether the specified configuration is a given value.
+
+For example:
+
+```console
+$ xmake f --test=hello1
+```
+
+```lua
+option("test")
+    set_showmenu("true")
+    set_description("The test config option")
+option_end()
+
+if is_config("test", "hello1", "hello2") then
+    add_defines("HELLO")
+end
+```
+
+Not only that, we can also set pattern matching rules to determine values, such as:
+
+```lua
+if is_config("test", "hello.*") then
+    add_defines("HELLO")
+end
+```
+
+<p class="tips">
+This interface is not only able to determine the custom options defined through the [option](#option), 
+but also to determine the built-in global and local configuration.
+</p>
+
 #### Global Interfaces
 
 The global interface affects the whole project description scope and all sub-project files.

+ 102 - 20
zh/manual.md

@@ -9,17 +9,17 @@ search: zh
 
 接口的命名,是有按照预定义的一些规范来命名的,这样更加方便理解和易于使用,目前命名按照如下一些规则:
 
-| 接口规则              | 描述                                                         |
-| --------------------- | ------------------------------------------------------------ |
-| `is_`前缀的接口       | 表示为条件判断                                               |
-| `set_`前缀的接口      | 表示为覆盖设置                                               |
-| `add_`前缀的接口      | 表示为追加设置                                               |
-| `s`后缀的接口         | 表示支持多值传入,例如:`add_files("*.c", "test.cpp")`       |
-| `on_`前缀的接口       | 表示为覆盖内置脚本                                           |
-| `before_`前缀的接口   | 表示为在内置脚本运行前,执行此脚本                           |
-| `after_`前缀的接口    | 表示为在内置脚本运行后,执行此脚本                           |
-| `scope("name")`的接口 | 表示为定义一个描述域,例如:`target("xxx")`, `option("xxx")` |
-| 描述域/描述设置       | 建议缩进表示                                                 |
+| 接口规则                | 描述                                                         |
+| ----------------------- | ------------------------------------------------------------ |
+| `is_`, `has_`前缀的接口 | 表示为条件判断                                               |
+| `set_`前缀的接口        | 表示为覆盖设置                                               |
+| `add_`前缀的接口        | 表示为追加设置                                               |
+| `s`后缀的接口           | 表示支持多值传入,例如:`add_files("*.c", "test.cpp")`       |
+| `on_`前缀的接口         | 表示为覆盖内置脚本                                           |
+| `before_`前缀的接口     | 表示为在内置脚本运行前,执行此脚本                           |
+| `after_`前缀的接口      | 表示为在内置脚本运行后,执行此脚本                           |
+| `scope("name")`的接口   | 表示为定义一个描述域,例如:`target("xxx")`, `option("xxx")` |
+| 描述域/描述设置         | 建议缩进表示                                                 |
 
 
 ## 接口文档
@@ -28,15 +28,17 @@ search: zh
 
 条件判断的api,一般用于必须要处理特定平台的编译逻辑的场合。。通常跟lua的if语句配合使用。
 
-| 接口                      | 描述                          | 支持版本 |
-| ------------------------- | ----------------------------- | -------- |
-| [is_os](#is_os)           | 判断当前构建目标的操作系统    | >= 2.0.1 |
-| [is_arch](#is_arch)       | 判断当前编译架构              | >= 2.0.1 |
-| [is_plat](#is_plat)       | 判断当前编译平台              | >= 2.0.1 |
-| [is_host](#is_host)       | 判断当前主机环境操作系统      | >= 2.1.4 |
-| [is_mode](#is_mode)       | 判断当前编译模式              | >= 2.0.1 |
-| [is_kind](#is_kind)       | 判断当前编译类型              | >= 2.0.1 |
-| [is_option](#is_option)   | 判断选项是否启用              | >= 2.0.1 |
+| 接口                      | 描述                          | 支持版本                |
+| ------------------------- | ----------------------------- | ----------------------- |
+| [is_os](#is_os)           | 判断当前构建目标的操作系统    | >= 2.0.1                |
+| [is_arch](#is_arch)       | 判断当前编译架构              | >= 2.0.1                |
+| [is_plat](#is_plat)       | 判断当前编译平台              | >= 2.0.1                |
+| [is_host](#is_host)       | 判断当前主机环境操作系统      | >= 2.1.4                |
+| [is_mode](#is_mode)       | 判断当前编译模式              | >= 2.0.1                |
+| [is_kind](#is_kind)       | 判断当前编译类型              | >= 2.0.1                |
+| [is_option](#is_option)   | 判断选项是否启用              | >= 2.0.1 < 2.2.2 已废弃 |
+| [is_config](#is_config)   | 判断指定配置是否为给定的值    | >= 2.2.2                |
+| [has_config](#has_config) | 判断配置是否启用或者存在      | >= 2.2.2                |
 
 ##### is_os 
 
@@ -239,6 +241,10 @@ $ xmake
 
 ###### 判断选项是否启用
 
+<p class="tips">
+此接口在2.2.2版本之后已经弃用,请使用[has_config](#has_config)来代替。
+</p>
+
 用于检测自定义的编译配置选型:`xmake f --xxxx=y`
 
 如果某个自动检测选项、手动设置选项被启用,那么可以通过`is_option`接口来判断,例如:
@@ -252,6 +258,82 @@ if is_option("demo") then
 end
 ```
 
+##### has_config
+
+###### 判断配置是否启用或者存在
+
+此接口从2.2.2版本开始引入,用于检测自定义或者内置的编译配置是否存在或启用。
+
+例如以下配置情况,都会返回true:
+
+```console
+# 启用某个配置选项(如果是boolean类型配置)
+$ xmake f --test1=y
+$ xmake f --test1=yes
+$ xmake f --test1=true
+
+# 设置某个配置选项的值
+$ xmake f --test2=value
+```
+
+```lua
+-- 如果test1或者test2被设置或者启用
+if has_config("test1", "test2") then
+    add_defines("TEST")
+end
+```
+
+而下面的情况则会禁用配置,返回false:
+
+```console
+# 禁用配置(如果是boolean类型配置)
+$ xmake f --test1=n
+$ xmake f --test1=no
+$ xmake f --test1=false
+```
+
+<p class="tips">
+此接口不仅能够判断内置的全局配置、本地配置,同时还可以判断通过[option](#option)定义的自定义配置选项。
+</p>
+
+##### is_config
+
+###### 判断指定配置是否为给定的值
+
+此接口从2.2.2版本开始引入,用于判断指定配置是否为给定的值。
+
+例如:
+
+```console
+$ xmake f --test=hello1
+```
+
+```lua
+-- 自定义一个配置选项到命令行菜单
+option("test")
+    set_showmenu("true")
+    set_description("The test config option")
+option_end()
+
+-- 如果自定义的test配置值是hello1或者hello2
+if is_config("test", "hello1", "hello2") then
+    add_defines("HELLO")
+end
+```
+
+不仅如此,我们还可以设置模式匹配规则去判断值,例如:
+
+```lua
+-- 如果自定义的test配置值带有hello前缀
+if is_config("test", "hello.*") then
+    add_defines("HELLO")
+end
+```
+
+<p class="tips">
+此接口不仅能够判断通过[option](#option)定义的自定义配置选项,同时还能判断内置的全局配置、本地配置。
+</p>
+
 #### 全局接口
 
 全局接口影响整个工程描述,被调用后,后面被包含进来的所有子`xmake.lua`都会受影响。