zoukankan      html  css  js  c++  java
  • Go Flag包-命令行参数解析

    Flag包用法

    package main
    
    import (
        "flag"
        "fmt"
    )
    
    func main() {
    
        var num int
        var mode string
        var encrypt bool
    
        // usage
        // cmd -flag
        // cmd --flag
        // cmd -flag=
        // cmd --flag=
    
        // 第一步,定义Flag参数
        flag.IntVar(&num, "num", 16, "-num the password length")
        //var num = flag.Int("num", 16, "-num the password length")
    
    
        flag.StringVar(&mode, "mode", "mix", "-mode the password generate mode")
        // var mode = flag.String("mode", "mix", "-mode the password generate mode")
    
        flag.BoolVar(&encrypt, "encrypt",true, "-encrypt the password use RSA")
        //var encrypt = flag.Bool("encrypt",true, "-encrypt the password use RSA")
    
    
        // 第二步,调用flag.Parse()解析命令行参数到定义的Flag
        flag.Parse()
    
        // 调用Parse解析后,就可以直接使用绑定的变量了
        fmt.Printf("num:%d mode:%s encrypt:%v
    ", num, mode, encrypt)
    
    }
  • 相关阅读:
    课后作业5
    类与对象动手动脑
    动手动脑
    找“水王”
    NABCD
    第七周学习进度
    web网页四则运算
    二维数组最大联通子数组求和
    第六周学习进度
    环状数组最大子数组求和
  • 原文地址:https://www.cnblogs.com/vincenshen/p/9426690.html
Copyright © 2011-2022 走看看