feat: add file input flag #12

Merged
mvhutz merged 2 commits from feat/file-input-flag into main 2026-01-11 21:43:12 +00:00
3 changed files with 32 additions and 13 deletions

View File

@@ -20,13 +20,13 @@ build:
chmod +x ${BINARY_NAME} chmod +x ${BINARY_NAME}
run: build run: build
./${BINARY_NAME} - < ./samples/$(TEST).txt > program.out ./${BINARY_NAME} -f ./samples/$(TEST).txt > program.out
profile: build profile: build
./${BINARY_NAME} -p profile/cpu.prof - < ./samples/$(TEST).txt > program.out ./${BINARY_NAME} -p profile/cpu.prof -f ./samples/$(TEST).txt > program.out
explain: build explain: build
./${BINARY_NAME} -x -p profile/cpu.prof - < ./samples/$(TEST).txt > program.out ./${BINARY_NAME} -x -p profile/cpu.prof -f ./samples/$(TEST).txt > program.out
graph: graph:
go tool pprof -raw -output=profile/cpu.raw profile/cpu.prof go tool pprof -raw -output=profile/cpu.raw profile/cpu.prof

View File

@@ -12,22 +12,29 @@ func FromArgs() (*Config, error) {
explanation := flag.Bool("x", false, "Explanation. Whether or not to show all reduction steps.") explanation := flag.Bool("x", false, "Explanation. Whether or not to show all reduction steps.")
statistics := flag.Bool("s", false, "Statistics. If set, the process will print various statistics about the run.") statistics := flag.Bool("s", false, "Statistics. If set, the process will print various statistics about the run.")
profile := flag.String("p", "", "CPU profiling. If an output file is defined, the program will profile its execution and dump its results into it.") profile := flag.String("p", "", "CPU profiling. If an output file is defined, the program will profile its execution and dump its results into it.")
file := flag.String("f", "", "File. If set, read source from the specified file.")
flag.Parse() flag.Parse()
// There must only be one input argument.
if flag.NArg() == 0 {
return nil, fmt.Errorf("no input given")
} else if flag.NArg() > 1 {
return nil, fmt.Errorf("more than 1 command-line argument")
}
// Parse source type. // Parse source type.
var source Source var source Source
if *file != "" {
// File flag takes precedence.
if flag.NArg() > 0 {
return nil, fmt.Errorf("cannot specify both -f flag and positional argument")
}
source = FileSource{Path: *file}
} else if flag.NArg() == 0 {
return nil, fmt.Errorf("no input given")
} else if flag.NArg() > 1 {
return nil, fmt.Errorf("more than 1 command-line argument")
} else {
// Positional argument.
if flag.Arg(0) == "-" { if flag.Arg(0) == "-" {
source = StdinSource{} source = StdinSource{}
} else { } else {
source = StringSource{Data: flag.Arg(0)} source = StringSource{Data: flag.Arg(0)}
} }
}
return &Config{ return &Config{
Source: source, Source: source,

View File

@@ -27,3 +27,15 @@ func (s StdinSource) Extract() (string, error) {
return string(data), nil return string(data), nil
} }
// A source reading from a file.
type FileSource struct{ Path string }
func (s FileSource) Extract() (string, error) {
data, err := os.ReadFile(s.Path)
if err != nil {
return "", err
}
return string(data), nil
}