github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/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.6 for /a/b/c/foo.s on amd64")
    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  )
    24  
    25  var (
    26  	D MultiFlag
    27  	I MultiFlag
    28  )
    29  
    30  func init() {
    31  	flag.Var(&D, "D", "predefined symbol with optional simple value -D=identifer=value; can be set multiple times")
    32  	flag.Var(&I, "I", "include directory; can be set multiple times")
    33  }
    34  
    35  // MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
    36  type MultiFlag []string
    37  
    38  func (m *MultiFlag) String() string {
    39  	return fmt.Sprint(*m)
    40  }
    41  
    42  func (m *MultiFlag) Set(val string) error {
    43  	(*m) = append(*m, val)
    44  	return nil
    45  }
    46  
    47  func Usage() {
    48  	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s\n")
    49  	fmt.Fprintf(os.Stderr, "Flags:\n")
    50  	flag.PrintDefaults()
    51  	os.Exit(2)
    52  }
    53  
    54  func Parse() {
    55  	flag.Usage = Usage
    56  	flag.Parse()
    57  	if flag.NArg() != 1 {
    58  		flag.Usage()
    59  	}
    60  
    61  	// Flag refinement.
    62  	if *OutputFile == "" {
    63  		input := filepath.Base(flag.Arg(0))
    64  		if strings.HasSuffix(input, ".s") {
    65  			input = input[:len(input)-2]
    66  		}
    67  		*OutputFile = fmt.Sprintf("%s.o", input)
    68  	}
    69  }