github.com/itchyny/rassemble-go@v0.1.1/cmd/rassemble/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"runtime"
     9  
    10  	"github.com/itchyny/rassemble-go"
    11  )
    12  
    13  const name = "rassemble"
    14  
    15  const version = "0.1.1"
    16  
    17  var revision = "HEAD"
    18  
    19  func main() {
    20  	os.Exit(run(os.Args[1:]))
    21  }
    22  
    23  const (
    24  	exitCodeOK = iota
    25  	exitCodeErr
    26  )
    27  
    28  func run(args []string) int {
    29  	fs := flag.NewFlagSet(name, flag.ContinueOnError)
    30  	fs.SetOutput(os.Stderr)
    31  	fs.Usage = func() {
    32  		fs.SetOutput(os.Stdout)
    33  		fmt.Printf(`%[1]s - assemble regular expressions
    34  
    35  Version: %s (rev: %s/%s)
    36  
    37  Synopsis:
    38    %% %[1]s re1 re2 ...
    39  
    40  Options:
    41  `, name, version, revision, runtime.Version())
    42  		fs.PrintDefaults()
    43  	}
    44  	var showVersion bool
    45  	fs.BoolVar(&showVersion, "version", false, "print version")
    46  	if err := fs.Parse(args); err != nil {
    47  		if err == flag.ErrHelp {
    48  			return exitCodeOK
    49  		}
    50  		return exitCodeErr
    51  	}
    52  	if showVersion {
    53  		fmt.Printf("%s %s (rev: %s/%s)\n", name, version, revision, runtime.Version())
    54  		return exitCodeOK
    55  	}
    56  	if len(args) == 0 {
    57  		s := bufio.NewScanner(os.Stdin)
    58  		for s.Scan() {
    59  			args = append(args, s.Text())
    60  		}
    61  		if err := s.Err(); err != nil {
    62  			fmt.Fprintf(os.Stderr, "%s: %s\n", name, err)
    63  			return exitCodeErr
    64  		}
    65  	}
    66  	pattern, err := rassemble.Join(args)
    67  	if err != nil {
    68  		fmt.Fprintf(os.Stderr, "%s: %s\n", name, err)
    69  		return exitCodeErr
    70  	}
    71  	fmt.Println(pattern)
    72  	return exitCodeOK
    73  }