github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/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  	if len(*m) == 0 {
    40  		return ""
    41  	}
    42  	return fmt.Sprint(*m)
    43  }
    44  
    45  func (m *MultiFlag) Set(val string) error {
    46  	(*m) = append(*m, val)
    47  	return nil
    48  }
    49  
    50  func Usage() {
    51  	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s\n")
    52  	fmt.Fprintf(os.Stderr, "Flags:\n")
    53  	flag.PrintDefaults()
    54  	os.Exit(2)
    55  }
    56  
    57  func Parse() {
    58  	flag.Usage = Usage
    59  	flag.Parse()
    60  	if flag.NArg() != 1 {
    61  		flag.Usage()
    62  	}
    63  
    64  	// Flag refinement.
    65  	if *OutputFile == "" {
    66  		input := filepath.Base(flag.Arg(0))
    67  		if strings.HasSuffix(input, ".s") {
    68  			input = input[:len(input)-2]
    69  		}
    70  		*OutputFile = fmt.Sprintf("%s.o", input)
    71  	}
    72  }