Code Monkey home page Code Monkey logo

clt's Introduction

clt

描述

基于go语言的命令行工具,根据输入提示可执行程序的帮助信息,适合用于复杂业务的脚本辅助

使用效果

$ ./test help
commands server - connect everysites!

Usage:

    test command [arguments]

The commands are:

    cmd1        example server
    server      server http

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

$ ./test help cmd1
usage: test cmd1 [jackma | ponyma | start]

        jackma              i am jackma.
        ponyma              i am ponyma.
        start               start the server.

$ ./test cmd1 jackma
悔创阿里杰克马

如何使用

package main

import (
	"fmt"
	"os"

	"github.com/xpfyg/clt"
)

func main() {
	cmdServerV1.Run = serverControllerV1
	cmdServerV2.Run = serverControllerV2
	cmdRunner := clt.Commands{
		CommandList: []*clt.Command{
			cmdServerV1,
			cmdServerV2,
		},
		ApiName: "test",
	}

	cmdRunner.Run()
}

var cmdServerV1 = &clt.Command{
	UsageLine: "cmd1 [jackma | ponyma | start]",
	Short:     "example server",
	Long: `
	jackma              i am jackma.
	ponyma              i am ponyma.
	start               start the server.
	`,
}

var cmdServerV2 = &clt.Command{
	UsageLine: "server [start | stop | start-console]",
	Short:     "server http",
	Long: `
	start              start the server.
	stop               stop the server.
	start-console      start the server in console.
	`,
}

func serverControllerV1(cmd *clt.Command, args []string) {
	if len(args) != 1 {
		fmt.Println("what's your problem!")
		os.Exit(2)
	}

	switch args[0] {
	case "jackma":
		fmt.Println("悔创阿里杰克马")
	case "ponyma":
		fmt.Println("普通家庭马化腾")
	case "start":
		fmt.Println("开始你的表演")
	default:
		fmt.Println("what's your problem!")
	}
}

func serverControllerV2(cmd *clt.Command, args []string) {
	if len(args) != 1 {
		fmt.Println("what's your problem!")
		os.Exit(2)
	}

	switch args[0] {
	case "start":
		fmt.Println("server start")
	case "stop":
		fmt.Println("server stop")
	case "start-console":
		fmt.Println("server start in console")
	default:
		fmt.Println("what's your problem!")
	}
}

clt's People

Contributors

xpfyg avatar

Stargazers

zhoufei avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.