Example #1
0
func (p *manPageParser) parseOptions(optsStr string) (flags []task.Flag, err error) {
	reader := bufio.NewReader(bytes.NewReader([]byte(optsStr)))
	flagRegexp := regexp.MustCompile(`\-?\-(\w+),?(=(.+))?`)
	var (
		isStringFlag    bool
		stringFlagValue string
		line, name      string
		content         []string
	)
	for err == nil {
		line, err = readLine(reader)
		if flagRegexp.MatchString(line) {
			if name != line {
				if name != "" {
					if isStringFlag {
						flags = append(flags, task.NewStringFlag(name, stringFlagValue, concatFlagContent(content)))
					} else {
						flags = append(flags, task.NewBoolFlag(name, concatFlagContent(content)))
					}
				}
				var fstrs []string
				// FIXME: everything is a string flag for now
				// TODO: if it sees `,`, it parses as a StringSlice flag
				isStringFlag = strings.Contains(line, `=`)
				stringFlagValue = ""
				for _, fstr := range flagRegexp.FindAllStringSubmatch(line, -1) {
					fstrs = append(fstrs, fstr[1])
					if isStringFlag {
						value := fstr[3]
						if !strings.HasPrefix(value, "<") && !strings.HasSuffix(value, ">") {
							stringFlagValue = fstr[3]
						}
					}
				}
				name = strings.Join(fstrs, ", ")
				content = []string{}
			}
		} else {
			if line != "" {
				line = strings.TrimSpace(line)
			}
			content = append(content, line)
		}
	}
	// the last one
	if name != "" {
		if isStringFlag {
			flags = append(flags, task.NewStringFlag(name, stringFlagValue, concatFlagContent(content)))
		} else {
			flags = append(flags, task.NewBoolFlag(name, concatFlagContent(content)))
		}
	}

	if err == io.EOF {
		err = nil
	}

	return
}
func TestMainWriter_Write(t *testing.T) {
	var out bytes.Buffer
	b := mainWriter{
		&task.TaskSet{
			ImportPath: "github.com/jingweno/gotask/examples",
			Tasks: []task.Task{
				{
					Name:        "HelloWorld",
					ActionName:  "TaskHelloWorld",
					Usage:       "Say Hello world",
					Description: "Print out Hello World",
					Flags: []task.Flag{
						task.NewBoolFlag("v, verbose", "Run in verbose mode"),
					},
				},
			},
		},
	}
	b.Write(&out)

	assert.Tf(t, strings.Contains(out.String(), `_task "github.com/jingweno/gotask/examples"`), "%v", out.String())
	assert.Tf(t, strings.Contains(out.String(), `Name: "HelloWorld"`), "%v", out.String())
	assert.Tf(t, strings.Contains(out.String(), `Usage: "Say Hello world"`), "%v", out.String())
	assert.Tf(t, strings.Contains(out.String(), `Description: "Print out Hello World`), "%v", out.String())
	assert.Tf(t, strings.Contains(out.String(), `task.NewBoolFlag("v, verbose", "Run in verbose mode")`), "%v", out.String())
}
Example #3
0
func (p *manPageParser) parseOptions(optsStr string) (flags []task.Flag, err error) {
	reader := bufio.NewReader(bytes.NewReader([]byte(optsStr)))
	flagRegexp := regexp.MustCompile(`\-?\-(\w+),?`)

	var (
		line, name string
		content    []string
	)
	for err == nil {
		line, err = readLine(reader)
		if flagRegexp.MatchString(line) {
			if name != line {
				if name != "" {
					flags = append(flags, task.NewBoolFlag(name, concatFlagContent(content)))
				}

				var fstrs []string
				for _, fstr := range flagRegexp.FindAllStringSubmatch(line, -1) {
					fstrs = append(fstrs, fstr[1])
				}

				name = strings.Join(fstrs, ", ")
				content = []string{}
			}
		} else {
			if line != "" {
				line = strings.TrimSpace(line)
			}
			content = append(content, line)
		}
	}
	// the last one
	if name != "" {
		flags = append(flags, task.NewBoolFlag(name, concatFlagContent(content)))
	}

	if err == io.EOF {
		err = nil
	}

	return
}