gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/basename/basename.go (about)

     1  // Copyright 2012-2018 the u-root 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  // Basename return name with leading path information removed.
     6  //
     7  // Synopsis:
     8  //     basename NAME [SUFFIX]
     9  //
    10  package main
    11  
    12  import (
    13  	"fmt"
    14  	"log"
    15  	"path/filepath"
    16  	"strings"
    17  
    18  	flag "github.com/spf13/pflag"
    19  )
    20  
    21  func usage() {
    22  	log.Fatal("Usage: basename NAME [SUFFIX]")
    23  }
    24  
    25  func main() {
    26  	flag.Parse()
    27  
    28  	args := flag.Args()
    29  	switch len(args) {
    30  	case 2:
    31  		fileName := filepath.Base(args[0])
    32  		if fileName != args[1] {
    33  			fileName = strings.TrimSuffix(fileName, args[1])
    34  		}
    35  		fmt.Println(fileName)
    36  	case 1:
    37  		fileName := filepath.Base(args[0])
    38  		fmt.Println(fileName)
    39  	default:
    40  		usage()
    41  	}
    42  }