From d6cd6916eadadd4339eae9e90b8124913ad61605 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Tue, 5 Apr 2022 18:00:15 +0300 Subject: [PATCH] Add @file flag parsing syntax --- main.go | 18 ++++++++++++++++++ test-all.sh | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/main.go b/main.go index 9c9afda7..d9429a1a 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ package main import ( "fmt" "os" + "strings" "github.com/boyter/scc/v3/processor" "github.com/spf13/cobra" @@ -16,6 +17,23 @@ func main() { //pprof.StartCPUProfile(f) //defer pprof.StopCPUProfile() + if len(os.Args) == 2 && strings.HasPrefix(os.Args[1], "@") { + // handle "scc @flags.txt" syntax + filepath := strings.TrimPrefix(os.Args[1], "@") + b, err := os.ReadFile(filepath) + if err != nil { + fmt.Printf("Error reading flags from a file: %s\n", err) + os.Exit(1) + } + + args := strings.Split(string(b), "\n") + var newArgs []string + for _, x := range args { + newArgs = append(newArgs, strings.TrimSpace(x)) + } + os.Args = append([]string{os.Args[0]}, newArgs...) + } + rootCmd := &cobra.Command{ Use: "scc [flags] [files or directories]", Short: "scc [files or directories]", diff --git a/test-all.sh b/test-all.sh index c8e9faca..faa39ebd 100755 --- a/test-all.sh +++ b/test-all.sh @@ -1,5 +1,8 @@ #!/bin/bash +# make sure this script can be executed from any dir +cd $(dirname $0) + GREEN='\033[1;32m' RED='\033[0;31m' NC='\033[0m' @@ -28,6 +31,24 @@ echo '```' > LANGUAGES.md ./scc --languages >> LANGUAGES.md echo '```' >> LANGUAGES.md + +echo "Running with @file flag parsing syntax" +# include \n, \r\n and no line terminators +echo -e "go.mod\ngo.sum\r\nLICENSE" > flags.txt +if ./scc @flags.txt ; then + echo -e "${GREEN}PASSED @file flag syntax" + # post processing + rm flags.txt +else + echo -e "${RED}=======================================================" + echo -e "FAILED Should handle @file flag parsing syntax" + echo -e "=======================================================${NC}" + # post processing + rm flags.txt + exit +fi + + echo "Building HTML report..." ./scc --format html -o SCC-OUTPUT-REPORT.html