zoukankan      html  css  js  c++  java
  • golang命令行参数解析

    1. os基础处理

    os包中有一个string类型的切片变量os.Args,其用来处理一些基本的命令行参数,它在程序启动后读取命令行输入的参数。参数会放置在切片os.Args[]中(以空格分隔),从索引1开始(os.Args[0]放的是程序本身的名字)。

    fmt.Println("Parameters:", os.Args[1:])

    2. flag参数解析

    flag包可以用来解析命令行选项,但通常被用来替换基本常量。例如,在某些情况下希望在命令行给常量一些不一样的值。

    type Flag struct {
        Name     string // name as it appears on command line
        Usage    string // help message
        Value    Value  // value as set
        DefValue string // default value (as text); for usage message
    }

    flag的使用规则是:首先定义flag(定义的flag会被解析),然后使用Parse()解析flag,解析后已定义的flag可以直接使用,未定义的剩余的flag可通过Arg(i)单独获取或通过Args()切片整个获取。

    定义flag

    func String(name string, value string, usage string) *string
    func StringVar(p *string, name string, value string, usage string)
    func Int(name string, value int, usage string) *int
    func IntVar(p *int, name string, value int, usage string)

    解析flag

    func Parse()

    Parse() parses the command-line flags from os.Args[1:]. Must be called after all flags are defined and before flags are accessed by the program.

    func Arg(i int) string
    func Args() []string

    Arg returns the i'th command-line argument. Arg(0) is the first remaining argument after flags have been processed.

    Args returns the non-flag command-line arguments.

    After parsing, the arguments following the flags are available as the slice flag.Args() or individually as flag.Arg(i). The arguments are indexed from 0 through flag.NArg()-1.

    func NArg() int

    NArg is the number of arguments remaining after flags have been processed.

    Flags may then be used directly. If you're using the flags themselves, they are all pointers; if you bind to variables, they're values.

    package main
    import (
            "fmt"
            "flag"
    )
    
    func main(){
            var new_line = flag.Bool("n", false, "new line")
            var max_num int
            flag.IntVar(&max_num, "MAX_NUM", 100, "the num max")
    
            flag.PrintDefaults()
            flag.Parse()
    
            fmt.Println("There are", flag.NFlag(), "remaining args, they are:", flag.Args())
            fmt.Println("n has value: ", *new_line)
            fmt.Println("MAX_NJUM has value: ", max_num)
    }
    $ go build -o flag flag.go
    $ ./flag
      -MAX_NUM int
            the num max (default 100)
      -n    new line
    There are 0 remaining args, they are: []
    n has value:  false
    MAX_NJUM has value:  100
    $ ./flag -n -MAX_NUM=1000 wang qin
      -MAX_NUM int
            the num max (default 100)
      -n    new line
    There are 2 remaining args, they are: [wang qin]
    n has value:  true
    MAX_NJUM has value:  1000
  • 相关阅读:
    [LeetCode] 146. LRU Cache(LRU 缓存)
    [LeetCode] 55. Jump Game(跳跃游戏)
    [LeetCode] 33. Search in Rotated Sorted Array(搜索旋转有序数组)
    [LeetCode] 19. Remove Nth Node From End of List(从单链表中移除倒数第 n 个节点)
    [LeetCode] 79. Word Search(单词查找)
    [LeetCode] 322. Coin Change(换硬币)
    [LeetCode] 34. Find First and Last Position of Elements in Sorted Array(在有序数组中寻找某个元素第一次和最后一次出现的位置)
    第04组 Alpha冲刺(2/6)
    第04组 Alpha冲刺(1/6)
    第04组 团队Git现场编程实战
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/11619290.html
Copyright © 2011-2022 走看看