Pages

Tuesday, 30 May 2023

FauxCLI:从给定的 YAML 文件模拟命令行客户端



FauxCLI (发音 foak-ley) 从给定的 YAML 文件模拟一个命令行客户端。

安装:

$ go get github.com/nextrevision/fauxcli

在当前目录创建一个 cli.yaml 文件:

name: spiderpig
help: does whatever a spiderpig does
commands:
  - name: swing
    help: swings from a web
    output: |
      I can't do that, I'm a pig!
  - name: plop
    help: super secret maneuver
    output: |
      Look out!

运行 fauxcli:

$ fauxcli
does whatever a spiderpig does

Usage:
  spiderpig [command]

Available Commands:
  swing       swings from a web
  plop        super secret maneuver

Flags:
  -h, --help   help for spiderpig

Use "spiderpig [command] --help" for more information about a command.

子命令:

$ fauxcli swing
I can't do that, I'm a pig!

别名:

$ alias spiderpig='fauxcli'
$ spiderpig plop
Look out!

安装

go:

go get github.com/nextrevision/fauxcli

使用 GitHub 版本:

# OSX
curl -s -o /usr/local/bin/fauxcli https://github.com/nextrevision/fauxcli/releases/download/1.0.0/fauxcli_darwin_amd64

# Linux
curl -s -o /usr/local/bin/fauxcli https://github.com/nextrevision/fauxcli/releases/download/1.0.0/fauxcli_linux_amd64

chmod +x /usr/local/bin/fauxcli

cli.yaml

---
# (required) name of the command
name: mycliapp

# (required) the help text for the command (displayed with -h)
help: does something cool

# additional command aliases
aliases: ["myapp", "app"]

# output to print when the command is run
# if this key is omitted, the command will act as a
# parent to any subcommands, essentially doing nothing
# but printing the help text
output: |
  Hello, World!

# flags available to the command
flags:
    # (required) long name of the flag (--debug)
  - name: debug

    # (required) help text for the flag
    help: enables debugging

    # short name for the flag (-d)
    short: d

    # default value of the flag
    default: false

    # make the flag globally available
    global: true

    # the type of the value (default string)
    # available types:
    #   - string
    #   - bool
    #   - int
    #   - float
    type: bool

# subcommands (nested from all the options above)
commands:
  - name: subcommand1
    help: a subcommand
    flags:
      - name: upper
        help: converts output to uppercase
        short: u
        type: bool
    output: |
      {{ if .Flags.upper.Bool -}}
      HELLO FROM SC1!
      {{ else -}}
      Hello from SC1!
      {{ end -}}
  - name: subcommand2
    help: another subcommand with children
    commands:
      - name: child1
        help: the first child command
        output: |
          Hello from child1
      - name: child2
        help: the second child command
        output: |
          Hello from child2

FauxCLI 使用 Go 编写,托管在 GitHub:
https://github.com/nextrevision/fauxcli

No comments:

Post a Comment