2017-12-31 16:18:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-01-19 15:51:18 +01:00
|
|
|
"os"
|
2018-03-09 03:03:04 +01:00
|
|
|
"strconv"
|
2018-01-19 15:51:18 +01:00
|
|
|
"strings"
|
2017-12-31 16:18:12 +01:00
|
|
|
)
|
|
|
|
|
2018-03-04 01:12:19 +01:00
|
|
|
// A basic set implementation for strings.
|
|
|
|
// This is used a lot so it deserves its own type.
|
2018-03-03 03:45:16 +01:00
|
|
|
// Other types of sets are used throughout the code but do not have
|
|
|
|
// their own typedef.
|
|
|
|
// String sets and <type>sets should be used throughout the code when applicable,
|
|
|
|
// they are a lot more flexable than slices and provide easy lookup.
|
2018-01-19 15:51:18 +01:00
|
|
|
type stringSet map[string]struct{}
|
2018-01-03 20:17:57 +01:00
|
|
|
|
2018-01-17 22:48:23 +01:00
|
|
|
func (set stringSet) set(v string) {
|
|
|
|
set[v] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set stringSet) get(v string) bool {
|
|
|
|
_, exists := set[v]
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set stringSet) remove(v string) {
|
|
|
|
delete(set, v)
|
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (set stringSet) toSlice() []string {
|
|
|
|
slice := make([]string, 0, len(set))
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
for v := range set {
|
|
|
|
slice = append(slice, v)
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
2018-03-19 16:30:44 +01:00
|
|
|
func sliceToStringSet(in []string) stringSet {
|
2018-03-09 03:03:04 +01:00
|
|
|
set := make(stringSet)
|
|
|
|
|
|
|
|
for _, v := range in {
|
|
|
|
set.set(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeStringSet(in ...string) stringSet {
|
2018-03-19 16:30:44 +01:00
|
|
|
return sliceToStringSet(in)
|
2018-03-09 03:03:04 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 03:45:16 +01:00
|
|
|
// Parses command line arguments in a way we can interact with programmatically but
|
|
|
|
// also in a way that can easily be passed to pacman later on.
|
2018-01-04 01:59:57 +01:00
|
|
|
type arguments struct {
|
2018-01-19 15:51:18 +01:00
|
|
|
op string
|
2017-12-31 16:18:12 +01:00
|
|
|
options map[string]string
|
2018-01-04 07:01:17 +01:00
|
|
|
globals map[string]string
|
2018-03-03 03:45:16 +01:00
|
|
|
doubles stringSet // Tracks args passed twice such as -yy and -dd
|
2018-01-04 01:59:57 +01:00
|
|
|
targets stringSet
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func makeArguments() *arguments {
|
2018-01-19 15:51:18 +01:00
|
|
|
return &arguments{
|
2017-12-31 16:18:12 +01:00
|
|
|
"",
|
|
|
|
make(map[string]string),
|
2018-01-04 07:01:17 +01:00
|
|
|
make(map[string]string),
|
2018-01-04 01:59:57 +01:00
|
|
|
make(stringSet),
|
|
|
|
make(stringSet),
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
func (parser *arguments) copy() (cp *arguments) {
|
|
|
|
cp = makeArguments()
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
cp.op = parser.op
|
2018-01-19 15:51:18 +01:00
|
|
|
|
|
|
|
for k, v := range parser.options {
|
2018-01-04 07:01:17 +01:00
|
|
|
cp.options[k] = v
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
|
|
|
for k, v := range parser.globals {
|
2018-01-04 07:01:17 +01:00
|
|
|
cp.globals[k] = v
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
|
|
|
for k, v := range parser.targets {
|
2018-01-04 07:01:17 +01:00
|
|
|
cp.targets[k] = v
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
|
|
|
for k, v := range parser.doubles {
|
2018-01-04 07:01:17 +01:00
|
|
|
cp.doubles[k] = v
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) delArg(options ...string) {
|
|
|
|
for _, option := range options {
|
|
|
|
delete(parser.options, option)
|
2018-01-04 07:01:17 +01:00
|
|
|
delete(parser.globals, option)
|
2018-01-04 01:59:57 +01:00
|
|
|
delete(parser.doubles, option)
|
|
|
|
}
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) needRoot() bool {
|
|
|
|
if parser.existsArg("h", "help") {
|
2018-01-03 19:42:40 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
if parser.existsArg("p", "print") {
|
2018-01-19 15:51:18 +01:00
|
|
|
return false
|
2018-01-03 19:42:40 +01:00
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-03 19:42:40 +01:00
|
|
|
switch parser.op {
|
|
|
|
case "V", "version":
|
|
|
|
return false
|
|
|
|
case "D", "database":
|
|
|
|
return true
|
|
|
|
case "F", "files":
|
2018-01-04 01:59:57 +01:00
|
|
|
if parser.existsArg("y", "refresh") {
|
2018-01-03 19:42:40 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
case "Q", "query":
|
|
|
|
return false
|
|
|
|
case "R", "remove":
|
|
|
|
return true
|
|
|
|
case "S", "sync":
|
2018-01-04 07:01:17 +01:00
|
|
|
if parser.existsArg("y", "refresh") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if parser.existsArg("u", "sysupgrade") {
|
|
|
|
return true
|
|
|
|
}
|
2018-01-04 01:59:57 +01:00
|
|
|
if parser.existsArg("s", "search") {
|
2018-01-03 19:42:40 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-01-04 01:59:57 +01:00
|
|
|
if parser.existsArg("l", "list") {
|
2018-01-03 19:42:40 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-01-04 07:01:17 +01:00
|
|
|
if parser.existsArg("i", "info") {
|
|
|
|
return false
|
|
|
|
}
|
2018-01-03 19:42:40 +01:00
|
|
|
return true
|
|
|
|
case "T", "deptest":
|
|
|
|
return false
|
|
|
|
case "U", "upgrade":
|
|
|
|
return true
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-03-03 03:45:16 +01:00
|
|
|
// yay specific
|
2018-01-03 19:42:40 +01:00
|
|
|
case "Y", "yay":
|
|
|
|
return false
|
2018-01-20 18:48:40 +01:00
|
|
|
case "P", "print":
|
|
|
|
return false
|
2018-01-03 19:42:40 +01:00
|
|
|
case "G", "getpkgbuild":
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) addOP(op string) (err error) {
|
|
|
|
if parser.op != "" {
|
2017-12-31 16:18:12 +01:00
|
|
|
err = fmt.Errorf("only one operation may be used at a time")
|
|
|
|
return
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
parser.op = op
|
2017-12-31 16:18:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) addParam(option string, arg string) (err error) {
|
2017-12-31 16:18:12 +01:00
|
|
|
if isOp(option) {
|
2018-01-04 01:59:57 +01:00
|
|
|
err = parser.addOP(option)
|
2017-12-31 16:18:12 +01:00
|
|
|
return
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
if parser.existsArg(option) {
|
|
|
|
parser.doubles[option] = struct{}{}
|
2018-01-04 07:01:17 +01:00
|
|
|
} else if isGlobal(option) {
|
|
|
|
parser.globals[option] = arg
|
2017-12-31 16:18:12 +01:00
|
|
|
} else {
|
2018-01-04 01:59:57 +01:00
|
|
|
parser.options[option] = arg
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) addArg(options ...string) (err error) {
|
|
|
|
for _, option := range options {
|
|
|
|
err = parser.addParam(option, "")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 07:01:17 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-03 03:45:16 +01:00
|
|
|
// Multiple args acts as an OR operator
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) existsArg(options ...string) bool {
|
|
|
|
for _, option := range options {
|
|
|
|
_, exists := parser.options[option]
|
|
|
|
if exists {
|
|
|
|
return true
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
_, exists = parser.globals[option]
|
|
|
|
if exists {
|
|
|
|
return true
|
|
|
|
}
|
2018-01-04 01:59:57 +01:00
|
|
|
}
|
|
|
|
return false
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 22:21:39 +01:00
|
|
|
func (parser *arguments) getArg(options ...string) (arg string, double bool, exists bool) {
|
|
|
|
for _, option := range options {
|
|
|
|
arg, exists = parser.options[option]
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
_, double = parser.doubles[option]
|
|
|
|
return
|
|
|
|
}
|
2018-01-04 07:01:17 +01:00
|
|
|
|
2018-01-04 22:21:39 +01:00
|
|
|
arg, exists = parser.globals[option]
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 22:21:39 +01:00
|
|
|
if exists {
|
|
|
|
_, double = parser.doubles[option]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) addTarget(targets ...string) {
|
|
|
|
for _, target := range targets {
|
|
|
|
parser.targets[target] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parser *arguments) delTarget(targets ...string) {
|
|
|
|
for _, target := range targets {
|
|
|
|
delete(parser.targets, target)
|
|
|
|
}
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 03:45:16 +01:00
|
|
|
// Multiple args acts as an OR operator
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) existsDouble(options ...string) bool {
|
|
|
|
for _, option := range options {
|
|
|
|
_, exists := parser.doubles[option]
|
|
|
|
if exists {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
return false
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) formatTargets() (args []string) {
|
|
|
|
for target := range parser.targets {
|
|
|
|
args = append(args, target)
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
return
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) formatArgs() (args []string) {
|
2018-01-21 12:48:09 +01:00
|
|
|
var op string
|
|
|
|
|
|
|
|
if parser.op != "" {
|
|
|
|
op = formatArg(parser.op)
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:42:40 +01:00
|
|
|
args = append(args, op)
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
for option, arg := range parser.options {
|
2018-02-20 18:03:50 +01:00
|
|
|
if option == "--" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
formatedOption := formatArg(option)
|
|
|
|
args = append(args, formatedOption)
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
if hasParam(option) {
|
2018-01-03 19:42:40 +01:00
|
|
|
args = append(args, arg)
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
if parser.existsDouble(option) {
|
2018-01-04 07:01:17 +01:00
|
|
|
args = append(args, formatedOption)
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parser *arguments) formatGlobals() (args []string) {
|
|
|
|
for option, arg := range parser.globals {
|
|
|
|
formatedOption := formatArg(option)
|
|
|
|
args = append(args, formatedOption)
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
if hasParam(option) {
|
|
|
|
args = append(args, arg)
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
if parser.existsDouble(option) {
|
|
|
|
args = append(args, formatedOption)
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
return
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func formatArg(arg string) string {
|
|
|
|
if len(arg) > 1 {
|
|
|
|
arg = "--" + arg
|
|
|
|
} else {
|
|
|
|
arg = "-" + arg
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
func isOp(op string) bool {
|
|
|
|
switch op {
|
|
|
|
case "V", "version":
|
|
|
|
return true
|
|
|
|
case "D", "database":
|
|
|
|
return true
|
|
|
|
case "F", "files":
|
|
|
|
return true
|
|
|
|
case "Q", "query":
|
|
|
|
return true
|
|
|
|
case "R", "remove":
|
|
|
|
return true
|
|
|
|
case "S", "sync":
|
|
|
|
return true
|
|
|
|
case "T", "deptest":
|
|
|
|
return true
|
|
|
|
case "U", "upgrade":
|
|
|
|
return true
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-03-03 03:45:16 +01:00
|
|
|
// yay specific
|
2017-12-31 16:18:12 +01:00
|
|
|
case "Y", "yay":
|
|
|
|
return true
|
2018-01-20 18:48:40 +01:00
|
|
|
case "P", "print":
|
|
|
|
return true
|
2018-01-03 05:55:47 +01:00
|
|
|
case "G", "getpkgbuild":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 07:01:17 +01:00
|
|
|
func isGlobal(op string) bool {
|
|
|
|
switch op {
|
|
|
|
case "b", "dbpath":
|
|
|
|
return true
|
|
|
|
case "r", "root":
|
|
|
|
return true
|
|
|
|
case "v", "verbose":
|
|
|
|
return true
|
|
|
|
case "arch":
|
|
|
|
return true
|
|
|
|
case "cachedir":
|
|
|
|
return true
|
|
|
|
case "color":
|
|
|
|
return true
|
|
|
|
case "config":
|
|
|
|
return true
|
|
|
|
case "debug":
|
|
|
|
return true
|
|
|
|
case "gpgdir":
|
|
|
|
return true
|
|
|
|
case "hookdir":
|
|
|
|
return true
|
|
|
|
case "logfile":
|
|
|
|
return true
|
|
|
|
case "noconfirm":
|
|
|
|
return true
|
|
|
|
case "confirm":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
func hasParam(arg string) bool {
|
|
|
|
switch arg {
|
|
|
|
case "dbpath", "b":
|
|
|
|
return true
|
|
|
|
case "root", "r":
|
|
|
|
return true
|
|
|
|
case "sysroot":
|
|
|
|
return true
|
|
|
|
case "config":
|
|
|
|
return true
|
|
|
|
case "ignore":
|
|
|
|
return true
|
|
|
|
case "assume-installed":
|
|
|
|
return true
|
|
|
|
case "overwrite":
|
|
|
|
return true
|
|
|
|
case "ask":
|
|
|
|
return true
|
|
|
|
case "cachedir":
|
|
|
|
return true
|
|
|
|
case "hookdir":
|
|
|
|
return true
|
|
|
|
case "logfile":
|
|
|
|
return true
|
|
|
|
case "ignoregroup":
|
|
|
|
return true
|
|
|
|
case "arch":
|
|
|
|
return true
|
|
|
|
case "print-format":
|
|
|
|
return true
|
|
|
|
case "gpgdir":
|
|
|
|
return true
|
|
|
|
case "color":
|
|
|
|
return true
|
2018-03-07 18:12:30 +01:00
|
|
|
|
|
|
|
//yay params
|
|
|
|
case "mflags":
|
|
|
|
return true
|
2018-03-17 21:40:24 +01:00
|
|
|
case "gpgflags":
|
|
|
|
return true
|
2018-03-08 02:36:06 +01:00
|
|
|
case "builddir":
|
|
|
|
return true
|
|
|
|
case "editor":
|
|
|
|
return true
|
|
|
|
case "makepkg":
|
|
|
|
return true
|
|
|
|
case "pacman":
|
|
|
|
return true
|
|
|
|
case "tar":
|
|
|
|
return true
|
|
|
|
case "git":
|
|
|
|
return true
|
2018-03-17 21:40:24 +01:00
|
|
|
case "gpg":
|
|
|
|
return true
|
2018-03-08 02:36:06 +01:00
|
|
|
case "requestsplitn":
|
|
|
|
return true
|
2017-12-31 16:18:12 +01:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 03:45:16 +01:00
|
|
|
// Parses short hand options such as:
|
|
|
|
// -Syu -b/some/path -
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) parseShortOption(arg string, param string) (usedNext bool, err error) {
|
2017-12-31 16:18:12 +01:00
|
|
|
if arg == "-" {
|
|
|
|
err = parser.addArg("-")
|
|
|
|
return
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
arg = arg[1:]
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
for k, _char := range arg {
|
|
|
|
char := string(_char)
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
if hasParam(char) {
|
2018-01-19 15:51:18 +01:00
|
|
|
if k < len(arg)-2 {
|
2017-12-31 16:18:12 +01:00
|
|
|
err = parser.addParam(char, arg[k+2:])
|
|
|
|
} else {
|
|
|
|
usedNext = true
|
|
|
|
err = parser.addParam(char, param)
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
break
|
|
|
|
} else {
|
|
|
|
err = parser.addArg(char)
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-03 03:45:16 +01:00
|
|
|
// Parses full length options such as:
|
|
|
|
// --sync --refresh --sysupgrade --dbpath /some/path --
|
2018-01-19 15:51:18 +01:00
|
|
|
func (parser *arguments) parseLongOption(arg string, param string) (usedNext bool, err error) {
|
2017-12-31 16:18:12 +01:00
|
|
|
if arg == "--" {
|
|
|
|
err = parser.addArg(arg)
|
|
|
|
return
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
arg = arg[2:]
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
if hasParam(arg) {
|
|
|
|
err = parser.addParam(arg, param)
|
|
|
|
usedNext = true
|
|
|
|
} else {
|
|
|
|
err = parser.addArg(arg)
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-04 01:59:57 +01:00
|
|
|
func (parser *arguments) parseStdin() (err error) {
|
2018-02-20 14:20:33 +01:00
|
|
|
for {
|
2017-12-31 16:18:12 +01:00
|
|
|
var target string
|
|
|
|
_, err = fmt.Scan(&target)
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
return
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
parser.addTarget(target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 15:51:18 +01:00
|
|
|
func (parser *arguments) parseCommandLine() (err error) {
|
2017-12-31 16:18:12 +01:00
|
|
|
args := os.Args[1:]
|
|
|
|
usedNext := false
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
if len(args) < 1 {
|
2018-03-31 22:55:15 +02:00
|
|
|
parser.parseShortOption("-Syu", "")
|
|
|
|
} else {
|
|
|
|
for k, arg := range args {
|
|
|
|
var nextArg string
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-03-31 22:55:15 +02:00
|
|
|
if usedNext {
|
|
|
|
usedNext = false
|
|
|
|
continue
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-03-31 22:55:15 +02:00
|
|
|
if k+1 < len(args) {
|
|
|
|
nextArg = args[k+1]
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-03-31 22:55:15 +02:00
|
|
|
if parser.existsArg("--") {
|
|
|
|
parser.addTarget(arg)
|
|
|
|
} else if strings.HasPrefix(arg, "--") {
|
|
|
|
usedNext, err = parser.parseLongOption(arg, nextArg)
|
|
|
|
} else if strings.HasPrefix(arg, "-") {
|
|
|
|
usedNext, err = parser.parseShortOption(arg, nextArg)
|
|
|
|
} else {
|
|
|
|
parser.addTarget(arg)
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-03-31 22:55:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-12-31 16:18:12 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
if parser.op == "" {
|
|
|
|
parser.op = "Y"
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2018-01-04 22:21:39 +01:00
|
|
|
if cmdArgs.existsArg("-") {
|
2018-03-12 01:04:50 +01:00
|
|
|
var file *os.File
|
2018-01-19 15:51:18 +01:00
|
|
|
err = cmdArgs.parseStdin()
|
2018-03-12 01:04:50 +01:00
|
|
|
cmdArgs.delArg("-")
|
2018-01-04 22:21:39 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-03-12 01:04:50 +01:00
|
|
|
|
|
|
|
file, err = os.Open("/dev/tty")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Stdin = file
|
2018-01-04 22:21:39 +01:00
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2017-12-31 16:18:12 +01:00
|
|
|
return
|
2018-01-19 15:51:18 +01:00
|
|
|
}
|
2018-03-09 03:03:04 +01:00
|
|
|
|
|
|
|
//parses input for number menus
|
|
|
|
//supports individual selection: 1 2 3 4
|
|
|
|
//supports range selections: 1-4 10-20
|
|
|
|
//supports negation: ^1 ^1-4
|
|
|
|
//
|
|
|
|
//include and excule holds numbers that should be added and should not be added
|
|
|
|
//respectively. other holds anythign that can't be parsed as an int. This is
|
|
|
|
//intended to allow words inside of number menus. e.g. 'all' 'none' 'abort'
|
|
|
|
//of course the implementation is up to the caller, this function mearley parses
|
|
|
|
//the input and organizes it
|
|
|
|
func parseNumberMenu(input string) (intRanges, intRanges, stringSet, stringSet) {
|
|
|
|
include := make(intRanges, 0, 0)
|
|
|
|
exclude := make(intRanges, 0, 0)
|
|
|
|
otherInclude := make(stringSet)
|
|
|
|
otherExclude := make(stringSet)
|
|
|
|
|
|
|
|
words := strings.Fields(input)
|
|
|
|
|
|
|
|
for _, word := range words {
|
|
|
|
var num1 int
|
|
|
|
var num2 int
|
|
|
|
var err error
|
|
|
|
invert := false
|
|
|
|
other := otherInclude
|
|
|
|
|
|
|
|
if word[0] == '^' {
|
|
|
|
invert = true
|
|
|
|
other = otherExclude
|
|
|
|
word = word[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
ranges := strings.SplitN(word, "-", 2)
|
|
|
|
|
|
|
|
num1, err = strconv.Atoi(ranges[0])
|
|
|
|
if err != nil {
|
|
|
|
other.set(strings.ToLower(word))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ranges) == 2 {
|
|
|
|
num2, err = strconv.Atoi(ranges[1])
|
|
|
|
if err != nil {
|
|
|
|
other.set(strings.ToLower(word))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
num2 = num1
|
|
|
|
}
|
|
|
|
|
|
|
|
mi := min(num1, num2)
|
|
|
|
ma := max(num1, num2)
|
|
|
|
|
|
|
|
if !invert {
|
|
|
|
include = append(include, makeIntRange(mi, ma))
|
|
|
|
} else {
|
|
|
|
exclude = append(exclude, makeIntRange(mi, ma))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return include, exclude, otherInclude, otherExclude
|
|
|
|
}
|