github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/cmd/asm/internal/flags/flags.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package flags implements top-level flags and the usage message for the assembler.
     6  package flags
     7  
     8  import (
     9  	"flag"
    10  	"fmt"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  )
    15  
    16  var (
    17  	Debug      = flag.Bool("debug", false, "dump instructions as they are parsed")
    18  	OutputFile = flag.String("o", "", "output file; default foo.o for /a/b/c/foo.s as first argument")
    19  	PrintOut   = flag.Bool("S", false, "print assembly and machine code")
    20  	TrimPath   = flag.String("trimpath", "", "remove prefix from recorded source file paths")
    21  	Shared     = flag.Bool("shared", false, "generate code that can be linked into a shared library")
    22  	Dynlink    = flag.Bool("dynlink", false, "support references to Go symbols defined in other shared libraries")
    23  	AllErrors  = flag.Bool("e", false, "no limit on number of errors reported")
    24  )
    25  
    26  var (
    27  	D MultiFlag
    28  	I MultiFlag
    29  )
    30  
    31  func init() {
    32  	flag.Var(&D, "D", "predefined symbol with optional simple value -D=identifier=value; can be set multiple times")
    33  	flag.Var(&I, "I", "include directory; can be set multiple times")
    34  }
    35  
    36  // MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
    37  type MultiFlag []string
    38  
    39  func (m *MultiFlag) String() string {
    40  	if len(*m) == 0 {
    41  		return ""
    42  	}
    43  	return fmt.Sprint(*m)
    44  }
    45  
    46  func (m *MultiFlag) Set(val string) error {
    47  	(*m) = append(*m, val)
    48  	return nil
    49  }
    50  
    51  func Usage() {
    52  	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s ...\n")
    53  	fmt.Fprintf(os.Stderr, "Flags:\n")
    54  	flag.PrintDefaults()
    55  	os.Exit(2)
    56  }
    57  
    58  func Parse() {
    59  	flag.Usage = Usage
    60  	flag.Parse()
    61  	if flag.NArg() == 0 {
    62  		flag.Usage()
    63  	}
    64  
    65  	// Flag refinement.
    66  	if *OutputFile == "" {
    67  		if flag.NArg() != 1 {
    68  			flag.Usage()
    69  		}
    70  		input := filepath.Base(flag.Arg(0))
    71  		if strings.HasSuffix(input, ".s") {
    72  			input = input[:len(input)-2]
    73  		}
    74  		*OutputFile = fmt.Sprintf("%s.o", input)
    75  	}
    76  }