wrap command line docs
This commit is contained in:
parent
395a6693d8
commit
8e0611f0b1
@ -14,7 +14,7 @@ var checkFailureTests = []mainTest{
|
||||
"-syntax-string",
|
||||
"-input",
|
||||
"-input-string",
|
||||
joinLines(positionalInputUsage),
|
||||
wrapLines(positionalInputUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -30,7 +30,7 @@ var checkFailureTests = []mainTest{
|
||||
"-syntax-string",
|
||||
"-input",
|
||||
"-input-string",
|
||||
joinLines(positionalInputUsage),
|
||||
wrapLines(positionalInputUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -46,7 +46,7 @@ var checkFailureTests = []mainTest{
|
||||
"-syntax-string",
|
||||
"-input",
|
||||
"-input-string",
|
||||
joinLines(positionalInputUsage),
|
||||
wrapLines(positionalInputUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -62,7 +62,7 @@ var checkFailureTests = []mainTest{
|
||||
"-syntax-string",
|
||||
"-input",
|
||||
"-input-string",
|
||||
joinLines(positionalInputUsage),
|
||||
wrapLines(positionalInputUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -78,7 +78,7 @@ var checkFailureTests = []mainTest{
|
||||
"-syntax-string",
|
||||
"-input",
|
||||
"-input-string",
|
||||
joinLines(positionalInputUsage),
|
||||
wrapLines(positionalInputUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -94,7 +94,7 @@ var checkFailureTests = []mainTest{
|
||||
"-syntax-string",
|
||||
"-input",
|
||||
"-input-string",
|
||||
joinLines(positionalInputUsage),
|
||||
wrapLines(positionalInputUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -110,7 +110,7 @@ var checkFailureTests = []mainTest{
|
||||
"-syntax-string",
|
||||
"-input",
|
||||
"-input-string",
|
||||
joinLines(positionalInputUsage),
|
||||
wrapLines(positionalInputUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -212,14 +212,14 @@ func TestCheck(t *testing.T) {
|
||||
"treerack", "check", "-help",
|
||||
},
|
||||
stdout: []string{
|
||||
joinLines(checkUsage),
|
||||
wrapLines(checkUsage),
|
||||
"-syntax",
|
||||
"-syntax-string",
|
||||
"-input",
|
||||
"-input-string",
|
||||
joinLines(positionalInputUsage),
|
||||
joinLines(checkExample),
|
||||
joinLines(docRef),
|
||||
wrapLines(positionalInputUsage),
|
||||
wrapLines(checkExample),
|
||||
wrapLines(docRef),
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -12,7 +12,7 @@ var checkSyntaxFailureTests = []mainTest{
|
||||
stderr: []string{
|
||||
"-syntax",
|
||||
"-syntax-string",
|
||||
joinLines(positionalSyntaxUsage),
|
||||
wrapLines(positionalSyntaxUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -26,7 +26,7 @@ var checkSyntaxFailureTests = []mainTest{
|
||||
"only one syntax",
|
||||
"-syntax",
|
||||
"-syntax-string",
|
||||
joinLines(positionalSyntaxUsage),
|
||||
wrapLines(positionalSyntaxUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -40,7 +40,7 @@ var checkSyntaxFailureTests = []mainTest{
|
||||
"only one syntax",
|
||||
"-syntax",
|
||||
"-syntax-string",
|
||||
joinLines(positionalSyntaxUsage),
|
||||
wrapLines(positionalSyntaxUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -54,7 +54,7 @@ var checkSyntaxFailureTests = []mainTest{
|
||||
"only one syntax",
|
||||
"-syntax",
|
||||
"-syntax-string",
|
||||
joinLines(positionalSyntaxUsage),
|
||||
wrapLines(positionalSyntaxUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -68,7 +68,7 @@ var checkSyntaxFailureTests = []mainTest{
|
||||
"missing syntax",
|
||||
"-syntax",
|
||||
"-syntax-string",
|
||||
joinLines(positionalSyntaxUsage),
|
||||
wrapLines(positionalSyntaxUsage),
|
||||
},
|
||||
},
|
||||
|
||||
@ -134,12 +134,12 @@ func TestCheckSyntax(t *testing.T) {
|
||||
"treerack", "check-syntax", "-help",
|
||||
},
|
||||
stdout: []string{
|
||||
joinLines(checkSyntaxUsage),
|
||||
wrapLines(checkSyntaxUsage),
|
||||
"-syntax",
|
||||
"-syntax-string",
|
||||
joinLines(positionalSyntaxUsage),
|
||||
joinLines(checkSyntaxExample),
|
||||
joinLines(docRef),
|
||||
wrapLines(positionalSyntaxUsage),
|
||||
wrapLines(checkSyntaxExample),
|
||||
wrapLines(docRef),
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -1,12 +1,15 @@
|
||||
package main
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const summary = `treerack - parser generator - https://github.com/aryszka/treerack`
|
||||
|
||||
const commandsHelp = `Available commands:
|
||||
check validates an arbitrary input against a syntax definition
|
||||
parse parses an arbitrary input with a syntax definition into an abstract syntax tree
|
||||
check validates arbitrary input against a syntax definition
|
||||
parse parses arbitrary input with a syntax definition into an abstract syntax tree
|
||||
check-syntax validates a syntax definition
|
||||
generate generates a parser from a syntax definition
|
||||
help prints the current help
|
||||
@ -40,8 +43,8 @@ const indentUsage = `string used for indentation of the printed AST`
|
||||
|
||||
const checkUsage = `'treerack check' takes a syntax description from a file or inline string, an arbitrary piece
|
||||
of text from the standard input, or a file, or inline string, and parses the input text with the defined syntax.
|
||||
It returns non-zero exit code and prints the problem if the provided syntax is not valid or the intput cannot be
|
||||
parsed against it.`
|
||||
It returns non-zero exit code and prints the problem if the provided syntax is not valid or the input cannot be
|
||||
parsed with it.`
|
||||
|
||||
const checkExample = `Example:
|
||||
treerack check -syntax example.treerack foo.example`
|
||||
@ -67,6 +70,37 @@ standard output.`
|
||||
const generateExample = `Example:
|
||||
treerack generate example.treerack > parser.go`
|
||||
|
||||
func joinLines(s string) string {
|
||||
return strings.Replace(s, "\n", " ", -1)
|
||||
const wrap = 72
|
||||
|
||||
func wrapLines(s string) string {
|
||||
s = strings.Replace(s, "\n", " ", -1)
|
||||
w := strings.Split(s, " ")
|
||||
|
||||
var l, ll []string
|
||||
for i := 0; i < len(w); i++ {
|
||||
ll = append(ll, w[i])
|
||||
lineLength := utf8.RuneCount([]byte(strings.Join(ll, " ")))
|
||||
if lineLength < wrap {
|
||||
continue
|
||||
}
|
||||
|
||||
if lineLength > wrap {
|
||||
ll = ll[:len(ll)-1]
|
||||
i--
|
||||
}
|
||||
|
||||
if len(ll) == 0 {
|
||||
l = append(l, w[i])
|
||||
i++
|
||||
} else {
|
||||
l = append(l, strings.Join(ll, " "))
|
||||
ll = nil
|
||||
}
|
||||
}
|
||||
|
||||
if len(ll) > 0 {
|
||||
l = append(l, strings.Join(ll, " "))
|
||||
}
|
||||
|
||||
return strings.Join(l, "\n")
|
||||
}
|
||||
|
@ -90,14 +90,14 @@ func TestGenerate(t *testing.T) {
|
||||
"treerack", "generate", "-help",
|
||||
},
|
||||
stdout: []string{
|
||||
joinLines(generateUsage),
|
||||
wrapLines(generateUsage),
|
||||
"-syntax",
|
||||
"-syntax-string",
|
||||
"-export",
|
||||
"-package-name",
|
||||
joinLines(positionalSyntaxUsage),
|
||||
joinLines(generateExample),
|
||||
joinLines(docRef),
|
||||
wrapLines(positionalSyntaxUsage),
|
||||
wrapLines(generateExample),
|
||||
wrapLines(docRef),
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -26,7 +26,7 @@ func (o *fileOptions) multipleInputsError() {
|
||||
stderr("Options:")
|
||||
o.flagSet.PrintDefaults()
|
||||
stderr()
|
||||
stderr(joinLines(o.positionalDoc))
|
||||
stderr(wrapLines(o.positionalDoc))
|
||||
}
|
||||
|
||||
func (o *fileOptions) missingInputError() {
|
||||
@ -35,7 +35,7 @@ func (o *fileOptions) missingInputError() {
|
||||
stderr("Options:")
|
||||
o.flagSet.PrintDefaults()
|
||||
stderr()
|
||||
stderr(joinLines(o.positionalDoc))
|
||||
stderr(wrapLines(o.positionalDoc))
|
||||
}
|
||||
|
||||
func (o *fileOptions) getSource() (hasInput bool, fileName string, inline string, code int) {
|
||||
|
@ -13,9 +13,9 @@ type commandOptions struct {
|
||||
func initOptions(usage, example, positionalDoc string, args []string) *commandOptions {
|
||||
var o commandOptions
|
||||
|
||||
o.usage = joinLines(usage)
|
||||
o.example = joinLines(example)
|
||||
o.positionalDoc = joinLines(positionalDoc)
|
||||
o.usage = wrapLines(usage)
|
||||
o.example = wrapLines(example)
|
||||
o.positionalDoc = wrapLines(positionalDoc)
|
||||
o.args = args
|
||||
|
||||
o.flagSet = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
@ -26,12 +26,12 @@ func initOptions(usage, example, positionalDoc string, args []string) *commandOp
|
||||
}
|
||||
|
||||
func (o *commandOptions) boolFlag(v *bool, name, usage string) {
|
||||
usage = joinLines(usage)
|
||||
usage = wrapLines(usage)
|
||||
o.flagSet.BoolVar(v, name, *v, usage)
|
||||
}
|
||||
|
||||
func (o *commandOptions) stringFlag(v *string, name, usage string) {
|
||||
usage = joinLines(usage)
|
||||
usage = wrapLines(usage)
|
||||
o.flagSet.StringVar(v, name, *v, usage)
|
||||
}
|
||||
|
||||
@ -65,7 +65,7 @@ func (o *commandOptions) printHelp() {
|
||||
stdout()
|
||||
stdout(o.example)
|
||||
stdout()
|
||||
stdout(joinLines(docRef))
|
||||
stdout(wrapLines(docRef))
|
||||
}
|
||||
|
||||
func (o *commandOptions) help() bool {
|
||||
|
@ -116,16 +116,16 @@ func TestParse(t *testing.T) {
|
||||
"treerack", "parse", "-help",
|
||||
},
|
||||
stdout: []string{
|
||||
joinLines(parseUsage),
|
||||
wrapLines(parseUsage),
|
||||
"-syntax",
|
||||
"-syntax-string",
|
||||
"-input",
|
||||
"-input-string",
|
||||
"-pretty",
|
||||
"-indent",
|
||||
joinLines(positionalInputUsage),
|
||||
joinLines(parseExample),
|
||||
joinLines(docRef),
|
||||
wrapLines(positionalInputUsage),
|
||||
wrapLines(parseExample),
|
||||
wrapLines(docRef),
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
[next]
|
||||
no random generator output and generator output check
|
||||
errors
|
||||
spellcheck
|
||||
linting
|
||||
releasing
|
||||
formatter
|
||||
report unused parsers
|
||||
parse hashed, storing only the results
|
||||
linux packaging
|
||||
|
||||
[cmd]
|
||||
help for positional argument
|
||||
|
||||
[errors]
|
||||
take the last
|
||||
test error report on invalid flag
|
||||
|
1156
self/self.go
1156
self/self.go
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user