schema-entry


Nameschema-entry JSON
Version 0.1.5 PyPI version JSON
download
home_pagehttps://github.com/Python-Tools/schema_entry
SummaryMy package description
upload_time2021-04-20 02:48:00
maintainer
docs_urlNone
authorhsz
requires_python
licenseMIT License
keywords config environment variable command line arguments config file
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 简介

程序入口的构造工具.

这个基类的设计目的是为了配置化入口的定义.通过继承和覆盖基类中的特定字段和方法来实现入口的参数配置读取.

目前的实现可以依次从指定路径下的json文件,环境变量,命令行参数读取需要的数据.
然后校验是否符合设定的json schema规定的模式,在符合模式后执行注册进去的回调函数.

入口树中可以有中间节点,用于分解复杂命令行参数,中间节点不会执行.
他们将参数传递给下一级节点,直到尾部可以执行为止.

# 特性

+ 根据子类的名字小写构造命令
+ 根据子类的docstring,`epilog字段`和`description字段`自动构造,命令行说明.
+ 根据子类的`schema字段`和`env_prefix字段`自动构造环境变量的读取规则.
+ 根据子类的`default_config_file_paths字段`自动按顺序读取json,yaml格式配置文件中的参数.
+ 根据`schema字段`构造命令行参数和配置校验
+ 使用装饰器`@as_main`注册获取到配置后执行的函数
+ 通过覆写`parse_commandline_args`方法来定义命令行参数的读取
+ 入口节点可以通过方法`regist_sub`注册子节点

# 安装

```bash
pip install schema_entry
```

# 使用介绍

## 动机

`schema_entry`模块提供了一个基类`EntryPoint`用于构造复杂的程序入口.通常我们的程序入口参数有3个途径:

1. 配置文件
2. 环境变量
3. 命令行参数

在docker广泛应用之前可能用的最多的是命令行参数.但在docker大行其道的现在,配置文件(docker config)和环境变量(environment字段)变得更加重要.

随之而来的是参数的校验问题,python标准库`argparse`本身有不错的参数约束能力,但配置文件中的和环境变量中的参数就需要额外校验了.

这个项目的目的是简化定义入口这个非常通用的业务,将代码尽量配置化.

## 使用方法

首先我们来分析下一个入口形式.

通常一个程序的入口可能简单也可能复杂,但无非两种

1. 中间节点,比如`docker stack`, 它本质上并不执行操作,它只是表示要执行的是关于子模块的操作.当单独执行这条命令时实际上它什么都没做,它下面的子命令`git submodule add`这类才是实际可以执行的节点.而我定义这种中间节点单独被执行应该打印其帮助说明文本.
2. 执行节点,比如`docker run`,这种就是`可以执行的节点`.

本模块的基本用法是:

1. 通过继承`EntryPoint`类并覆写其中的字段来定义不同的节点

2. 通过实例化`EntryPoint`的子类并使用其实例方法`regist_subcmd`或者`regist_sub`来定义不同节点的类型和节点的调用顺序

3. 使用`可以执行节点`的实例方法`as_main`(装饰器)来指定不同节点的入口函数.

4. 命令行中按`根节点`到`可以执行节点`的顺序输入构造命令,获取来自配置文件,环境变量,命令行参数中的参数,作为注册入口函数的参数调用入口函数.

### 节点名

我们可以定义`_name`字段为节点命名,如果没有那么节点名则为子类类名的全小写形式.

### 节点的帮助信息

我们可以定义`usage`来定义用法帮助字符串,如果没有定义则会自动构造,中间节点会是`root subcmd ... [subcmd]`;
可执行节点会是`root subcmd ... entry [options]`

### 执行节点

上面说过执行节点的任务有3个:

1. 从配置文件,环境变量,命令行参数获取配置参数
2. [可选]校验配置参数是否符合要求
3. [可选]将配置作为参数引用到程序中.

#### 通过定义`schema字段进行参数校验`

我们可以定义`schema字段`来激活校验功能

```python
class Test_A(EntryPoint):
    default_config_file_paths = [
        "/test_config.json",
        str(Path.home().joinpath(".test_config.json")),
        "./test_config.json"
    ]
    schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "properties": {
            "a": {
                "type": "integer"
            }
        },
        "required": ["a"]
    }
```

`EntryPoint`的子类会在解析获得参数后校验参数字典是否符合schema中定义的模式.

当然schema字段也不能乱写,它的规则是json schema的一个子集:

```json
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "properties": {
            "type": "object",
            "minProperties": 1,
            "additionalProperties": False,
            "patternProperties": {
                "^\\w+$": {
                    "oneOf": [
                        {
                            "type": "object",
                            "additionalProperties": False,
                            "required": ["type"],
                            "properties": {
                                "type": {
                                    "type": "string",
                                    "const": "boolean"
                                },
                                "default": {
                                    "type": "boolean",
                                },
                                "const": {
                                    "type": "string"
                                },
                                "description": {
                                    "type": "string"
                                },
                                "$comment": {
                                    "type": "string"
                                },
                                "title": {
                                    "type": "string",
                                    "pattern": "^[a-b]|[d-z]$"
                                }
                            }
                        },
                        {
                            "type": "object",
                            "additionalProperties": false,
                            "required": ["type"],
                            "properties": {
                                "type": {
                                    "type": "string",
                                    "const": "string"
                                },
                                "default": {
                                    "type": "string",
                                },
                                "const": {
                                    "type": "string"
                                },
                                "enum": {
                                    "type": "array",
                                    "items": {
                                        "type": "string"
                                    }
                                },
                                "maxLength": {
                                    "type": "integer",
                                    "minimum": 0
                                },
                                "minLength": {
                                    "type": "integer",
                                    "minimum": 0
                                },
                                "pattern": {
                                    "type": "string"
                                },
                                "format": {
                                    "type": "string"
                                },
                                "description": {
                                    "type": "string"
                                },
                                "$comment": {
                                    "type": "string"
                                },
                                "title": {
                                    "type": "string",
                                    "pattern": r"^[a-b]|[d-z]$"
                                }
                            }
                        },
                        {
                            "type": "object",
                            "additionalProperties": false,
                            "required": ["type"],
                            "properties": {
                                "type": {
                                    "type": "string",
                                    "const": "number"
                                },
                                "default": {
                                    "type": "number",
                                },
                                "const": {
                                    "type": "number"
                                },
                                "enum": {
                                    "type": "array",
                                    "items": {
                                        "type": "number"
                                    }
                                },
                                "maximum": {
                                    "type": "number",
                                },
                                "exclusiveMaximum": {
                                    "type": "number",
                                },
                                "minimum": {
                                    "type": "number",

                                },
                                "exclusiveMinimum": {
                                    "type": "number",

                                },
                                "description": {
                                    "type": "string"
                                },
                                "$comment": {
                                    "type": "string"
                                },
                                "title": {
                                    "type": "string",
                                    "pattern": "^[a-b]|[d-z]$"
                                }
                            }
                        },
                        {
                            "type": "object",
                            "additionalProperties": false,
                            "required": ["type"],
                            "properties": {
                                "type": {
                                    "type": "string",
                                    "const": "integer"
                                },
                                "default": {
                                    "type": "integer",
                                },
                                "const": {
                                    "type": "integer"
                                },
                                "enum": {
                                    "type": "array",
                                    "items": {
                                        "type": "integer"
                                    }
                                },
                                "maximum": {
                                    "type": "integer",
                                },
                                "exclusiveMaximum": {
                                    "type": "integer",
                                },
                                "minimum": {
                                    "type": "integer",

                                },
                                "exclusiveMinimum": {
                                    "type": "integer",

                                },
                                "description": {
                                    "type": "string"
                                },
                                "$comment": {
                                    "type": "string"
                                },
                                "title": {
                                    "type": "string",
                                    "pattern": "^[a-b]|[d-z]$"
                                }
                            }
                        },
                        {
                            "type": "object",
                            "additionalProperties": false,
                            "required": ["type"],
                            "properties": {
                                "type": {
                                    "type": "string",
                                    "const": "array"
                                },
                                "default": {
                                    "type": "array",
                                    "items": {
                                        "type": ["string", "number", "integer"]
                                    }
                                },
                                "items": {
                                    "type": "object",
                                    "required": ["type"],
                                    "additionalProperties": false,
                                    "properties": {
                                        "type": {
                                            "type": "string",
                                            "enum": ["string", "number", "integer"]
                                        },
                                        "enum":{
                                            "type": "array"
                                        }
                                    }
                                },
                                "description": {
                                    "type": "string"
                                },
                                "$comment": {
                                    "type": "string"
                                },
                                "title": {
                                    "type": "string",
                                    "pattern": "^[a-b]|[d-z]$"
                                }
                            }
                        }
                    ]

                }
            }
        },
        "type": {
            "type": "string",
            "const": "object"
        },
        "required": {
            "type": "array",
            "items": {
                "type": "string"
            }
        }

    },
    "required": ["properties", "type"]
}
```

简而言之就是:

1. 最外层必须有`properties`和`type`字段且`type`字段必须为`object`,可以有`required`字段
2. 最外层`properties`中的字段名必须是由`数字`,`字母`和`_`组成,
3. 字段类型只能是`string`,`boolean`,`number`,`integer`,`array`之一
4. 字段类型如果为`array`则内部必须要有`items`且`items`中必须有`type`字段,且该`type`字段的值必须为`string`,`number`,`integer`之一


如果我们不想校验,那么可以设置`verify_schema`为`False`强行关闭这个功能.

#### 从定义的schema中获取默认配置

我们在定义schema时可以在`"properties"`字段定义的模式描述中通过`default`字段指定描述字段的默认值

```python
class Test_A(EntryPoint):
    schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "properties": {
            "a_a": {
                "type": "number"
                "default": 10.1
            }
        },
        "required": ["a_a"]
    }
```

这样即便没有其他输入这个参数也会有这个默认值兜底

#### 从指定配置文件中读取配置

我们可以使用字段`default_config_file_paths`指定从固定的几个路径中读取配置文件,配置文件支持`json`和`yaml`两种格式.
我们也可以通过字段`config_file_only_get_need`定义从配置文件中读取配置的行为(默认为`True`),
 当置为`True`时我们只会在配置文件中读取schema中定义的字段,否则则会加载全部字段.

也可以通过设置`load_all_config_file = True`来按设定顺序读取全部预设的配置文件位置

默认配置文件地址是一个列表,会按顺序查找读取,只要找到了满足条件的配置文件就会读取.

```python
from pathlib import Path
from schema_entry import EntryPoint

class Test_A(EntryPoint):

    default_config_file_paths = [
        "/test_config.json",
        str(Path.home().joinpath(".test_config.json")),
        "./test_config.json",
        "./test_config_other.json"
    ]
```

##### 指定特定命名的配置文件的解析方式

可以使用`@regist_config_file_parser(config_file_name)`来注册如何解析特定命名的配置文件.这一特性可以更好的定制化配置文件的读取

```python
class Test_AC(EntryPoint):
    load_all_config_file = True
    default_config_file_paths = [
        "./test_config.json",
        "./test_config1.json",
        "./test_other_config2.json"
    ]
root = Test_AC()

@root.regist_config_file_parser("test_other_config2.json")
def _1(p: Path) -> Dict[str, Any]:
    with open(p) as f:
        temp = json.load(f)
    return {k.lower(): v for k, v in temp.items()}

```

如果想在定义子类时固定好,也可以定义`_config_file_parser_map:Dict[str,Callable[[Path], Dict[str, Any]]]`

```python
def test_other_config2_parser( p: Path) -> Dict[str, Any]:
    with open(p) as f:
        temp = json.load(f)
    return {k.lower(): v for k, v in temp.items()}
class Test_AC(EntryPoint):
    load_all_config_file = True
    default_config_file_paths = [
        "./test_config.json",
        "./test_config1.json",
        "./test_other_config2.json"
    ]
    _config_file_parser_map = {
        "test_other_config2.json": test_other_config2_parser
    }

root = Test_AC()

```

#### 从环境变量中读取配置参数

要从环境变量中读取配置必须设置`schema`字段,`EntryPoint`会按照其中`properties`字段定义的字段范围和字段类型解析环境变量.

环境变量key的规则为`前缀_字段名的大写`.前缀的默认值为`...父节命令节点的父命令节点大写_父节命令节点大写_子命令节点大写`.
我们也可以通过设定`env_prefix`字段来替换默认前缀,替换的前缀依然会被转化为大写.

```python
class Test_A(EntryPoint):
    env_prefix = "app"
    schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "properties": {
            "a_a": {
                "type": "number"
            }
        },
        "required": ["a_a"]
    }
```

如果我们不希望从环境变量中解析配置,那么也可以设置`parse_env`为`False`

#### 从命令行参数中获取配置参数

当我们定义好`schema`后所有schema中定义好的参数都可以以`--xxxx`的形式从命令行中读取,需要注意schema中定义的字段中`_`会被修改为`-`.
如果定义的字段模式中含有`title`字段,则使用title字段作为命令行缩写即`-x`的形式

这个命令行读取是使用的标准库`argparse`,构造出的解析器中`useage`,`epilog`和`description`会由类中定义的`usage`,`epilog`和docstring决定;`argv`则为传到节点处时剩下的命令行参数(每多一个节点就会从左侧摘掉一个命令行参数).

通常情况下构造的命令行解析器全部都是可选项,如果我们希望指定`schema`中一项是没有`--`的那种配置,那么可以在定义类时指定`argparse_noflag`为想要的字段,如果希望命令行中校验必填项则可以在定义类时指定`argparse_check_required=True`.需要注意如果一个字段被指定为了`noflag`那么它就是必填项了.

```python
class Test_A(EntryPoint):
    argparse_noflag = "a"
    argparse_check_required=True
    schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "properties": {
            "a": {
                "type": "number"
            },
            "b": {
                "type": "number"
            }
        },
        "required": ["a","b"]
    }
```

命令行中默认使用`-c`/`--config`来指定读取配置文件,它的读取行为受上面介绍的从自定义配置文件中读取配置的设置影响.

#### 配置的读取顺序

配置的读取顺序为`schema中定义的default值`->`配置指定的配置文件路径`->`命令行指定的配置文件`->`环境变量`->`命令行参数`,而覆盖顺序则是反过来.

#### 注册入口的执行函数

我们使用实例的装饰器方法`as_main`来实现对执行节点入口函数的注册,注册的入口函数会在解析好参数后执行,其参数就是解析好的`**config`

```python

root = Test_A()
@root.as_main
def main(a,b):
    print(a)
    print(b)

```

另一种指定入口函数的方法是重写子类的`do_main(self)->None`方法

```python
class Test_A(EntryPoint):
    argparse_noflag = "a"
    argparse_check_required=True
    schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "properties": {
            "a": {
                "type": "number"
            },
            "b": {
                "type": "number"
            }
        },
        "required": ["a","b"]
    }
    def do_main(self)->None:
        print(self.config)
```

#### 直接从节点对象中获取配置

节点对象的`config`属性会在每次调用时copy一份当前的配置值,config是不可写的.

```python
print(root.config)
```

### 中间节点

中间节点并不能执行程序,它只是用于描述一个范围内的命令集合,因此它的作用就是充当`help`指令.我们定义中间节点并不能执行.但必须有至少一个子节点才是中间节点.因此即便一个节点定义了上面的配置,只要它有子节点就不会按上面的执行流程执行.

利用中间节点我们可以构造出非常复杂的启动命令树.

#### 注册子节点

中间节点的注册有两个接口

+ `regist_subcmd`用于注册一个已经实例化的子节点

    ```python
    class A(EntryPoint):
        pass

    class B(EntryPoint):
        pass

    a = A()

    b = B()

    a.regist_subcmd(b)
    ```

+ `regist_sub`用于注册一个子节点类,它会返回被注册的节点的一个实例

    ```python
    class A(EntryPoint):
        pass

    class B(EntryPoint):
        pass

    a = A()
    b =a.regist_sub(B)
    ```

MIT License

Copyright (c) 2020 Python-Tools

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Python-Tools/schema_entry",
    "name": "schema-entry",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "config,environment variable,command line arguments,config file",
    "author": "hsz",
    "author_email": "hsz1273327@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/5f/0e/55ab8399a6cdbf1ad1f42824080a00274bee22c1a838042138eb1112c410/schema_entry-0.1.5.tar.gz",
    "platform": "",
    "description": "# \u7b80\u4ecb\n\n\u7a0b\u5e8f\u5165\u53e3\u7684\u6784\u9020\u5de5\u5177.\n\n\u8fd9\u4e2a\u57fa\u7c7b\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u4e3a\u4e86\u914d\u7f6e\u5316\u5165\u53e3\u7684\u5b9a\u4e49.\u901a\u8fc7\u7ee7\u627f\u548c\u8986\u76d6\u57fa\u7c7b\u4e2d\u7684\u7279\u5b9a\u5b57\u6bb5\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u5165\u53e3\u7684\u53c2\u6570\u914d\u7f6e\u8bfb\u53d6.\n\n\u76ee\u524d\u7684\u5b9e\u73b0\u53ef\u4ee5\u4f9d\u6b21\u4ece\u6307\u5b9a\u8def\u5f84\u4e0b\u7684json\u6587\u4ef6,\u73af\u5883\u53d8\u91cf,\u547d\u4ee4\u884c\u53c2\u6570\u8bfb\u53d6\u9700\u8981\u7684\u6570\u636e.\n\u7136\u540e\u6821\u9a8c\u662f\u5426\u7b26\u5408\u8bbe\u5b9a\u7684json schema\u89c4\u5b9a\u7684\u6a21\u5f0f,\u5728\u7b26\u5408\u6a21\u5f0f\u540e\u6267\u884c\u6ce8\u518c\u8fdb\u53bb\u7684\u56de\u8c03\u51fd\u6570.\n\n\u5165\u53e3\u6811\u4e2d\u53ef\u4ee5\u6709\u4e2d\u95f4\u8282\u70b9,\u7528\u4e8e\u5206\u89e3\u590d\u6742\u547d\u4ee4\u884c\u53c2\u6570,\u4e2d\u95f4\u8282\u70b9\u4e0d\u4f1a\u6267\u884c.\n\u4ed6\u4eec\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u4e0b\u4e00\u7ea7\u8282\u70b9,\u76f4\u5230\u5c3e\u90e8\u53ef\u4ee5\u6267\u884c\u4e3a\u6b62.\n\n# \u7279\u6027\n\n+ \u6839\u636e\u5b50\u7c7b\u7684\u540d\u5b57\u5c0f\u5199\u6784\u9020\u547d\u4ee4\n+ \u6839\u636e\u5b50\u7c7b\u7684docstring,`epilog\u5b57\u6bb5`\u548c`description\u5b57\u6bb5`\u81ea\u52a8\u6784\u9020,\u547d\u4ee4\u884c\u8bf4\u660e.\n+ \u6839\u636e\u5b50\u7c7b\u7684`schema\u5b57\u6bb5`\u548c`env_prefix\u5b57\u6bb5`\u81ea\u52a8\u6784\u9020\u73af\u5883\u53d8\u91cf\u7684\u8bfb\u53d6\u89c4\u5219.\n+ \u6839\u636e\u5b50\u7c7b\u7684`default_config_file_paths\u5b57\u6bb5`\u81ea\u52a8\u6309\u987a\u5e8f\u8bfb\u53d6json,yaml\u683c\u5f0f\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u53c2\u6570.\n+ \u6839\u636e`schema\u5b57\u6bb5`\u6784\u9020\u547d\u4ee4\u884c\u53c2\u6570\u548c\u914d\u7f6e\u6821\u9a8c\n+ \u4f7f\u7528\u88c5\u9970\u5668`@as_main`\u6ce8\u518c\u83b7\u53d6\u5230\u914d\u7f6e\u540e\u6267\u884c\u7684\u51fd\u6570\n+ \u901a\u8fc7\u8986\u5199`parse_commandline_args`\u65b9\u6cd5\u6765\u5b9a\u4e49\u547d\u4ee4\u884c\u53c2\u6570\u7684\u8bfb\u53d6\n+ \u5165\u53e3\u8282\u70b9\u53ef\u4ee5\u901a\u8fc7\u65b9\u6cd5`regist_sub`\u6ce8\u518c\u5b50\u8282\u70b9\n\n# \u5b89\u88c5\n\n```bash\npip install schema_entry\n```\n\n# \u4f7f\u7528\u4ecb\u7ecd\n\n## \u52a8\u673a\n\n`schema_entry`\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u57fa\u7c7b`EntryPoint`\u7528\u4e8e\u6784\u9020\u590d\u6742\u7684\u7a0b\u5e8f\u5165\u53e3.\u901a\u5e38\u6211\u4eec\u7684\u7a0b\u5e8f\u5165\u53e3\u53c2\u6570\u67093\u4e2a\u9014\u5f84:\n\n1. \u914d\u7f6e\u6587\u4ef6\n2. \u73af\u5883\u53d8\u91cf\n3. \u547d\u4ee4\u884c\u53c2\u6570\n\n\u5728docker\u5e7f\u6cdb\u5e94\u7528\u4e4b\u524d\u53ef\u80fd\u7528\u7684\u6700\u591a\u7684\u662f\u547d\u4ee4\u884c\u53c2\u6570.\u4f46\u5728docker\u5927\u884c\u5176\u9053\u7684\u73b0\u5728,\u914d\u7f6e\u6587\u4ef6(docker config)\u548c\u73af\u5883\u53d8\u91cf(environment\u5b57\u6bb5)\u53d8\u5f97\u66f4\u52a0\u91cd\u8981.\n\n\u968f\u4e4b\u800c\u6765\u7684\u662f\u53c2\u6570\u7684\u6821\u9a8c\u95ee\u9898,python\u6807\u51c6\u5e93`argparse`\u672c\u8eab\u6709\u4e0d\u9519\u7684\u53c2\u6570\u7ea6\u675f\u80fd\u529b,\u4f46\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u548c\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u53c2\u6570\u5c31\u9700\u8981\u989d\u5916\u6821\u9a8c\u4e86.\n\n\u8fd9\u4e2a\u9879\u76ee\u7684\u76ee\u7684\u662f\u7b80\u5316\u5b9a\u4e49\u5165\u53e3\u8fd9\u4e2a\u975e\u5e38\u901a\u7528\u7684\u4e1a\u52a1,\u5c06\u4ee3\u7801\u5c3d\u91cf\u914d\u7f6e\u5316.\n\n## \u4f7f\u7528\u65b9\u6cd5\n\n\u9996\u5148\u6211\u4eec\u6765\u5206\u6790\u4e0b\u4e00\u4e2a\u5165\u53e3\u5f62\u5f0f.\n\n\u901a\u5e38\u4e00\u4e2a\u7a0b\u5e8f\u7684\u5165\u53e3\u53ef\u80fd\u7b80\u5355\u4e5f\u53ef\u80fd\u590d\u6742,\u4f46\u65e0\u975e\u4e24\u79cd\n\n1. \u4e2d\u95f4\u8282\u70b9,\u6bd4\u5982`docker stack`, \u5b83\u672c\u8d28\u4e0a\u5e76\u4e0d\u6267\u884c\u64cd\u4f5c,\u5b83\u53ea\u662f\u8868\u793a\u8981\u6267\u884c\u7684\u662f\u5173\u4e8e\u5b50\u6a21\u5757\u7684\u64cd\u4f5c.\u5f53\u5355\u72ec\u6267\u884c\u8fd9\u6761\u547d\u4ee4\u65f6\u5b9e\u9645\u4e0a\u5b83\u4ec0\u4e48\u90fd\u6ca1\u505a,\u5b83\u4e0b\u9762\u7684\u5b50\u547d\u4ee4`git submodule add`\u8fd9\u7c7b\u624d\u662f\u5b9e\u9645\u53ef\u4ee5\u6267\u884c\u7684\u8282\u70b9.\u800c\u6211\u5b9a\u4e49\u8fd9\u79cd\u4e2d\u95f4\u8282\u70b9\u5355\u72ec\u88ab\u6267\u884c\u5e94\u8be5\u6253\u5370\u5176\u5e2e\u52a9\u8bf4\u660e\u6587\u672c.\n2. \u6267\u884c\u8282\u70b9,\u6bd4\u5982`docker run`,\u8fd9\u79cd\u5c31\u662f`\u53ef\u4ee5\u6267\u884c\u7684\u8282\u70b9`.\n\n\u672c\u6a21\u5757\u7684\u57fa\u672c\u7528\u6cd5\u662f:\n\n1. \u901a\u8fc7\u7ee7\u627f`EntryPoint`\u7c7b\u5e76\u8986\u5199\u5176\u4e2d\u7684\u5b57\u6bb5\u6765\u5b9a\u4e49\u4e0d\u540c\u7684\u8282\u70b9\n\n2. \u901a\u8fc7\u5b9e\u4f8b\u5316`EntryPoint`\u7684\u5b50\u7c7b\u5e76\u4f7f\u7528\u5176\u5b9e\u4f8b\u65b9\u6cd5`regist_subcmd`\u6216\u8005`regist_sub`\u6765\u5b9a\u4e49\u4e0d\u540c\u8282\u70b9\u7684\u7c7b\u578b\u548c\u8282\u70b9\u7684\u8c03\u7528\u987a\u5e8f\n\n3. \u4f7f\u7528`\u53ef\u4ee5\u6267\u884c\u8282\u70b9`\u7684\u5b9e\u4f8b\u65b9\u6cd5`as_main`(\u88c5\u9970\u5668)\u6765\u6307\u5b9a\u4e0d\u540c\u8282\u70b9\u7684\u5165\u53e3\u51fd\u6570.\n\n4. \u547d\u4ee4\u884c\u4e2d\u6309`\u6839\u8282\u70b9`\u5230`\u53ef\u4ee5\u6267\u884c\u8282\u70b9`\u7684\u987a\u5e8f\u8f93\u5165\u6784\u9020\u547d\u4ee4,\u83b7\u53d6\u6765\u81ea\u914d\u7f6e\u6587\u4ef6,\u73af\u5883\u53d8\u91cf,\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u7684\u53c2\u6570,\u4f5c\u4e3a\u6ce8\u518c\u5165\u53e3\u51fd\u6570\u7684\u53c2\u6570\u8c03\u7528\u5165\u53e3\u51fd\u6570.\n\n### \u8282\u70b9\u540d\n\n\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49`_name`\u5b57\u6bb5\u4e3a\u8282\u70b9\u547d\u540d,\u5982\u679c\u6ca1\u6709\u90a3\u4e48\u8282\u70b9\u540d\u5219\u4e3a\u5b50\u7c7b\u7c7b\u540d\u7684\u5168\u5c0f\u5199\u5f62\u5f0f.\n\n### \u8282\u70b9\u7684\u5e2e\u52a9\u4fe1\u606f\n\n\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49`usage`\u6765\u5b9a\u4e49\u7528\u6cd5\u5e2e\u52a9\u5b57\u7b26\u4e32,\u5982\u679c\u6ca1\u6709\u5b9a\u4e49\u5219\u4f1a\u81ea\u52a8\u6784\u9020,\u4e2d\u95f4\u8282\u70b9\u4f1a\u662f`root subcmd ... [subcmd]`;\n\u53ef\u6267\u884c\u8282\u70b9\u4f1a\u662f`root subcmd ... entry [options]`\n\n### \u6267\u884c\u8282\u70b9\n\n\u4e0a\u9762\u8bf4\u8fc7\u6267\u884c\u8282\u70b9\u7684\u4efb\u52a1\u67093\u4e2a:\n\n1. \u4ece\u914d\u7f6e\u6587\u4ef6,\u73af\u5883\u53d8\u91cf,\u547d\u4ee4\u884c\u53c2\u6570\u83b7\u53d6\u914d\u7f6e\u53c2\u6570\n2. [\u53ef\u9009]\u6821\u9a8c\u914d\u7f6e\u53c2\u6570\u662f\u5426\u7b26\u5408\u8981\u6c42\n3. [\u53ef\u9009]\u5c06\u914d\u7f6e\u4f5c\u4e3a\u53c2\u6570\u5f15\u7528\u5230\u7a0b\u5e8f\u4e2d.\n\n#### \u901a\u8fc7\u5b9a\u4e49`schema\u5b57\u6bb5\u8fdb\u884c\u53c2\u6570\u6821\u9a8c`\n\n\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49`schema\u5b57\u6bb5`\u6765\u6fc0\u6d3b\u6821\u9a8c\u529f\u80fd\n\n```python\nclass Test_A(EntryPoint):\n    default_config_file_paths = [\n        \"/test_config.json\",\n        str(Path.home().joinpath(\".test_config.json\")),\n        \"./test_config.json\"\n    ]\n    schema = {\n        \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n        \"type\": \"object\",\n        \"properties\": {\n            \"a\": {\n                \"type\": \"integer\"\n            }\n        },\n        \"required\": [\"a\"]\n    }\n```\n\n`EntryPoint`\u7684\u5b50\u7c7b\u4f1a\u5728\u89e3\u6790\u83b7\u5f97\u53c2\u6570\u540e\u6821\u9a8c\u53c2\u6570\u5b57\u5178\u662f\u5426\u7b26\u5408schema\u4e2d\u5b9a\u4e49\u7684\u6a21\u5f0f.\n\n\u5f53\u7136schema\u5b57\u6bb5\u4e5f\u4e0d\u80fd\u4e71\u5199,\u5b83\u7684\u89c4\u5219\u662fjson schema\u7684\u4e00\u4e2a\u5b50\u96c6:\n\n```json\n{\n    \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"properties\": {\n            \"type\": \"object\",\n            \"minProperties\": 1,\n            \"additionalProperties\": False,\n            \"patternProperties\": {\n                \"^\\\\w+$\": {\n                    \"oneOf\": [\n                        {\n                            \"type\": \"object\",\n                            \"additionalProperties\": False,\n                            \"required\": [\"type\"],\n                            \"properties\": {\n                                \"type\": {\n                                    \"type\": \"string\",\n                                    \"const\": \"boolean\"\n                                },\n                                \"default\": {\n                                    \"type\": \"boolean\",\n                                },\n                                \"const\": {\n                                    \"type\": \"string\"\n                                },\n                                \"description\": {\n                                    \"type\": \"string\"\n                                },\n                                \"$comment\": {\n                                    \"type\": \"string\"\n                                },\n                                \"title\": {\n                                    \"type\": \"string\",\n                                    \"pattern\": \"^[a-b]|[d-z]$\"\n                                }\n                            }\n                        },\n                        {\n                            \"type\": \"object\",\n                            \"additionalProperties\": false,\n                            \"required\": [\"type\"],\n                            \"properties\": {\n                                \"type\": {\n                                    \"type\": \"string\",\n                                    \"const\": \"string\"\n                                },\n                                \"default\": {\n                                    \"type\": \"string\",\n                                },\n                                \"const\": {\n                                    \"type\": \"string\"\n                                },\n                                \"enum\": {\n                                    \"type\": \"array\",\n                                    \"items\": {\n                                        \"type\": \"string\"\n                                    }\n                                },\n                                \"maxLength\": {\n                                    \"type\": \"integer\",\n                                    \"minimum\": 0\n                                },\n                                \"minLength\": {\n                                    \"type\": \"integer\",\n                                    \"minimum\": 0\n                                },\n                                \"pattern\": {\n                                    \"type\": \"string\"\n                                },\n                                \"format\": {\n                                    \"type\": \"string\"\n                                },\n                                \"description\": {\n                                    \"type\": \"string\"\n                                },\n                                \"$comment\": {\n                                    \"type\": \"string\"\n                                },\n                                \"title\": {\n                                    \"type\": \"string\",\n                                    \"pattern\": r\"^[a-b]|[d-z]$\"\n                                }\n                            }\n                        },\n                        {\n                            \"type\": \"object\",\n                            \"additionalProperties\": false,\n                            \"required\": [\"type\"],\n                            \"properties\": {\n                                \"type\": {\n                                    \"type\": \"string\",\n                                    \"const\": \"number\"\n                                },\n                                \"default\": {\n                                    \"type\": \"number\",\n                                },\n                                \"const\": {\n                                    \"type\": \"number\"\n                                },\n                                \"enum\": {\n                                    \"type\": \"array\",\n                                    \"items\": {\n                                        \"type\": \"number\"\n                                    }\n                                },\n                                \"maximum\": {\n                                    \"type\": \"number\",\n                                },\n                                \"exclusiveMaximum\": {\n                                    \"type\": \"number\",\n                                },\n                                \"minimum\": {\n                                    \"type\": \"number\",\n\n                                },\n                                \"exclusiveMinimum\": {\n                                    \"type\": \"number\",\n\n                                },\n                                \"description\": {\n                                    \"type\": \"string\"\n                                },\n                                \"$comment\": {\n                                    \"type\": \"string\"\n                                },\n                                \"title\": {\n                                    \"type\": \"string\",\n                                    \"pattern\": \"^[a-b]|[d-z]$\"\n                                }\n                            }\n                        },\n                        {\n                            \"type\": \"object\",\n                            \"additionalProperties\": false,\n                            \"required\": [\"type\"],\n                            \"properties\": {\n                                \"type\": {\n                                    \"type\": \"string\",\n                                    \"const\": \"integer\"\n                                },\n                                \"default\": {\n                                    \"type\": \"integer\",\n                                },\n                                \"const\": {\n                                    \"type\": \"integer\"\n                                },\n                                \"enum\": {\n                                    \"type\": \"array\",\n                                    \"items\": {\n                                        \"type\": \"integer\"\n                                    }\n                                },\n                                \"maximum\": {\n                                    \"type\": \"integer\",\n                                },\n                                \"exclusiveMaximum\": {\n                                    \"type\": \"integer\",\n                                },\n                                \"minimum\": {\n                                    \"type\": \"integer\",\n\n                                },\n                                \"exclusiveMinimum\": {\n                                    \"type\": \"integer\",\n\n                                },\n                                \"description\": {\n                                    \"type\": \"string\"\n                                },\n                                \"$comment\": {\n                                    \"type\": \"string\"\n                                },\n                                \"title\": {\n                                    \"type\": \"string\",\n                                    \"pattern\": \"^[a-b]|[d-z]$\"\n                                }\n                            }\n                        },\n                        {\n                            \"type\": \"object\",\n                            \"additionalProperties\": false,\n                            \"required\": [\"type\"],\n                            \"properties\": {\n                                \"type\": {\n                                    \"type\": \"string\",\n                                    \"const\": \"array\"\n                                },\n                                \"default\": {\n                                    \"type\": \"array\",\n                                    \"items\": {\n                                        \"type\": [\"string\", \"number\", \"integer\"]\n                                    }\n                                },\n                                \"items\": {\n                                    \"type\": \"object\",\n                                    \"required\": [\"type\"],\n                                    \"additionalProperties\": false,\n                                    \"properties\": {\n                                        \"type\": {\n                                            \"type\": \"string\",\n                                            \"enum\": [\"string\", \"number\", \"integer\"]\n                                        },\n                                        \"enum\":{\n                                            \"type\": \"array\"\n                                        }\n                                    }\n                                },\n                                \"description\": {\n                                    \"type\": \"string\"\n                                },\n                                \"$comment\": {\n                                    \"type\": \"string\"\n                                },\n                                \"title\": {\n                                    \"type\": \"string\",\n                                    \"pattern\": \"^[a-b]|[d-z]$\"\n                                }\n                            }\n                        }\n                    ]\n\n                }\n            }\n        },\n        \"type\": {\n            \"type\": \"string\",\n            \"const\": \"object\"\n        },\n        \"required\": {\n            \"type\": \"array\",\n            \"items\": {\n                \"type\": \"string\"\n            }\n        }\n\n    },\n    \"required\": [\"properties\", \"type\"]\n}\n```\n\n\u7b80\u800c\u8a00\u4e4b\u5c31\u662f:\n\n1. \u6700\u5916\u5c42\u5fc5\u987b\u6709`properties`\u548c`type`\u5b57\u6bb5\u4e14`type`\u5b57\u6bb5\u5fc5\u987b\u4e3a`object`,\u53ef\u4ee5\u6709`required`\u5b57\u6bb5\n2. \u6700\u5916\u5c42`properties`\u4e2d\u7684\u5b57\u6bb5\u540d\u5fc5\u987b\u662f\u7531`\u6570\u5b57`,`\u5b57\u6bcd`\u548c`_`\u7ec4\u6210,\n3. \u5b57\u6bb5\u7c7b\u578b\u53ea\u80fd\u662f`string`,`boolean`,`number`,`integer`,`array`\u4e4b\u4e00\n4. \u5b57\u6bb5\u7c7b\u578b\u5982\u679c\u4e3a`array`\u5219\u5185\u90e8\u5fc5\u987b\u8981\u6709`items`\u4e14`items`\u4e2d\u5fc5\u987b\u6709`type`\u5b57\u6bb5,\u4e14\u8be5`type`\u5b57\u6bb5\u7684\u503c\u5fc5\u987b\u4e3a`string`,`number`,`integer`\u4e4b\u4e00\n\n\n\u5982\u679c\u6211\u4eec\u4e0d\u60f3\u6821\u9a8c,\u90a3\u4e48\u53ef\u4ee5\u8bbe\u7f6e`verify_schema`\u4e3a`False`\u5f3a\u884c\u5173\u95ed\u8fd9\u4e2a\u529f\u80fd.\n\n#### \u4ece\u5b9a\u4e49\u7684schema\u4e2d\u83b7\u53d6\u9ed8\u8ba4\u914d\u7f6e\n\n\u6211\u4eec\u5728\u5b9a\u4e49schema\u65f6\u53ef\u4ee5\u5728`\"properties\"`\u5b57\u6bb5\u5b9a\u4e49\u7684\u6a21\u5f0f\u63cf\u8ff0\u4e2d\u901a\u8fc7`default`\u5b57\u6bb5\u6307\u5b9a\u63cf\u8ff0\u5b57\u6bb5\u7684\u9ed8\u8ba4\u503c\n\n```python\nclass Test_A(EntryPoint):\n    schema = {\n        \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n        \"type\": \"object\",\n        \"properties\": {\n            \"a_a\": {\n                \"type\": \"number\"\n                \"default\": 10.1\n            }\n        },\n        \"required\": [\"a_a\"]\n    }\n```\n\n\u8fd9\u6837\u5373\u4fbf\u6ca1\u6709\u5176\u4ed6\u8f93\u5165\u8fd9\u4e2a\u53c2\u6570\u4e5f\u4f1a\u6709\u8fd9\u4e2a\u9ed8\u8ba4\u503c\u515c\u5e95\n\n#### \u4ece\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\n\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u6bb5`default_config_file_paths`\u6307\u5b9a\u4ece\u56fa\u5b9a\u7684\u51e0\u4e2a\u8def\u5f84\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6,\u914d\u7f6e\u6587\u4ef6\u652f\u6301`json`\u548c`yaml`\u4e24\u79cd\u683c\u5f0f.\n\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5b57\u6bb5`config_file_only_get_need`\u5b9a\u4e49\u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u7684\u884c\u4e3a(\u9ed8\u8ba4\u4e3a`True`),\n \u5f53\u7f6e\u4e3a`True`\u65f6\u6211\u4eec\u53ea\u4f1a\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6schema\u4e2d\u5b9a\u4e49\u7684\u5b57\u6bb5,\u5426\u5219\u5219\u4f1a\u52a0\u8f7d\u5168\u90e8\u5b57\u6bb5.\n\n\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e`load_all_config_file = True`\u6765\u6309\u8bbe\u5b9a\u987a\u5e8f\u8bfb\u53d6\u5168\u90e8\u9884\u8bbe\u7684\u914d\u7f6e\u6587\u4ef6\u4f4d\u7f6e\n\n\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\u5730\u5740\u662f\u4e00\u4e2a\u5217\u8868,\u4f1a\u6309\u987a\u5e8f\u67e5\u627e\u8bfb\u53d6,\u53ea\u8981\u627e\u5230\u4e86\u6ee1\u8db3\u6761\u4ef6\u7684\u914d\u7f6e\u6587\u4ef6\u5c31\u4f1a\u8bfb\u53d6.\n\n```python\nfrom pathlib import Path\nfrom schema_entry import EntryPoint\n\nclass Test_A(EntryPoint):\n\n    default_config_file_paths = [\n        \"/test_config.json\",\n        str(Path.home().joinpath(\".test_config.json\")),\n        \"./test_config.json\",\n        \"./test_config_other.json\"\n    ]\n```\n\n##### \u6307\u5b9a\u7279\u5b9a\u547d\u540d\u7684\u914d\u7f6e\u6587\u4ef6\u7684\u89e3\u6790\u65b9\u5f0f\n\n\u53ef\u4ee5\u4f7f\u7528`@regist_config_file_parser(config_file_name)`\u6765\u6ce8\u518c\u5982\u4f55\u89e3\u6790\u7279\u5b9a\u547d\u540d\u7684\u914d\u7f6e\u6587\u4ef6.\u8fd9\u4e00\u7279\u6027\u53ef\u4ee5\u66f4\u597d\u7684\u5b9a\u5236\u5316\u914d\u7f6e\u6587\u4ef6\u7684\u8bfb\u53d6\n\n```python\nclass Test_AC(EntryPoint):\n    load_all_config_file = True\n    default_config_file_paths = [\n        \"./test_config.json\",\n        \"./test_config1.json\",\n        \"./test_other_config2.json\"\n    ]\nroot = Test_AC()\n\n@root.regist_config_file_parser(\"test_other_config2.json\")\ndef _1(p: Path) -> Dict[str, Any]:\n    with open(p) as f:\n        temp = json.load(f)\n    return {k.lower(): v for k, v in temp.items()}\n\n```\n\n\u5982\u679c\u60f3\u5728\u5b9a\u4e49\u5b50\u7c7b\u65f6\u56fa\u5b9a\u597d,\u4e5f\u53ef\u4ee5\u5b9a\u4e49`_config_file_parser_map:Dict[str,Callable[[Path], Dict[str, Any]]]`\n\n```python\ndef test_other_config2_parser( p: Path) -> Dict[str, Any]:\n    with open(p) as f:\n        temp = json.load(f)\n    return {k.lower(): v for k, v in temp.items()}\nclass Test_AC(EntryPoint):\n    load_all_config_file = True\n    default_config_file_paths = [\n        \"./test_config.json\",\n        \"./test_config1.json\",\n        \"./test_other_config2.json\"\n    ]\n    _config_file_parser_map = {\n        \"test_other_config2.json\": test_other_config2_parser\n    }\n\nroot = Test_AC()\n\n```\n\n#### \u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u8bfb\u53d6\u914d\u7f6e\u53c2\u6570\n\n\u8981\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u8bfb\u53d6\u914d\u7f6e\u5fc5\u987b\u8bbe\u7f6e`schema`\u5b57\u6bb5,`EntryPoint`\u4f1a\u6309\u7167\u5176\u4e2d`properties`\u5b57\u6bb5\u5b9a\u4e49\u7684\u5b57\u6bb5\u8303\u56f4\u548c\u5b57\u6bb5\u7c7b\u578b\u89e3\u6790\u73af\u5883\u53d8\u91cf.\n\n\u73af\u5883\u53d8\u91cfkey\u7684\u89c4\u5219\u4e3a`\u524d\u7f00_\u5b57\u6bb5\u540d\u7684\u5927\u5199`.\u524d\u7f00\u7684\u9ed8\u8ba4\u503c\u4e3a`...\u7236\u8282\u547d\u4ee4\u8282\u70b9\u7684\u7236\u547d\u4ee4\u8282\u70b9\u5927\u5199_\u7236\u8282\u547d\u4ee4\u8282\u70b9\u5927\u5199_\u5b50\u547d\u4ee4\u8282\u70b9\u5927\u5199`.\n\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u5b9a`env_prefix`\u5b57\u6bb5\u6765\u66ff\u6362\u9ed8\u8ba4\u524d\u7f00,\u66ff\u6362\u7684\u524d\u7f00\u4f9d\u7136\u4f1a\u88ab\u8f6c\u5316\u4e3a\u5927\u5199.\n\n```python\nclass Test_A(EntryPoint):\n    env_prefix = \"app\"\n    schema = {\n        \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n        \"type\": \"object\",\n        \"properties\": {\n            \"a_a\": {\n                \"type\": \"number\"\n            }\n        },\n        \"required\": [\"a_a\"]\n    }\n```\n\n\u5982\u679c\u6211\u4eec\u4e0d\u5e0c\u671b\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u89e3\u6790\u914d\u7f6e,\u90a3\u4e48\u4e5f\u53ef\u4ee5\u8bbe\u7f6e`parse_env`\u4e3a`False`\n\n#### \u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u83b7\u53d6\u914d\u7f6e\u53c2\u6570\n\n\u5f53\u6211\u4eec\u5b9a\u4e49\u597d`schema`\u540e\u6240\u6709schema\u4e2d\u5b9a\u4e49\u597d\u7684\u53c2\u6570\u90fd\u53ef\u4ee5\u4ee5`--xxxx`\u7684\u5f62\u5f0f\u4ece\u547d\u4ee4\u884c\u4e2d\u8bfb\u53d6,\u9700\u8981\u6ce8\u610fschema\u4e2d\u5b9a\u4e49\u7684\u5b57\u6bb5\u4e2d`_`\u4f1a\u88ab\u4fee\u6539\u4e3a`-`.\n\u5982\u679c\u5b9a\u4e49\u7684\u5b57\u6bb5\u6a21\u5f0f\u4e2d\u542b\u6709`title`\u5b57\u6bb5,\u5219\u4f7f\u7528title\u5b57\u6bb5\u4f5c\u4e3a\u547d\u4ee4\u884c\u7f29\u5199\u5373`-x`\u7684\u5f62\u5f0f\n\n\u8fd9\u4e2a\u547d\u4ee4\u884c\u8bfb\u53d6\u662f\u4f7f\u7528\u7684\u6807\u51c6\u5e93`argparse`,\u6784\u9020\u51fa\u7684\u89e3\u6790\u5668\u4e2d`useage`,`epilog`\u548c`description`\u4f1a\u7531\u7c7b\u4e2d\u5b9a\u4e49\u7684`usage`,`epilog`\u548cdocstring\u51b3\u5b9a;`argv`\u5219\u4e3a\u4f20\u5230\u8282\u70b9\u5904\u65f6\u5269\u4e0b\u7684\u547d\u4ee4\u884c\u53c2\u6570(\u6bcf\u591a\u4e00\u4e2a\u8282\u70b9\u5c31\u4f1a\u4ece\u5de6\u4fa7\u6458\u6389\u4e00\u4e2a\u547d\u4ee4\u884c\u53c2\u6570).\n\n\u901a\u5e38\u60c5\u51b5\u4e0b\u6784\u9020\u7684\u547d\u4ee4\u884c\u89e3\u6790\u5668\u5168\u90e8\u90fd\u662f\u53ef\u9009\u9879,\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6307\u5b9a`schema`\u4e2d\u4e00\u9879\u662f\u6ca1\u6709`--`\u7684\u90a3\u79cd\u914d\u7f6e,\u90a3\u4e48\u53ef\u4ee5\u5728\u5b9a\u4e49\u7c7b\u65f6\u6307\u5b9a`argparse_noflag`\u4e3a\u60f3\u8981\u7684\u5b57\u6bb5,\u5982\u679c\u5e0c\u671b\u547d\u4ee4\u884c\u4e2d\u6821\u9a8c\u5fc5\u586b\u9879\u5219\u53ef\u4ee5\u5728\u5b9a\u4e49\u7c7b\u65f6\u6307\u5b9a`argparse_check_required=True`.\u9700\u8981\u6ce8\u610f\u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u88ab\u6307\u5b9a\u4e3a\u4e86`noflag`\u90a3\u4e48\u5b83\u5c31\u662f\u5fc5\u586b\u9879\u4e86.\n\n```python\nclass Test_A(EntryPoint):\n    argparse_noflag = \"a\"\n    argparse_check_required=True\n    schema = {\n        \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n        \"type\": \"object\",\n        \"properties\": {\n            \"a\": {\n                \"type\": \"number\"\n            },\n            \"b\": {\n                \"type\": \"number\"\n            }\n        },\n        \"required\": [\"a\",\"b\"]\n    }\n```\n\n\u547d\u4ee4\u884c\u4e2d\u9ed8\u8ba4\u4f7f\u7528`-c`/`--config`\u6765\u6307\u5b9a\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6,\u5b83\u7684\u8bfb\u53d6\u884c\u4e3a\u53d7\u4e0a\u9762\u4ecb\u7ecd\u7684\u4ece\u81ea\u5b9a\u4e49\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u7684\u8bbe\u7f6e\u5f71\u54cd.\n\n#### \u914d\u7f6e\u7684\u8bfb\u53d6\u987a\u5e8f\n\n\u914d\u7f6e\u7684\u8bfb\u53d6\u987a\u5e8f\u4e3a`schema\u4e2d\u5b9a\u4e49\u7684default\u503c`->`\u914d\u7f6e\u6307\u5b9a\u7684\u914d\u7f6e\u6587\u4ef6\u8def\u5f84`->`\u547d\u4ee4\u884c\u6307\u5b9a\u7684\u914d\u7f6e\u6587\u4ef6`->`\u73af\u5883\u53d8\u91cf`->`\u547d\u4ee4\u884c\u53c2\u6570`,\u800c\u8986\u76d6\u987a\u5e8f\u5219\u662f\u53cd\u8fc7\u6765.\n\n#### \u6ce8\u518c\u5165\u53e3\u7684\u6267\u884c\u51fd\u6570\n\n\u6211\u4eec\u4f7f\u7528\u5b9e\u4f8b\u7684\u88c5\u9970\u5668\u65b9\u6cd5`as_main`\u6765\u5b9e\u73b0\u5bf9\u6267\u884c\u8282\u70b9\u5165\u53e3\u51fd\u6570\u7684\u6ce8\u518c,\u6ce8\u518c\u7684\u5165\u53e3\u51fd\u6570\u4f1a\u5728\u89e3\u6790\u597d\u53c2\u6570\u540e\u6267\u884c,\u5176\u53c2\u6570\u5c31\u662f\u89e3\u6790\u597d\u7684`**config`\n\n```python\n\nroot = Test_A()\n@root.as_main\ndef main(a,b):\n    print(a)\n    print(b)\n\n```\n\n\u53e6\u4e00\u79cd\u6307\u5b9a\u5165\u53e3\u51fd\u6570\u7684\u65b9\u6cd5\u662f\u91cd\u5199\u5b50\u7c7b\u7684`do_main(self)->None`\u65b9\u6cd5\n\n```python\nclass Test_A(EntryPoint):\n    argparse_noflag = \"a\"\n    argparse_check_required=True\n    schema = {\n        \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n        \"type\": \"object\",\n        \"properties\": {\n            \"a\": {\n                \"type\": \"number\"\n            },\n            \"b\": {\n                \"type\": \"number\"\n            }\n        },\n        \"required\": [\"a\",\"b\"]\n    }\n    def do_main(self)->None:\n        print(self.config)\n```\n\n#### \u76f4\u63a5\u4ece\u8282\u70b9\u5bf9\u8c61\u4e2d\u83b7\u53d6\u914d\u7f6e\n\n\u8282\u70b9\u5bf9\u8c61\u7684`config`\u5c5e\u6027\u4f1a\u5728\u6bcf\u6b21\u8c03\u7528\u65f6copy\u4e00\u4efd\u5f53\u524d\u7684\u914d\u7f6e\u503c,config\u662f\u4e0d\u53ef\u5199\u7684.\n\n```python\nprint(root.config)\n```\n\n### \u4e2d\u95f4\u8282\u70b9\n\n\u4e2d\u95f4\u8282\u70b9\u5e76\u4e0d\u80fd\u6267\u884c\u7a0b\u5e8f,\u5b83\u53ea\u662f\u7528\u4e8e\u63cf\u8ff0\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u547d\u4ee4\u96c6\u5408,\u56e0\u6b64\u5b83\u7684\u4f5c\u7528\u5c31\u662f\u5145\u5f53`help`\u6307\u4ee4.\u6211\u4eec\u5b9a\u4e49\u4e2d\u95f4\u8282\u70b9\u5e76\u4e0d\u80fd\u6267\u884c.\u4f46\u5fc5\u987b\u6709\u81f3\u5c11\u4e00\u4e2a\u5b50\u8282\u70b9\u624d\u662f\u4e2d\u95f4\u8282\u70b9.\u56e0\u6b64\u5373\u4fbf\u4e00\u4e2a\u8282\u70b9\u5b9a\u4e49\u4e86\u4e0a\u9762\u7684\u914d\u7f6e,\u53ea\u8981\u5b83\u6709\u5b50\u8282\u70b9\u5c31\u4e0d\u4f1a\u6309\u4e0a\u9762\u7684\u6267\u884c\u6d41\u7a0b\u6267\u884c.\n\n\u5229\u7528\u4e2d\u95f4\u8282\u70b9\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u51fa\u975e\u5e38\u590d\u6742\u7684\u542f\u52a8\u547d\u4ee4\u6811.\n\n#### \u6ce8\u518c\u5b50\u8282\u70b9\n\n\u4e2d\u95f4\u8282\u70b9\u7684\u6ce8\u518c\u6709\u4e24\u4e2a\u63a5\u53e3\n\n+ `regist_subcmd`\u7528\u4e8e\u6ce8\u518c\u4e00\u4e2a\u5df2\u7ecf\u5b9e\u4f8b\u5316\u7684\u5b50\u8282\u70b9\n\n    ```python\n    class A(EntryPoint):\n        pass\n\n    class B(EntryPoint):\n        pass\n\n    a = A()\n\n    b = B()\n\n    a.regist_subcmd(b)\n    ```\n\n+ `regist_sub`\u7528\u4e8e\u6ce8\u518c\u4e00\u4e2a\u5b50\u8282\u70b9\u7c7b,\u5b83\u4f1a\u8fd4\u56de\u88ab\u6ce8\u518c\u7684\u8282\u70b9\u7684\u4e00\u4e2a\u5b9e\u4f8b\n\n    ```python\n    class A(EntryPoint):\n        pass\n\n    class B(EntryPoint):\n        pass\n\n    a = A()\n    b =a.regist_sub(B)\n    ```\n\nMIT License\n\nCopyright (c) 2020 Python-Tools\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "My package description",
    "version": "0.1.5",
    "project_urls": {
        "Homepage": "https://github.com/Python-Tools/schema_entry"
    },
    "split_keywords": [
        "config",
        "environment variable",
        "command line arguments",
        "config file"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5484d426d071b0697db8f5ab91b63023adca4e275f260d919e520777e0d35b85",
                "md5": "15052b28375fd7f0652423ecaaf69cc0",
                "sha256": "bbc23605500163efc3715d74abe7bae1fe6250a290b31fbd9f88174de57cf9c8"
            },
            "downloads": -1,
            "filename": "schema_entry-0.1.5-py3.9.egg",
            "has_sig": false,
            "md5_digest": "15052b28375fd7f0652423ecaaf69cc0",
            "packagetype": "bdist_egg",
            "python_version": "0.1.5",
            "requires_python": null,
            "size": 16927,
            "upload_time": "2021-04-20T02:47:59",
            "upload_time_iso_8601": "2021-04-20T02:47:59.010578Z",
            "url": "https://files.pythonhosted.org/packages/54/84/d426d071b0697db8f5ab91b63023adca4e275f260d919e520777e0d35b85/schema_entry-0.1.5-py3.9.egg",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1ffb66e1346dddfead2214679c608f470dbd7bc9d6ff302996767a6255fd641c",
                "md5": "ada52d59b23c435298ba5d9a31a7d284",
                "sha256": "776dc0a4da4528bac0ef267f1c3c90d9c6e6e168683c12af2e8cc89c39977cfd"
            },
            "downloads": -1,
            "filename": "schema_entry-0.1.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ada52d59b23c435298ba5d9a31a7d284",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 17794,
            "upload_time": "2021-04-20T02:47:57",
            "upload_time_iso_8601": "2021-04-20T02:47:57.442447Z",
            "url": "https://files.pythonhosted.org/packages/1f/fb/66e1346dddfead2214679c608f470dbd7bc9d6ff302996767a6255fd641c/schema_entry-0.1.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5f0e55ab8399a6cdbf1ad1f42824080a00274bee22c1a838042138eb1112c410",
                "md5": "5bd261df51941e03668be417511b5ca3",
                "sha256": "4c9d5cbdebccd1deec41c6c13b7a6070008c991072691049d4fcb18ea2638c21"
            },
            "downloads": -1,
            "filename": "schema_entry-0.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "5bd261df51941e03668be417511b5ca3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 21849,
            "upload_time": "2021-04-20T02:48:00",
            "upload_time_iso_8601": "2021-04-20T02:48:00.328510Z",
            "url": "https://files.pythonhosted.org/packages/5f/0e/55ab8399a6cdbf1ad1f42824080a00274bee22c1a838042138eb1112c410/schema_entry-0.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-04-20 02:48:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Python-Tools",
    "github_project": "schema_entry",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "schema-entry"
}
        
hsz
Elapsed time: 0.41130s