Back to Go Swagger

Generate a CLI client

docs/generate/cli.md

0.35.310.5 KB
Original Source

Generate a CLI (command line tool) from swagger spec

This toolkit can generate a CLI to interact with your server

Features of generated app

  • auto-completion for bash, zsh, fish and powershell.
  • use config file to specify common flags.
  • each param and each field in body has a cli flag. etc.

CLI usage

Usage:
  swagger [OPTIONS] generate cli [cli-OPTIONS]

generate a command line client tool from the swagger spec

Application Options:
  -q, --quiet                                                                                silence logs
      --log-output=LOG-FILE                                                                  redirect logs to file

Help Options:
  -h, --help                                                                                 Show this help message

[cli command options]
      -c, --client-package=                                                                  the package to save the client
                                                                                             specific code (default: client)
      -P, --principal=                                                                       the model to use for the security
                                                                                             principal
          --default-scheme=                                                                  the default scheme for this API
                                                                                             (default: http)
          --principal-is-interface                                                           the security principal provided is an
                                                                                             interface, not a struct
          --default-produces=                                                                the default mime type that API
                                                                                             operations produce (default:
                                                                                             application/json)
          --default-consumes=                                                                the default mime type that API
                                                                                             operations consume (default:
                                                                                             application/json)
          --skip-models                                                                      no models will be generated when this
                                                                                             flag is specified
          --skip-operations                                                                  no operations will be generated when
                                                                                             this flag is specified
      -A, --name=                                                                            the name of the application, defaults
                                                                                             to a mangled value of info.title
          --cli-app-name=                                                                    the app name for the cli executable.
                                                                                             useful for go install. (default: cli)
          --cli-package=                                                                     the package to save the cli specific
                                                                                             code (default: cli)

    Options common to all code generation commands:
          --with-expand                                                                      expands all $ref's in spec prior to generation (shorthand
                                                                                             to --with-flatten=expand)
          --with-flatten=[minimal|full|expand|verbose|noverbose|remove-unused|keep-names]    flattens all $ref's in spec prior to generation (default:
                                                                                             minimal, verbose)
          --with-custom-formatter                                                            use faster custom contributed go import processing
                                                                                             instead of the standard one
      -f, --spec=                                                                            the spec file to use (default swagger.{json,yml,yaml})
      -t, --target=                                                                          the base directory for generating the files (default: ./)
          --template=[stratoscale]                                                           load contributed templates
      -T, --template-dir=                                                                    alternative template override directory
      -C, --config-file=                                                                     configuration file to use for overriding template options
      -r, --copyright-file=                                                                  copyright file used to add copyright header
          --additional-initialism=                                                           consecutive capitals that should be considered intialisms
          --allow-template-override                                                          allows overriding protected templates
          --skip-validation                                                                  skips validation of spec prior to generation
          --dump-data                                                                        when present dumps the json for the template generator
                                                                                             instead of generating files
          --strict-responders                                                                Use strict type for the handler return value
      -e, --return-errors                                                                    handlers explicitly return an error as the second value
          --restricted                                                                       Use restricted http client for remote $ref
          --rooted=                                                                          Local $ref resolution contained relative to root FS
      -p, --template-plugin=                                                                 the template plugin to use

    Options for model generation:
      -m, --model-package=                                                                   the package to save the models
                                                                                             (default: models)
      -M, --model=                                                                           specify a model to include in
                                                                                             generation, repeat for multiple
                                                                                             (defaults to all)
          --existing-models=                                                                 use pre-generated models e.g.
                                                                                             github.com/foobar/model
          --strict-additional-properties                                                     disallow extra properties when
                                                                                             additionalProperties is set to false
          --keep-spec-order                                                                  keep schema properties order identical
                                                                                             to spec file
          --struct-tags=                                                                     the struct tags to generate, repeat
                                                                                             for multiple (defaults to json)
          --rooted-error-path                                                                extends validation errors with the
                                                                                             type name instead of an empty path, in
                                                                                             the case of arrays and maps

    Options for operation generation:
      -O, --operation=                                                                       specify an operation to include,
                                                                                             repeat for multiple (defaults to all)
          --tags=                                                                            the tags to include, if not specified
                                                                                             defaults to all
      -a, --api-package=                                                                     the package to save the operations
                                                                                             (default: operations)
          --with-enum-ci                                                                     allow case-insensitive enumerations
          --skip-tag-packages                                                                skips the generation of tag-based
                                                                                             operation packages, resulting in a
                                                                                             flat generation 

Build a CLI

There is an example cli and tutorial provided at: https://github.com/go-swagger/examples/tree/master/cli

To generate a CLI:

swagger generate cli -f [http-url|filepath] --cli-app-name [app-name]

Cli is a wrapper of generated client code (see client for details), so all client generation options are honored.

To build the generated CLI code:

go build cmd/<app-name>/main.go 

Or install in your go/bin

go install cmd/<app-name>/main.go

See details of the generated app help message for usage

<app name> help

A more detailed/complicated example is generated CLI for docker engine: https://github.com/go-swagger/dockerctl