github.com/square/finch@v0.0.0-20240412205204-6530c03e2b96/boot/cmdline.go (about)

     1  // Copyright 2023 Block, Inc.
     2  
     3  package boot
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/alexflint/go-arg"
     9  
    10  	"github.com/square/finch"
    11  )
    12  
    13  // Options represents the command line options
    14  type Options struct {
    15  	Client     string `arg:"env:FINCH_CLIENT"`
    16  	CPUProfile string `arg:"--cpu-profile,env:FINCH_CPU_PROFILE"`
    17  	Database   string `arg:"-D,--database,env:FINCH_DB"`
    18  	Debug      bool   `arg:"env:FINCH_DEBUG"`
    19  	DSN        string `arg:"env:FINCH_DSN"`
    20  	Help       bool
    21  	Params     []string `arg:"-p,--param,separate"`
    22  	Server     string   `arg:"env:FINCH_SERVER"`
    23  	Test       bool     `arg:"env:FINCH_TEST"`
    24  	Version    bool
    25  }
    26  
    27  type CommandLine struct {
    28  	Options
    29  	Args []string `arg:"positional"`
    30  }
    31  
    32  func ParseCommandLine(args []string) (CommandLine, error) {
    33  	var c CommandLine
    34  	p, err := arg.NewParser(arg.Config{Program: "finch"}, &c)
    35  	if err != nil {
    36  		return c, err
    37  	}
    38  	if err := p.Parse(args); err != nil {
    39  		switch err {
    40  		case arg.ErrHelp:
    41  			c.Help = true
    42  		case arg.ErrVersion:
    43  			c.Version = true
    44  		default:
    45  			return c, fmt.Errorf("Error parsing command line: %s\n", err)
    46  		}
    47  	}
    48  	return c, nil
    49  }
    50  
    51  func printHelp() {
    52  	fmt.Printf("Usage:\n"+
    53  		"  finch [options] STAGE_1_FILE [STAGE_N_FILE...]\n\n"+
    54  		"Options:\n"+
    55  		"  --client ADDR[:PORT]  Run as client of server at ADDR\n"+
    56  		"  --cpu-profile FILE    Save CPU profile of stage execution to FILE\n"+
    57  		"  --database (-D) DB    Default database on connect\n"+
    58  		"  --debug               Print debug output to stderr\n"+
    59  		"  --dsn DSN             MySQL DSN (overrides stage files)\n"+
    60  		"  --help                Print help and exit\n"+
    61  		"  --param (-p) KEY=VAL  Set param key=value (override stage files)\n"+
    62  		"  --server ADDR[:PORT]  Run as server on ADDR\n"+
    63  		"  --test                Validate stages, test connections, and exit\n"+
    64  		"  --version             Print version and exit\n"+
    65  		"\n"+
    66  		"Docs:\n"+
    67  		"  https://square.github.io/finch/\n\n"+
    68  		"finch %s\n",
    69  		finch.VERSION,
    70  	)
    71  }