summaryrefslogtreecommitdiff
path: root/argv.parseOsArgs.go
blob: 37a477d506b308928da096d4e132470eb4486d88 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package argvpb

// initializes logging and command line options

import (
	"fmt"
	"os"
	"strings"
)

func GetCmd() string {
	return PB.GetCmd()
}

func mypanic(blah string) {
	// me.debug = true
	// doStdoutStderr()
	panic(blah)
}

// todo: this is wrong
func (pb *Argv) GetCmd() string {
	var curcmd string
	if pb == nil {
		return "pb=<nil>"
	}
	for _, s := range pb.Real {
		if strings.HasPrefix(s, "-") {
			// option is something like --verbose
			// skip these. they are not subcommands
			continue
		}
		// found the subcommand
		curcmd = s
		break
	}
	if pb.Partial == "'"+curcmd+"'" {
		// not really a command, it's just a partially inputed string from the user
		return ""
	}
	return curcmd
}

// todo: this is dumb
func (pb *Argv) getSubSubCmd() string {
	var subcmd string
	for _, s := range pb.Real {
		p := fmt.Sprintf("'%s'", s)
		if pb.Partial == p {
			pb.Debugf("DEBUG: Last argv MATCHES Partial %s %s", s, p)
			continue
		} else {
			pb.Debugf("DEBUG: Last argv DOES NOT MATCH Partial %s %s", s, p)
		}
		subcmd = s
	}
	return subcmd
}

func (pb *Argv) parseOsArgs() {
	// this shouldn't really happen. OS/POSIX error?
	if len(os.Args) == 0 {
		// fmt.Fprintf(Stderr, "what OS is this?\n")
		return
	}

	// there is nothing on the command line. user just ran "forge" and hit enter
	if len(os.Args) == 1 {
		// pb.Arg0 = os.Args[0]
		return
	}

	// try to setup bash autocomplete and exit
	if len(os.Args) > 1 && os.Args[1] == "--bash" {
		me.setupAuto = true
		return
	}

	// try to setup zsh autocomplete and exit
	if len(os.Args) > 1 && os.Args[1] == "--zsh" {
		me.setupAuto = true
		return
	}

	// print the version and exit
	if len(os.Args) > 1 && os.Args[1] == "--version" {
		// if binary defined buildtime() then process standard version output here
		doVersion(pb)
		saveAndExit()
	}

	if os.Args[1] != "--auto-complete" {
		// if the first arg is not --auto-complete, then don't go any farther
		for _, s := range os.Args[1:] {
			pb.Real = append(pb.Real, s)
		}
		// found the subcommand
		// pb.Cmd = pb.findCmd()
		// exit here. not autocomplete
		// todo: actually finish parsing the pb. is it safe to continue from here?
		return
	}

	// the shell is trying to get autocomplete information

	// initial PB setup
	me.isAuto = true
	// pb.Arg0 = os.Args[0]
	// pb.Arg1 = os.Args[1]
	pb.Partial = os.Args[2]
	// pb.Arg3 = os.Args[3]

	if len(os.Args) < 5 {
		// the user is doing autocomplete on the command itself
		// no subcommand. user has done "forge <TAB><TAB>"
		pb.Partial = ""
		pb.Real = []string{""}
		return
	}

	// figure out if there is a subcommand or a partial match
	if pb.Partial == "''" {
		pb.Partial = ""
	}

	// pb.Argv = os.Args[4:]
	for _, s := range os.Args[4:] {
		if s == "--argvdebug" {
			me.debug = true
			continue
		}
		tmp := strings.Trim(pb.Partial, "'")
		if tmp == s {
			// don't put pb.Partial into Argv
			continue
		}
		pb.Real = append(pb.Real, s)
	}
	return
}