-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.go
More file actions
86 lines (69 loc) · 2.12 KB
/
args.go
File metadata and controls
86 lines (69 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
// Regular Imports:
import (
"fmt"
"github.com/alexflint/go-arg"
"os"
util "github.com/soerlemans/table/util"
)
// Arguments Struct:
type Arguments struct {
ProgramFile string `arg:"-f,--file" help:"Path to file containing filters."`
FromStdin bool `arg:"--stdin" help:"Specifies if the program should read from stdin." default:"false"`
FieldSeparator rune `arg:"-F,--field-separator" help:"Define the field separator."`
Csv bool `help:"Define that input is CSV (default)."`
Json bool `help:"Define that input is JSON."`
Excel bool `help:"Define that input is Excel. "`
// Positional.
ProgramText string `arg:"positional" help:"Filter to execute."`
InputFiles []string `arg:"positional" help:"Files to source as input."`
}
// Arguments Methods:
func (Arguments) Version() string {
return fmt.Sprintf("Version: %s", VERSION)
}
// Globals:
const (
VERSION = "0.1"
)
// Functions:
func handleProgramFile(t_args *Arguments) error {
if len(t_args.ProgramFile) != 0 {
// If the program file was supplied then the program text positional arg.
// Should be moved to other input files to parse.
slice := []string{t_args.ProgramText}
t_args.InputFiles = append(slice, t_args.InputFiles...)
_, err := os.Stat(t_args.ProgramFile)
if err != nil {
return err
}
// TODO: Implement the rest.
t_args.ProgramText = "TODO: Overwrite with args.ProgramFile content."
}
return nil
}
func initArgs() (Arguments, error) {
var args Arguments
// Parse and handle arguments.
arg.MustParse(&args)
defer func() { util.Logf("args: %+v", args) }()
// Logging:
err := handleProgramFile(&args)
if err != nil {
return args, err
}
// If no input format was specified automatically assume csv input.
if !(args.Csv && args.Json && args.Excel) {
util.Logln("No specific input format was specified assuming csv.")
args.Csv = true
}
// Do log this, for debugging purposes.
if len(args.ProgramText) == 0 {
util.Logf("No program text given.")
}
// If no input files are supplied check stdin.
if len(args.InputFiles) == 0 {
args.FromStdin = true
}
return args, nil
}