fix flag parser
All checks were successful
judge / Build judge (push) Successful in 9s
judge / Linux / gcc / Debug (push) Successful in 10s
judge / Linux / gcc / Release (push) Successful in 11s
judge / Linux / clang / Release (push) Successful in 11s
judge / Linux / clang / Sanitized (push) Successful in 10s
judge / Linux / gcc / Sanitized (push) Successful in 10s
judge / Linux / gcc / Debug (valgrind) (push) Successful in 23s
judge / Windows / clang / Debug (push) Successful in 46s
judge / Windows / clang / Release (push) Successful in 44s
judge / Windows / msvc / Debug (push) Successful in 48s
judge / Windows / msvc / Release (push) Successful in 48s
judge / SUMMARY (push) Successful in 4s
All checks were successful
judge / Build judge (push) Successful in 9s
judge / Linux / gcc / Debug (push) Successful in 10s
judge / Linux / gcc / Release (push) Successful in 11s
judge / Linux / clang / Release (push) Successful in 11s
judge / Linux / clang / Sanitized (push) Successful in 10s
judge / Linux / gcc / Sanitized (push) Successful in 10s
judge / Linux / gcc / Debug (valgrind) (push) Successful in 23s
judge / Windows / clang / Debug (push) Successful in 46s
judge / Windows / clang / Release (push) Successful in 44s
judge / Windows / msvc / Debug (push) Successful in 48s
judge / Windows / msvc / Release (push) Successful in 48s
judge / SUMMARY (push) Successful in 4s
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/Mond1c/judge/dsl"
|
"github.com/Mond1c/judge/dsl"
|
||||||
"github.com/Mond1c/judge/reporter"
|
"github.com/Mond1c/judge/reporter"
|
||||||
@@ -29,30 +29,30 @@ Example:
|
|||||||
`
|
`
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) >= 2 && os.Args[1] == "aggregate" {
|
args := os.Args[1:]
|
||||||
runAggregate()
|
|
||||||
|
if len(args) == 0 || hasFlag(args, "--help") || hasFlag(args, "-h") {
|
||||||
|
fmt.Print(usage)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) >= 1 && args[0] == "aggregate" {
|
||||||
|
runAggregate(args[1:])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fs := flag.NewFlagSet("judge", flag.ContinueOnError)
|
jsonOutput := hasFlag(args, "--json")
|
||||||
fs.SetOutput(os.Stderr)
|
wrapper := flagValue(args, "--wrapper")
|
||||||
fs.Usage = func() { fmt.Fprint(os.Stderr, usage) }
|
binary := flagValue(args, "--binary")
|
||||||
|
positional := positionalArgs(args)
|
||||||
|
|
||||||
jsonOutput := fs.Bool("json", false, "output as JSON")
|
if len(positional) < 2 {
|
||||||
wrapper := fs.String("wrapper", "", "exec wrapper command")
|
|
||||||
binary := fs.String("binary", "", "binary name override")
|
|
||||||
|
|
||||||
if err := fs.Parse(os.Args[1:]); err != nil {
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
if fs.NArg() < 2 {
|
|
||||||
fmt.Fprintf(os.Stderr, "error: need <tests.jdg> and <solution-dir>\n\n%s", usage)
|
fmt.Fprintf(os.Stderr, "error: need <tests.jdg> and <solution-dir>\n\n%s", usage)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
testFile := fs.Arg(0)
|
testFile := positional[0]
|
||||||
solutionDir := fs.Arg(1)
|
solutionDir := positional[1]
|
||||||
|
|
||||||
src, err := os.ReadFile(testFile)
|
src, err := os.ReadFile(testFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -73,12 +73,12 @@ func main() {
|
|||||||
|
|
||||||
r := runner.New(f, runner.Config{
|
r := runner.New(f, runner.Config{
|
||||||
WorkDir: solutionDir,
|
WorkDir: solutionDir,
|
||||||
BinaryName: *binary,
|
BinaryName: binary,
|
||||||
Wrapper: *wrapper,
|
Wrapper: wrapper,
|
||||||
})
|
})
|
||||||
result := r.Run()
|
result := r.Run()
|
||||||
|
|
||||||
if *jsonOutput {
|
if jsonOutput {
|
||||||
if err := reporter.JSON(os.Stdout, result); err != nil {
|
if err := reporter.JSON(os.Stdout, result); err != nil {
|
||||||
fatalf("json output error: %v", err)
|
fatalf("json output error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -91,12 +91,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runAggregate() {
|
func runAggregate(args []string) {
|
||||||
if len(os.Args) < 3 {
|
if len(args) < 1 {
|
||||||
fatalf("usage: judge aggregate <reports-dir>")
|
fatalf("usage: judge aggregate <reports-dir>")
|
||||||
}
|
}
|
||||||
dir := os.Args[2]
|
if err := reporter.Aggregate(os.Stdout, args[0]); err != nil {
|
||||||
if err := reporter.Aggregate(os.Stdout, dir); err != nil {
|
|
||||||
fatalf("%v", err)
|
fatalf("%v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,3 +104,51 @@ func fatalf(msg string, args ...any) {
|
|||||||
fmt.Fprintf(os.Stderr, "error: "+msg+"\n", args...)
|
fmt.Fprintf(os.Stderr, "error: "+msg+"\n", args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasFlag(args []string, name string) bool {
|
||||||
|
for _, a := range args {
|
||||||
|
if a == name {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func flagValue(args []string, name string) string {
|
||||||
|
prefix := name + "="
|
||||||
|
for i, a := range args {
|
||||||
|
if a == name && i+1 < len(args) {
|
||||||
|
return args[i+1]
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(a, prefix) {
|
||||||
|
return a[len(prefix):]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func positionalArgs(args []string) []string {
|
||||||
|
known := map[string]bool{"--json": true, "--help": true, "-h": true}
|
||||||
|
withValue := map[string]bool{"--wrapper": true, "--binary": true}
|
||||||
|
|
||||||
|
var out []string
|
||||||
|
skip := false
|
||||||
|
for _, a := range args {
|
||||||
|
if skip {
|
||||||
|
skip = false
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if known[a] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if withValue[a] {
|
||||||
|
skip = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(a, "--wrapper=") || strings.HasPrefix(a, "--binary=") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, a)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user