go 语言命令行程序

Posted zeroArn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go 语言命令行程序相关的知识,希望对你有一定的参考价值。

// lab3 project main.go
// minglinghang project main.go
package main

import (
    "fmt"
    "math"
    "os"
    "strings"
    "unsafe"
)

func add() {
    var firstNum, secondNum int

    //fmt.Println("Please input two integers: ")
    fmt.Scanln(&firstNum, &secondNum)

    fmt.Printf("%d + %d = %d\n", firstNum, secondNum, firstNum+secondNum)
}

func help() {
    ShowAllCommand(list)
}

func sub() {
    var firstNum, secondNum int

    //fmt.Println("Please input two integers: ")
    fmt.Scanln(&firstNum, &secondNum)

    fmt.Printf("%d - %d = %d\n", firstNum, secondNum, firstNum-secondNum)
}

func mul() {
    var firstNum, secondNum int

    //fmt.Println("Please input two integers: ")
    fmt.Scanln(&firstNum, &secondNum)

    fmt.Printf("%d * %d = %d\n", firstNum, secondNum, firstNum*secondNum)
}

func div() {
    var firstNum, secondNum int

    //fmt.Println("Please input two integers: ")
    fmt.Scanln(&firstNum, &secondNum)

    fmt.Printf("%d / %d = %.2f\n", firstNum, secondNum, float64(firstNum)/float64(secondNum))
}

func pow() {
    var firstNum, secondNum int

    //fmt.Println("Please input two integers: ")
    fmt.Scanln(&firstNum, &secondNum)

    fmt.Printf("%d ^ %d = %.0f\n", firstNum, secondNum, math.Pow(float64(firstNum), float64(secondNum)))
}

func mod() {
    var firstNum, secondNum int

    //fmt.Println("Please input two integers: ")
    fmt.Scanln(&firstNum, &secondNum)

    fmt.Printf("%d mod %d = %d\n", firstNum, secondNum, firstNum%secondNum)
}

func quit() {
    os.Exit(1)
}

type DataNode struct {
    next *DataNode
    cmd string
    desc string
    handler func()
}

type linknode struct {
    next *linknode
}

type linklist struct {
    head *linknode
    tail *linknode
}

func CreateLinklist() *linklist {
    return &linklist{}
}

func AddNode(list *linklist, newnode *linknode) {
    if list.head == nil {
        list.head = newnode
        list.tail = list.head
    } else {
        list.tail.next = newnode
        list.tail = newnode
    }
}
func ShowAllCommand(list *linklist) {
    for node := list.head; node != nil; node = node.next {
        tmp := (*DataNode)(unsafe.Pointer(node))
        fmt.Printf("%s - %s\n", tmp.cmd, tmp.desc)
    }
}

func searchcondition(node *DataNode, cmd interface{}) bool {
    str := cmd.(string)

    //if strings.Compare(str, node.cmd) {
    if node.cmd == str {
        return true
    } else {
        return false
    }
}
func search(list *linklist, cmd string) *DataNode {
    for node := list.head; node != nil; node = node.next {
        tmp := (*DataNode)(unsafe.Pointer(node))
        if searchcondition(tmp, cmd) {
            return tmp
        }
    }
    return (*DataNode)(unsafe.Pointer(list.head))
}
func Init(list *linklist) {
    menuconfig(list, "help", "this is help cmd", help)
    menuconfig(list, "add", "this is add cmd", add)
    menuconfig(list, "sub", "this is sub cmd", sub)
    menuconfig(list, "mul", "this is mul cmd", mul)
    menuconfig(list, "div", "this is div cmd", div)
    menuconfig(list, "mod", "this is mod cmd", mod)
    menuconfig(list, "pow", "this is pow cmd", pow)
    menuconfig(list, "quit", "this is quit cmd", quit)
    /*node := &DataNode{nil, "help", "this is help cmd", help}
    AddNode(list, (*linknode)(unsafe.Pointer(node)))
    node := &DataNode{nil, "add", "this is add cmd", add}
    AddNode(list, (*linknode)(unsafe.Pointer(node)))
    node = &DataNode{nil, "sub", "this is sub cmd", sub}
    AddNode(list, (*linknode)(unsafe.Pointer(node)))
    node = &DataNode{nil, "mul", "this is mul cmd", mul}
    AddNode(list, (*linknode)(unsafe.Pointer(node)))
    node = &DataNode{nil, "div", "this is div cmd", div}
    AddNode(list, (*linknode)(unsafe.Pointer(node)))
    node = &DataNode{nil, "mod", "this is mod cmd", mod}
    AddNode(list, (*linknode)(unsafe.Pointer(node)))
    node = &DataNode{nil, "pow", "this is pow cmd", pow}
    AddNode(list, (*linknode)(unsafe.Pointer(node)))
    node = &DataNode{nil, "quit", "this is squit cmd", quit}
    AddNode(list, (*linknode)(unsafe.Pointer(node)))
    */
}

func menuconfig(list *linklist, cmd string, desc string, handler func()) {
    node := &DataNode{nil, cmd, desc, handler}
    AddNode(list, (*linknode)(unsafe.Pointer(node)))
}

const CMD_LENTH = 10

func Excutemenu(list *linklist) {
    for {
        var argc int = 0
        var cmdi string
        var arg [5]string
        fmt.Println("Input a cmd number >")
        fmt.Scanf("%s", &cmdi)
        var cmdis []string = strings.Split(cmdi, " ")
        for (argc < len(cmdis)) && argc < CMD_LENTH {
            arg[argc] = cmdis[argc]
            argc = argc + 1
        }
        node := search(list, arg[0])
        if node != nil {
            node.handler()
        } else {
            fmt.Printf("this is a wrong cmd ")
        }
    }

}

var list *linklist

func main() {
    list = CreateLinklist()
    Init(list)
    Excutemenu(list)

}

以上是关于go 语言命令行程序的主要内容,如果未能解决你的问题,请参考以下文章

Go语言---小白入门-命令行库Cobra的使用

Go语言命令行利器cobra使用教程

Go语言-批量删除文件的命令行程序delete_tool.exe

Go语言命令行参数

Go语言命令行参数

Go语言-获取命令行参数