feat: better structured internal
This commit is contained in:
@@ -5,18 +5,24 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Arguments given to program.
|
||||
type CLIOptions struct {
|
||||
Input string
|
||||
Verbose bool
|
||||
// The source code given to the program.
|
||||
Input string
|
||||
// Whether or not to print debug logs.
|
||||
Verbose bool
|
||||
// Whether or not to print an explanation of the reduction.
|
||||
Explanation bool
|
||||
}
|
||||
|
||||
func ParseOptions(args []string) (*CLIOptions, error) {
|
||||
// Parse flags and arguments.
|
||||
// Extract the program configuration from the command-line arguments.
|
||||
func ParseOptions() (*CLIOptions, error) {
|
||||
// Parse flags.
|
||||
verbose := flag.Bool("v", false, "Verbosity. If set, the program will print logs.")
|
||||
explanation := flag.Bool("x", false, "Explanation. Whether or not to show all reduction steps.")
|
||||
flag.Parse()
|
||||
|
||||
// Parse non-flag arguments.
|
||||
if flag.NArg() == 0 {
|
||||
return nil, fmt.Errorf("No input given.")
|
||||
} else if flag.NArg() > 1 {
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// A helper function to handle errors in the program. If it is given an error,
|
||||
// the program will exist, and print the error.
|
||||
func HandleError(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
@@ -12,4 +14,4 @@ func HandleError(err error) {
|
||||
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetLogger(o CLIOptions) *slog.Logger {
|
||||
var level slog.Level
|
||||
if o.Verbose {
|
||||
level = slog.LevelInfo
|
||||
} else {
|
||||
level = slog.LevelError
|
||||
}
|
||||
|
||||
return slog.New(
|
||||
slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: level,
|
||||
}),
|
||||
)
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ReadInput() (string, error) {
|
||||
data, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
Reference in New Issue
Block a user