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