Skip to content
A 200-line command-line parsing library
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
LICENSE Initial commit Nov 29, 2019
README.md
argparse.go

README.md

argparse

A 200-line command-line parsing library

blog: 200 行代码实现一个简单的命令行解析库

Installation

$ go get -u -v github.com/elliotxx/argparse

Usage

package main

import (
  "fmt"
  "github.com/elliotxx/argparse"
)

func main() {
    isr   := argparse.Bool("r", false, "Output text in reverse order")
    n     := argparse.Int("n", -1, "Output n lines")
    ish   := argparse.Bool("h", false, "Help information")
    isH   := argparse.Bool("help", false, "Help information")
    
    err := argparse.Parse()
    if err != nil {
        fmt.Println(err)
        return
    }

    if *ish || *isH {
        argparse.Help()
        return
    }
    fmt.Printf("isr=%v, n=%d\n", *isr, *n)
}

Ouput:

$ go run cat.go -h
Usage of cat
    -h,--help	bool	Help information
    -n	int	Output n lines
    -r	bool	Output text in reverse order
$ go run cat.go -nr 10
isr=true, n=10
You can’t perform that action at this time.