Python ApiDoc 工具手册中文译本

本文档详细介绍了Python ApiDoc工具的安装、使用方法、配置文件格式和源文件格式,包括命令行参数、配置文件示例、源文件结构等,帮助开发者更好地理解和使用ApiDoc生成API文档。
摘要由CSDN通过智能技术生成

一、快速开始

安装

使用Ubuntu环境,安装命令如下:

sudo apt-get install python3-pip
sudo pip3 install apidoc

apidoc依赖三个库,分别是:Jinja2==2.7.3、PyYAML==3.11、jsonschema==2.4.0

如果python3-pip不存在,还可以使用如下命令安装:

sudo apt-get install python3-setuptools
sudo easy_install3 pip
sudo pip3-2 install apidoc

示例

使用如下命令,运行一个简单的示例:

mkdir apidoc
cd apidoc
wget https://raw.github.com/SolutionsCloud/apidoc/master/example/demo/source.yml
apidoc -i source.yml -o output/index.html
firefox output/index.html

通过浏览器看到的效果如图:

二、使用方法

命令行参数

通过如下命令查看apidoc的完整命令行参数:apidoc -h

usage: apidoc [-h] [-c CONFIG] [-i DIRECTORY OR FILE [DIRECTORY OR FILE ...]]
              [-o FILE] [-v] [-n] [-a ARGUMENT [ARGUMENT ...]] [-y] [-w] [-q]
              [-qq] [-t]

Base command-line interface for ApiDoc

optional arguments:
  -h, --help            show this help message and exit
  -c CONFIG, --config CONFIG
                        configuration file
  -i DIRECTORY OR FILE [DIRECTORY OR FILE ...], --input DIRECTORY OR FILE [DIRECTORY OR FILE ...]
                        directories and/or files containing documentation's
                        source files
  -o FILE, --output FILE
                        rendered output file
  -v, --version         show program's version number and exit
  -n, --no-validate     disable validation
  -a ARGUMENT [ARGUMENT ...], --arguments ARGUMENT [ARGUMENT ...]
                        documentation's arguments arg1=value1 arg2=value2
  -y, --dry-run         analyse config's and source's files without building
                        the documentation
  -w, --watch           re-render the documentation each time a source's file
                        or a template's file changes
  -q, --quiet           does not display logging information below warning
                        level
  -qq, --silence        does not display any logging information
  -t, --traceback       display traceback when an exception raised

将上述命令行参数简单翻译就是:

  • -h 查看帮助信息

  • -c 指定配置文件

  • -i 指定输入,可以是目录或者文件

  • -o 指定输出,输出的只能是文件

  • -v 查看版本信息

  • -n 关闭验证

  • -a 指定文档的参数arg1=value1 arg2=value2

  • -y 仅对配置或源文件做分析不输出文档

  • -w 每次源文件或模板文件变化就重新生成文档

  • -q 告警级别以下的日志不显示

  • -qq 不显示任何日志

  • -t 如果异常则展示

基本参数

通过指定的源文件生成文档:

apidoc -i ./example/source_simple/simple.yml

通过多个源文件生成文档:

apidoc -i ./example/source_simple/one.yml ./example/source_simple/two.yml

通过包含源文件的目录生成文档:

apidoc -i ./example/source_simple/

通过指定配置文件生成文档:

apidoc -c ./example/config/config.yaml

组合上述参数生成文档:

apidoc -c ./config.yaml -i ./folder1/ ./folder2/ /folder3/file.yaml /folder3/file.json

仅分析源文件不生成文档:

apidoc -i ./example/source_simple/simple.yml -y

每次文件变化都自动重新生成文档:

apidoc -i ./example/souce_simple/simple.yml -w

显示更少的日志生成文档:

apidoc -i ./example/source_simple/simple.yml -q
apidoc -i ./example/source_simple/simple.yml -qq

显示异常生成文档:

apidoc -i ./example/source_simple/simple.yml -t

三、配置文件格式

ApiDoc可以通过命令行参数或者一个包含选项的配置文件来生成文档。配置文件的格式可以是YAML或者JSON,文件后缀必须是:.yaml、.yml、.json

示例

通过指定配置文件为参数来生成文档:

apidoc -c ./path-to-config.yaml

一个简单的配置文件示例

最简单的配置文件:

input:
  locations:
    - ./sources/one.yml
output:
  location:
    ./output/sample.html

最基本的配置文件:

input:
  locations:
    - ./sources
    - ./sources2/one.yml
  arguments:
    url: api.sfr.com
filter:
  versions:
    excludes:
      - v2
output:
  location: ./output/sample.html
  componants: local
  template: default

对上述参数进行详解:

input

input定义了源文件的位置,包含三个子选项:

  • locations:包含了文件列表、目录

  • validate:验证开关,验证源文件是否符合JSON格式验证

  • arguments:包含了源文件里所需的参数、变量

作为配置文件,源文件必须是上面提到的yaml、yml、json,当指定位置为目录时,目录下所有的源文件都会被生成文档,且配置文件至少包含一个指定的源文件。

一个完整的input例子:

input:
  locations:
    - ./project/api-sources
    - ../common-api/sources
    -
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值